📄 m5_1.m
字号:
%%%%%%%%%%%%%%%% Trapezoidal or Simpson %%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\n Rules of Trapezoidal/Simpson\n');
fprintf('The function to be integrated is hard--coded infunction()\n');
z=input('Type 0 for Trapezoidal,or 1 for Simpson :');
while (z~=0 | z~=1)
fprintf('\n\n Warn:Please enter 0 or 1!!\n');
z=input('Type 0 for Trapezoidal,or 1 for Simpson :');
if z==0|z==1 break;end
end
n=input('Numerical of interval?');
a=input('Lower limit of integration?');
b=input('upper limit of integration?');
h=(b-a)/n;
if z==0
x=[a:h:b];
y=f(x);
Result=h/2*(2*sum(y)-f(a)-f(b));
fprintf('Result=%12.5f.\n',Result);
end
if z==1
x=[a:h:b];
for i=1:n
xx(i)=x(i)+h/2;
end
yy=f(xx);
y=f(x);
Result=h/6*(4*sum(yy)+2*sum(y)-f(a)-f(b));
fprintf('Result=%12.5f.\n',Result);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -