Advertisement
Guest User

Untitled

a guest
Nov 4th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.40 KB | None | 0 0
  1. a=-1
  2. b=2
  3.  
  4.  
  5. function y=func(x)
  6.     y=abs(sin(x))
  7. endfunction
  8.  
  9. I=intg(a,b,func)
  10.  
  11. function R=rectangles(x,n)
  12.     R=0
  13.     for i=1:n
  14.         R=R+((x(i+1)-x(i))*(func((x(i+1)+x(i))./2)))    
  15.     end
  16. endfunction  
  17.  
  18. function T=Trapezium(x,n)
  19.      T=0
  20.     for i=1:n
  21.         T=T+((x(i+1)-x(i))*((func(x(i+1))+func(x(i)))./2))    
  22.     end
  23. endfunction
  24.  
  25. function S=Simpson(x,n)
  26.      S=0
  27.     for i=1:n
  28.         A=func(x(i))+func(x(i+1))+4*func((x(i+1)+x(i))./2)
  29.         S=S+(((x(i+1)-x(i))./6)* A)
  30.     end
  31. endfunction
  32.  
  33. function G=GaussTwo(x,n)
  34.     G=0
  35.    
  36.     for i=1:n
  37.         A=func(0.5*(x(i)+x(i+1))+(sqrt(3)/6)*(x(i+1)-x(i)))
  38.         B=func(0.5*(x(i)+x(i+1))-(sqrt(3)/6)*(x(i+1)-x(i)))
  39.         G=G+((x(i+1)-x(i))./2)*(A+B)
  40.     end
  41. endfunction
  42.  
  43.  
  44. function Ga=GaussThree(x,n)
  45.     Ga=0
  46.     // корни полинома Лежандра
  47.     l(1)=-sqrt(3/5)
  48.     l(2)=0
  49.     l(3)=sqrt(3/5)
  50.     v(1)=0.55555555555555555555555555555555555555
  51.     v(2)=0.88888888888888888888888888888888888888
  52.     v(3)=0.55555555555555555555555555555555555555
  53.     for i=1:n
  54.     for j=1:3
  55.         x1(j)=((0.5)*(x(i)+x(i+1)))+((0.5)*(x(i+1)-x(i))*l(j))
  56.         c(j)=(0.5)*(x(i+1)-x(i))*v(j)
  57.         Ga=Ga+c(j)*(func(x1(j)))
  58.     end
  59.    
  60. end
  61. endfunction
  62.  
  63.  
  64. for i=1:10
  65.     n=2^i
  66.     h=(b-a)/n
  67.     X=a:h:b
  68.      disp("------------")
  69.     disp(sprintf('n=%6.0f',n))
  70. R(i)=log10(abs(rectangles(X,n)-I))
  71. disp(abs(rectangles(X,n)-I))
  72. T(i)=log10(abs(Trapezium(X,n)-I))
  73. disp(abs(Trapezium(X,n)-I))
  74. S(i)=log10(abs(Simpson(X,n)-I))
  75. disp(abs(Simpson(X,n)-I))
  76. G(i)=log10(abs(GaussTwo(X,n)-I))
  77. disp(abs(GaussTwo(X,n)-I))
  78. Ga(i)=log10(abs(GaussThree(X,n)-I))
  79. disp(abs(GaussThree(X,n)-I))
  80. end
  81.  
  82.  
  83. for i=1:10
  84. N(i)=log2(2^i)
  85. end
  86.  
  87. subplot(5,1,1)
  88. //xlabel('n')
  89. ylabel('погрешность')
  90. plot(N,R,"r");
  91. xtitle('формула средних прямоугольников')
  92.  
  93. subplot(5,1,2)
  94. //xlabel('n')
  95. ylabel('погрешность')
  96. plot(N,T,"r");
  97. xtitle('формула трапеций')
  98.  
  99. subplot(5,1,3)
  100. //xlabel('n')
  101. ylabel('погрешность')
  102. plot(N,S,"r");
  103. xtitle('формула Симпсона')
  104.  
  105. subplot(5,1,4)
  106. //xlabel('n')
  107. ylabel('погрешность')
  108. plot(N,G,"r");
  109. xtitle('формула Гаусса по двум узлам')
  110.  
  111. subplot(5,1,5)
  112. //xlabel('n')
  113. ylabel('погрешность')
  114. plot(N,Ga,"r");
  115. xtitle('формула Гаусса по трем узлам')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement