辛普森1/3规则

时间:2018-03-09 23:24:32

标签: matlab simpsons-rule

问题的解决方案是Simpson 1/3规则的1.732400451459101。相反,程序给我的解决方案是1.73239801

任何人都可以帮助我吗?提前谢谢。

clc
clear
close all

f = @(x) sin(x);
a = 0.1; 
g = a;
b = 2.4;
k = 19;   

n = 2*k;
S = 0;
h = (b-a)/n;

for i=1:k
    S=S+(h/3)*(f(a)+4*f(a+h)+f(a+2*h));
    a=a+2*h;

end

fprintf('La integral se aproxima a: %0.8f \n',S)

syms x

y = sin(x);

InT = int(y,g,b);

InT = double(InT)

1 个答案:

答案 0 :(得分:0)

Composite Simpson's rule逼近积分时的错误是:

enter image description here

1.7149e-07案例中约为f(x)=sin(x),这意味着绝对误差界限为9.8990e-08,这对我来说很合适。

此外,以下是上述代码的替代方法:

f = @(x) sin(x);
[a,b,g,k]=deal(0.1,2.4,0.1,19);
[S,n,h]=deal(0,2*k,(b-a)/(2*k));
for i=1:k
    S=S+(h/3)*(f(g)+4*f(g+h)+f(g+2*h));
    g=g+2*h;
end

或者,我们可以打电话:

f = @(x) sin(x);
[a,b,k]=deal(0.1,2.4,19);
Int = a:(b-a)/(2*k):b;
S=(b-a)/(6*k) * ( f(Int(1)) + 4*sum(f(Int(2:2:end-1))) ...
                + 2*sum(f(Int(3:2:end-2))) + f(Int(end)));