Matlab梯形规则

时间:2014-12-25 01:20:18

标签: matlab function integration

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

TRAPEZOIDAL RULE SUM

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

1 个答案:

答案 0 :(得分: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