Matlab使用Simpson辛普森法计算数值积分

Matlab 运用Simpson辛普森公式求定积分(附代码)

直接附代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
simpson.m
function [out] = simpson(f,n,a,b)
%SIMPSON 此处显示有关此函数的摘要
% 此处显示详细说明

h=(b-a)/n;

s1=fi(f,1,h,a)+fi(f,n+1,h,a);

s2=0;
for index=3:2:n-1
s2=s2+2*fi(f,index,h,a);
end

s4=0;
for index=2:2:n
s4=s4+4*fi(f,index,h,a);
end

out=h/3*(s1+s2+s4);
end


function [out] = fi(f,i,h,a)

out=f(a+(i-1)*h);
end

调用:

1
simpson(<函数句柄>, <n>, <下限>, <上限>)

参考文章

matlab用辛普森公式求积分_积分近似计算之辛普森公式_weixin_39726131的博客-CSDN博客

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2020-2022 Eigeen
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信