matlab实现复合梯形法则
复合梯形法则:
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实现复合梯形法则的更多相关文章
- 复合梯形公式、复合辛普森公式 matlab
1. 用1阶至4阶Newton-Cotes公式计算积分 程序: function I = NewtonCotes(f,a,b,type) % syms t; t=findsym(sym(f)); I= ...
- MatLab 组件大全
MATLAB 矩阵实验室 7.0.1 Simulink ...
- MATLAB 编程风格指南及注意事项
MATLAB编程风格指南Richard Johnson 著Genial 译MATLAB 编程风格指南Richard JohnsonVersion 1.5,Oct. 2002版权: Datatool 所 ...
- MATLAB中trapz和cumtrapz函数
这两个函数都是MATLAB中的内置函数,是基于梯形法则的数值积分公式 例如我们有函数y=x^3-2x-3,为了计算在[0,1]上的积分,可以这么做: 其中x和y分别是自变量和对应的值,trapz其实就 ...
- matlab中s函数编写心得(转)
Part I: 所谓s函数是system Function的简称, 用它来写自己的simulink模块. s函数可以用matlab.C.C++.Fortran.Ada等语言来写, 这儿我只介绍怎样用m ...
- [学习一个] Matlab GUI 学习笔记 Ⅰ
Matlab GUI 学习笔记 Ⅰ 1. Foreword Matlab 是严格意义上的编程语言吗?曾经有人告诉我他是通过 Matlab 学会了面对对象编程,我是不信的,但这依然不妨碍它在特殊领域的强 ...
- Matlab基本数学应用
基本线性代数 [R jb]=rref(A)将A化为行最简型矩阵.R为所得行最简型矩阵,jb是一个向量显示每行首非0元所在列号. inv(A)求方阵A的逆,注意结果可能出现错误.当结果中出现Inf和Na ...
- 基于MATLAB的多项式数据拟合方法研究-毕业论文
摘要:本论文先介绍了多项式数据拟合的相关背景,以及对整个课题做了一个完整的认识.接下来对拟合模型,多项式数学原理进行了详细的讲解,通过对文献的阅读以及自己的知识积累对原理有了一个系统的认识.介绍多项式 ...
- MATLAB数学实验总结
L1 MATLAB 基础知识 P6 表1-3 数据显示格式 format rat format long P20 表2-5 常用的矩阵函数 zeros(m,n) %零阵 eye(n) %单位阵 one ...
随机推荐
- hdu 3311 斯坦纳树
思路:虚拟一个0号节点,将每个点建一条到0号节点的边,权值为挖井需要的价值.并要保证0号节点同另外n个寺庙一样被选择即可. 然后就是求斯坦纳树了. #include<map> #inclu ...
- Ubuntu 16.04 - python3 安装mysql驱动
阿西吧,今天碰到一件特别蛋疼的事,给Ubuntu安装Python的MySQL驱动,驱动显示安装成功了 pip install mysql-connector 但是 在程序中导入,老是报错. Trace ...
- CSS之密码强度检测
输入密码后单击空白处即可检测. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- TouchAndGuest触摸事件和手势
//// nextViewController.m#import "nextViewController.h"#import "my.h"@interface ...
- Row_Number实现分页(适用SQL)
1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号的集合 2:再查询该集合的 第 1 ...
- 20141124-JS函数
函数: 函数是由事件驱动或者当它被调用时执行的可重复色代码块. <head> <script> function hanshu() { alert("你好!" ...
- UI1_UITabBarController
// // AppDelegate.h // UI1_UITabBarController // // Created by zhangxueming on 15/7/8. // Copyright ...
- 使用单用户模式破解Linux密码
使用单用户模式破解Linux密码 特别说明:在实际工作应用中,安装Linux操作系统必须设置装载口令,否则很容易被破解. 1.使用reboot指令重启Linux操作系统 2.在进入操作系统数秒时,单击 ...
- SAP存货账龄分析之库存获取
前段时间上面要求做一个历史库存账龄分析,取历史数据的时候一直纠结于用mchb/mchbh/mska/mskah等实时和历史库存表,然而试来试去还是不能成功,于是决定DEBUG下MB5B的源代码,测试了 ...
- IOS引导页拨动4张图片最后一张停三秒进入主页,页面推送
// // ViewController.m // // // Created by 张艳锋 on 15/8/26. // Copyright (c) 2015年 张艳锋. All rights ...