复合梯形法则:

function int_f = CompoundEchelon( f, a, b, m )
% input : f : function handler
% a : the lower limit of integral
% b : the upper limit of integral
% m : cut integral area into m peace
% output : int_f : the answer of the integral
h = (b - a) / m;
int_f = 0;
if m >= 2
for i = 1 : m-1
int_f = int_f + 2 * f(a + h * i);
end
end
int_f = int_f + f(a) + f(b);
int_f = int_f * h / 2;
end

例子:

clear all
format long
clc
%% (a)
fprintf(' (a) \n')
f = @(x) x./((x.^2+9).^0.5);
int1_16 = CompoundEchelon(f, 0, 4, 16);
int1_32 = CompoundEchelon(f, 0, 4, 32);
correct_int1 = quadgk(f, 0, 4);
error1_16 = abs(correct_int1 - int1_16);
error1_32 = abs(correct_int1 - int1_32);
fprintf('int1_16 = %g\n', int1_16);
fprintf('int1_32 = %g\n', int1_32);
fprintf('correct_int1 = %g\n', correct_int1);
fprintf('error1_16 = %g\n', error1_16);
fprintf('error1_32 = %g\n', error1_32);
%% (b)
fprintf(' (b) \n')
f = @(x) (x.^3)./(x.^2+1);
int2_16 = CompoundEchelon(f, 0, 1, 16);
int2_32 = CompoundEchelon(f, 0, 1, 32);
correct_int2 = quadgk(f, 0, 1);
error2_16 = abs(correct_int2 - int2_16);
error2_32 = abs(correct_int2 - int2_32);
fprintf('int2_16 = %g\n', int2_16);
fprintf('int2_32 = %g\n', int2_32);
fprintf('correct_int2 = %g\n', correct_int2);
fprintf('error2_16 = %g\n', error2_16);
fprintf('error2_32 = %g\n', error2_32);
%% (c)
fprintf(' (c) \n')
f = @(x) x.*exp(x);
int3_16 = CompoundEchelon(f, 0, 1, 16);
int3_32 = CompoundEchelon(f, 0, 1, 32);
correct_int3 = quadgk(f, 0, 1);
error3_16 = abs(correct_int3 - int3_16);
error3_32 = abs(correct_int3 - int3_32);
fprintf('int3_16 = %g\n', int3_16);
fprintf('int3_32 = %g\n', int3_32);
fprintf('correct_int3 = %g\n', correct_int3);
fprintf('error3_16 = %g\n', error3_16);
fprintf('error3_32 = %g\n', error3_32);
%% (d)
fprintf(' (d) \n')
f = @(x) (x.^2).*(log(x));
int4_16 = CompoundEchelon(f, 1, 3, 16);
int4_32 = CompoundEchelon(f, 1, 3, 32);
correct_int4 = quadgk(f, 1, 3);
error4_16 = abs(correct_int4 - int4_16);
error4_32 = abs(correct_int4 - int4_32);
fprintf('int4_16 = %g\n', int4_16);
fprintf('int4_32 = %g\n', int4_32);
fprintf('correct_int4 = %g\n', correct_int4);
fprintf('error4_16 = %g\n', error4_16);
fprintf('error4_32 = %g\n', error4_32);
%% (e)
fprintf(' (e) \n')
f = @(x) (x.^2).*(sin(x));
int5_16 = CompoundEchelon(f, 0, pi, 16);
int5_32 = CompoundEchelon(f, 0, pi, 32);
correct_int5 = quadgk(f, 0, pi);
error5_16 = abs(correct_int5 - int5_16);
error5_32 = abs(correct_int5 - int5_32);
fprintf('int5_16 = %g\n', int5_16);
fprintf('int5_32 = %g\n', int5_32);
fprintf('correct_int5 = %g\n', correct_int5);
fprintf('error5_16 = %g\n', error5_16);
fprintf('error5_32 = %g\n', error5_32);
%% (f)
fprintf(' (f) \n')
f = @(x) (x.^3)./((x.^4-1).^0.5);
int6_16 = CompoundEchelon(f, 2, 3, 16);
int6_32 = CompoundEchelon(f, 2, 3, 32);
correct_int6 = quadgk(f, 2, 3);
error6_16 = abs(correct_int6 - int6_16);
error6_32 = abs(correct_int6 - int6_32);
fprintf('int6_16 = %g\n', int6_16);
fprintf('int6_32 = %g\n', int6_32);
fprintf('correct_int6 = %g\n', correct_int6);
fprintf('error6_16 = %g\n', error6_16);
fprintf('error6_32 = %g\n', error6_32);
%% (g)
fprintf(' (g) \n')
f = @(x) 1./((x.^2+4).^0.5);
int7_16 = CompoundEchelon(f, 0, 2*3^0.5, 16);
int7_32 = CompoundEchelon(f, 0, 2*3^0.5, 32);
correct_int7 = quadgk(f, 0, 2*3^0.5);
error7_16 = abs(correct_int7 - int7_16);
error7_32 = abs(correct_int7 - int7_32);
fprintf('int7_16 = %g\n', int7_16);
fprintf('int7_32 = %g\n', int7_32);
fprintf('correct_int7 = %g\n', correct_int7);
fprintf('error7_16 = %g\n', error7_16);
fprintf('error7_32 = %g\n', error7_32);
%% (h)
fprintf(' (h) \n')
f = @(x) x./((x.^4+1).^0.5);
int8_16 = CompoundEchelon(f, 0, 1, 16);
int8_32 = CompoundEchelon(f, 0, 1, 32);
correct_int8 = quadgk(f, 0, 1);
error8_16 = abs(correct_int8 - int8_16);
error8_32 = abs(correct_int8 - int8_32);
fprintf('int8_16 = %g\n', int8_16);
fprintf('int8_32 = %g\n', int8_32);
fprintf('correct_int8 = %g\n', correct_int8);
fprintf('error8_16 = %g\n', error8_16);
fprintf('error8_32 = %g\n', error8_32);

