Matlab梯形法则

问题描述 投票:0回答:1

enter image description here

我创建了一个 fas.m 脚本,但得到了错误的结果。

功能

 function [fnc2] = fas(x)
if x>=0 && x<1
fnc2 = x^3;
elseif x>=1 && x<2
        fnc2 = 2-((x^2)/5);
elseif x>2
            fnc2 = x^2+x;
 elseif x<0 
     fprintf('x is smaller than 0, function is not defined');

end

梯形规则和

clear
clc
h=0.05;
x=0.05;
x0=0;
xn=3;
while x<=2.95
fas(x);
I=0.025*(fas(x0)+2*fas(x)+fas(x0));
x=x+h;
end
function matlab numerical-integration
1个回答
2
投票

梯形法则是,

enter image description here

所以,

h = 0.05;
x = 0;
I = 0;
while x < 3
   I = I + h * (fas(x) + fas(x + h)) / 2;
   x = x + h;
end
disp(I);

您将得到

I = 11.3664
,而
I
的实际值为
10.3667

© www.soinside.com 2019 - 2024. All rights reserved.