用MATLAB程序编写复化梯形公式和复化simpsion公式求解积分

2020-04-22 社会 249阅读
%Compound trapezoid formula复化梯形法
function y = traint(a,b,n,f)
h = (b - a) / n;
x = linspace(a,b,n+1);
y1 = h * feval(f,x);
y1(1) = y1(1) / 2;
y1(n+1) = y1(n+1) / 2;
y = sum(y1);

%compound Simpson formula复化辛普森公式
function y = sraint(a,b,n,f)
h = (b - a) / n;
x = linspace(a,b,2*n+1);
y1 = feval(f,x);
y1(2:2:2*n) = 4 * y1(2:2:2*n);
y1(3:2:2*n-1) = 2 * y1(3:2:2*n-1);
y = h / 6 * sum(y1);

>>f=inline('4./(1+x.*x)');
>> traint(-1,1,8,f)
ans =
6.2624
>> sraint(-1,1,8,f)
ans =
6.2832
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com