我创建了一个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
答案 0 :(得分:2)
梯形规则是,
所以,
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
。