MATLAB complex trapezoidal formula, complex Simpson formula

insert image description here

insert image description here
fm file:

function f = f(x)
f = (x^2)*sin(x);
end

Compound Trapezoidal Formula

Tn.m file:

function Tn=Tn(n) % n代表区间数
    a = -2; % 区间下界
    b = 2; % 区间下界
    h=(b-a)/n;
    sum=0;
    for k=1:n-1
        sum=sum+f(a+k.*h);
    end
 
    Tn=(f(a)+2*sum+f(b))*h/2;
end

Simpson Official

Sn.m file:

function Sn = Sn(n)      
    a = -2;
    b = 2;
    h = (b-a)/n;
    sum1 = 0;
    sum2 = 0;
    for i = 0:n-1
        sum1 = sum1 + f(a+(i+1/2).*h);
    end
    for j = 1:n-1
        sum2 = sum2 + f(a+j.*h);
    end
    Sn = h/6*(f(a)+4*sum1+2*sum2+f(b));

Guess you like

Origin blog.csdn.net/weixin_41866717/article/details/117067040