matlab实现复合梯形法则的更多相关文章

  1. 复合梯形公式、复合辛普森公式 matlab

    1. 用1阶至4阶Newton-Cotes公式计算积分 程序: function I = NewtonCotes(f,a,b,type) % syms t; t=findsym(sym(f)); I= ...

  2. MatLab 组件大全

    MATLAB                                                                    矩阵实验室 7.0.1 Simulink       ...

  3. MATLAB 编程风格指南及注意事项

    MATLAB编程风格指南Richard Johnson 著Genial 译MATLAB 编程风格指南Richard JohnsonVersion 1.5,Oct. 2002版权: Datatool 所 ...

  4. MATLAB中trapz和cumtrapz函数

    这两个函数都是MATLAB中的内置函数,是基于梯形法则的数值积分公式 例如我们有函数y=x^3-2x-3,为了计算在[0,1]上的积分,可以这么做: 其中x和y分别是自变量和对应的值,trapz其实就 ...

  5. matlab中s函数编写心得(转)

    Part I: 所谓s函数是system Function的简称, 用它来写自己的simulink模块. s函数可以用matlab.C.C++.Fortran.Ada等语言来写, 这儿我只介绍怎样用m ...

  6. [学习一个] Matlab GUI 学习笔记 Ⅰ

    Matlab GUI 学习笔记 Ⅰ 1. Foreword Matlab 是严格意义上的编程语言吗?曾经有人告诉我他是通过 Matlab 学会了面对对象编程,我是不信的,但这依然不妨碍它在特殊领域的强 ...

  7. Matlab基本数学应用

    基本线性代数 [R jb]=rref(A)将A化为行最简型矩阵.R为所得行最简型矩阵,jb是一个向量显示每行首非0元所在列号. inv(A)求方阵A的逆,注意结果可能出现错误.当结果中出现Inf和Na ...

  8. 基于MATLAB的多项式数据拟合方法研究-毕业论文

    摘要:本论文先介绍了多项式数据拟合的相关背景,以及对整个课题做了一个完整的认识.接下来对拟合模型,多项式数学原理进行了详细的讲解,通过对文献的阅读以及自己的知识积累对原理有了一个系统的认识.介绍多项式 ...

  9. MATLAB数学实验总结

    L1 MATLAB 基础知识 P6 表1-3 数据显示格式 format rat format long P20 表2-5 常用的矩阵函数 zeros(m,n) %零阵 eye(n) %单位阵 one ...

随机推荐

  1. 剑指Offer14 逆序链表

    /************************************************************************* > File Name: 14_Revers ...

  2. 非常棒的 「Sublime Text 配色/主题」与「编程字体」

    *标有 CT 的是配色 **主题中调用的字体和相配套的Sublime主程序图标可访问GitHub获取 Afterglow https://github.com/YabataDesign/aftergl ...

  3. 9种CSS3炫酷图片展开预览展示动画特效

    详细内容请点击 在线预览立即下载 这是一组共9款CSS3炫酷图片预览展示动画特效插件.css的新特性可以让我们制作出各种炫酷的动画效果.该图片预览展示动画特效就是一个很好的例子,该效果开始时图片堆叠在 ...

  4. 【Mongodb】---基本命令

    show dbs:显示数据库列表 show collections:显示当前数据库中的集合(类似关系数据库中的表) show users:显示用户 use <db name>:切换当前数据 ...

  5. 图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  6. Jquery实现循环删除Reaper某一行

    一.实现的效果图:(点击删除图标,juery实现删除整行) 二.MVC开发模式 SQLServer层 #region 删除 /// <summary> /// 根据自动编号删除快递线路信息 ...

  7. Windows SDK 之 Hook的使用

    在使用SetWindowsHookEx的过程中遇到的问题 函数原型 HHOOK WINAPI SetWindowsHookEx( _In_ int idHook, _In_ HOOKPROC lpfn ...

  8. PHP中大括号{}用法总结

    刚用到一个由字符串来设定对像属性名的功能.发现大括号的作用真强…. 1. 动态设置对象的属性名的使用:写法一(不能正确设置): $obj->$string[$key]; //这里只能使用$str ...

  9. 【Unity3D】Unity3D之 注册表动态存取游戏存档——PlayerPrefs类

    [Unity3D]Unity3D之 注册表动态存取游戏存档--PlayerPrefs类 1.Unity3D提供了一个用于本地持久化保存与读取的类--PlayerPrefs.工作原理非常简单,以键值对的 ...

  10. 初识sass

    大图:http://images2015.cnblogs.com/blog/730765/201605/730765-20160529113743209-72994369.png