Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a=-1
- b=2
- function y=func(x)
- y=abs(sin(x))
- endfunction
- I=intg(a,b,func)
- function R=rectangles(x,n)
- R=0
- for i=1:n
- R=R+((x(i+1)-x(i))*(func((x(i+1)+x(i))./2)))
- end
- endfunction
- function T=Trapezium(x,n)
- T=0
- for i=1:n
- T=T+((x(i+1)-x(i))*((func(x(i+1))+func(x(i)))./2))
- end
- endfunction
- function S=Simpson(x,n)
- S=0
- for i=1:n
- A=func(x(i))+func(x(i+1))+4*func((x(i+1)+x(i))./2)
- S=S+(((x(i+1)-x(i))./6)* A)
- end
- endfunction
- function G=GaussTwo(x,n)
- G=0
- for i=1:n
- A=func(0.5*(x(i)+x(i+1))+(sqrt(3)/6)*(x(i+1)-x(i)))
- B=func(0.5*(x(i)+x(i+1))-(sqrt(3)/6)*(x(i+1)-x(i)))
- G=G+((x(i+1)-x(i))./2)*(A+B)
- end
- endfunction
- function Ga=GaussThree(x,n)
- Ga=0
- // корни полинома Лежандра
- l(1)=-sqrt(3/5)
- l(2)=0
- l(3)=sqrt(3/5)
- v(1)=0.55555555555555555555555555555555555555
- v(2)=0.88888888888888888888888888888888888888
- v(3)=0.55555555555555555555555555555555555555
- for i=1:n
- for j=1:3
- x1(j)=((0.5)*(x(i)+x(i+1)))+((0.5)*(x(i+1)-x(i))*l(j))
- c(j)=(0.5)*(x(i+1)-x(i))*v(j)
- Ga=Ga+c(j)*(func(x1(j)))
- end
- end
- endfunction
- for i=1:10
- n=2^i
- h=(b-a)/n
- X=a:h:b
- disp("------------")
- disp(sprintf('n=%6.0f',n))
- R(i)=log10(abs(rectangles(X,n)-I))
- disp(abs(rectangles(X,n)-I))
- T(i)=log10(abs(Trapezium(X,n)-I))
- disp(abs(Trapezium(X,n)-I))
- S(i)=log10(abs(Simpson(X,n)-I))
- disp(abs(Simpson(X,n)-I))
- G(i)=log10(abs(GaussTwo(X,n)-I))
- disp(abs(GaussTwo(X,n)-I))
- Ga(i)=log10(abs(GaussThree(X,n)-I))
- disp(abs(GaussThree(X,n)-I))
- end
- for i=1:10
- N(i)=log2(2^i)
- end
- subplot(5,1,1)
- //xlabel('n')
- ylabel('погрешность')
- plot(N,R,"r");
- xtitle('формула средних прямоугольников')
- subplot(5,1,2)
- //xlabel('n')
- ylabel('погрешность')
- plot(N,T,"r");
- xtitle('формула трапеций')
- subplot(5,1,3)
- //xlabel('n')
- ylabel('погрешность')
- plot(N,S,"r");
- xtitle('формула Симпсона')
- subplot(5,1,4)
- //xlabel('n')
- ylabel('погрешность')
- plot(N,G,"r");
- xtitle('формула Гаусса по двум узлам')
- subplot(5,1,5)
- //xlabel('n')
- ylabel('погрешность')
- plot(N,Ga,"r");
- xtitle('формула Гаусса по трем узлам')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement