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 ...
随机推荐
- C++字符类型总结区别wchar_t,char,WCHAR
转至:http://www.360doc.com/content/12/0807/01/9290626_228750141.shtml 1.区别wchar_t,char,WCHAR ANSI:即 ch ...
- MongoDB - Introduction to MongoDB, MongoDB Extended JSON
JSON can only represent a subset of the types supported by BSON. To preserve type information, Mongo ...
- JS中NULL和undifined区别及NULL的作用
1.博客地址:http://www.cnblogs.com/eastday/archive/2010/03/03/1677324.html 2.参考地址2:https://www.zhihu.com/ ...
- leetcode题1Two sum 练习
题目为: 给一个整数数组, 返回数组中的两数之和等于指定值的两数在数组中的下标. Example: Given nums = [2, 7, 11, 15], target = 9, Because n ...
- OSPF LSA的详解
LSA类型的配置与查看 1基本配置 R1(config)#NO IP DO LO R1(config)#NO ENAble PAssword R1(config)#LINe COnsole 0 R1( ...
- 20141128--JavaScript HTML DOM
通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素. HTML DOM 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). 每个 ...
- lua中pairs和ipairs的区别
标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 (string.gmatch)等等.LUA手册中 ...
- C++模板实例化(1)
On-Demand实例化 当C++编译器遇到模板特化的时候,他会利用所给的实参替换对应的模板参数,从而产生该模板的特化.该过程是自动进行的.有时候也会被称为隐式实例化,或者是自动实例化. on-dem ...
- python中文json串创建与解析
下面代码,举例说明了json如何创建和解析含有中文的json串: #coding=gbk import os import sys reload(sys) sys.setdefaultencoding ...
- 《openstack 和hadoop的区别是什么?》
openstack 和hadoop的区别是什么? (一) openstack仿照的Amazon的云,hadoop仿照的是Google的云 openstack注重的是虚拟化/虚拟机及其配套的服务,had ...