复化Simpson公式以及复化梯形公式

  • 格式:docx
  • 大小:16.69 KB
  • 文档页数:3

数值分析程序:

Simpson公式,梯形,以及复合Simspon,复合梯形公式:

clc

clear

%fuhuaTX

syms x;

a=input('a=');

b=input('b=');

n1=input('分割n1=');

h=(b-a)/n1;

sum=0;

fori=1:n1-1

sum=sum+f(a+i*h);

end

%f0=0;

%sum=2*sum+f0+f(b);

sum=2*sum+f(a)+f(b);

TX=sum*h/2

%代数精度

m=200;n=0;

for j=1:m

D=int(g(j,x),x,a,b);

h1=(b-a)/2;

s1=g(j,a)+g(j,b);

T=h1*s1;

if(T==D)

n=n+1;

else

n=n;

end

end

disp('梯形公式的代数精度为')

disp(n)

n=0;sum=0;

for j=1:m

D=int(g(j,x),x,a,b);

h1=h/2;

fori=1:n-1

sum=sum+g(i,a+i*h);

end

s1=2*sum+g(j,a)+g(j,b);

T=h1*s1;

if(T==D)

n=n+1; else

n=n;

end

end

disp('复合梯形公式的代数精度为')

disp(n)

%fuhuaSimps

syms x;

format short

n=n1/2;

n1=2*n;

h=(b-a)/n;

sum1=0;sum2=0;

fori=1:n-1

sum1=sum1+2*f(a+i*h);%中间偶数点2倍求和

end

fori=0:n-1

sum2=sum2+4*f((i+(i+1))/n1); %中间奇数点

end

%f0=0;

%sum=sum1+sum2+f0+f(b);

sum=sum1+sum2+f(a)+f(b);

Simpson=sum*h/6

%代数精度

m=200;n2=0;N=zeros(1,m);

for j=1:m

D=int(g(j,x),x,a,b);

sum1=0;sum2=0;

fori=1:n-1

sum1=sum1+2*g(j,a+i*h);%中间偶数点2倍求和

end

fori=0:n-1

sum2=sum2+4*g(j,(i+(i+1))/n1); %中间奇数点

end

sum=sum1+sum2+g(j,a)+g(j,b);

T=sum*h/6;

M=abs(D-T);

N(1,j)=M;

end

for j=1:m

if N(1,j)<0.0001

n2=n2+1;

else

n2=n2; end

end

disp('复化Simpson公式的代数精度为')

disp(n2)

n3=0;

for j=1:m

D=int(g(j,x),x,a,b);

h1=(b-a)/6;

s1=g(j,a)+g(j,b)+4*g(j,(a+b)/2);

T=h1*s1;

if(T==D)

n3=n3+1;

else

n3=n3;

end

end

disp('Simpson公式的代数精度为')

disp(n3)