我正在尝试在 Matlab 中使用辛普森一家 3/8 规则求解数值积分,但我遇到了一个我不明白的错误。
图片中给出了数值积分(公式 26)
这也是 Matlab 代码:
clc;
clear all;
be = 1.5;
gam = 2;
N=1;
c=1;
f=@(x) 1./(x.*((-be*N)-(gam.*log(x))+(c*beta.*x))); %Change here for different function
a=0; % exmple a=1
b=1; % exmple b=2
n=30; % exmple n=21
h=(b-a)/n;
if rem(n,3)~=0
fprintf('\n Enter valid n!!!');
n=input('\n Enter n as multiple of 3: ');
end
for k=1:1:n
x(k)=a+k*h;
y(k)=f(x(k));
end
so=0;sm3=0;% Formula: (3h/8)*[(y0+yn)+2*(y3+y6+..multiple of 3 terms)+3*(y1+y2+y4+y5+...remining terms)]
for k=2:1:n-1
if rem(k,3)==0
sm3=sm3+y(k); %sum of multiple of 3 terms
else
so=so+y(k);%sum of others terms
end
end
answer=(3*h/8)*(f(a)+f(b)+3*so+2*sm3);
fprintf('\n The value of integration is %f',answer); % exmple The value of integration is 0.381665