Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 5.08 KB | None | 0 0
  1. //Феномен Рунге
  2. function y=my_func(x)
  3. y = 1./(1 + x.^2)
  4. endfunction
  5.  
  6. function y=newton_method(xCount, x, xInterpolCount, Xn, Yn)
  7. //заполняем нулями и высчитываем диагональ
  8.     for i=1:xInterpolCount
  9.         for j=1:xInterpolCount
  10.             if i==j then
  11.                 M(i,j) = Yn(i);
  12.             else
  13.                 M(i,j) = 0;
  14.             end;
  15.         end;
  16.     end;
  17.  
  18. //высчитываем разделённые разности
  19.     for i=1:+1:xInterpolCount-1
  20.         for j=1:xInterpolCount-i
  21.             M(j,j+i)=(M(j+1,j+i) - M(j,j+i-1)) / (Xn(j+i)-Xn(j));
  22.         end
  23.     end;
  24.  
  25.     for i = 1:xCount
  26.         y(i) = Yn(1);
  27.         if (xInterpolCount > 1)
  28.         then
  29.         p=1;
  30.             for u=2:+1:xInterpolCount
  31.                 p = p*(x(i) - Xn(u-1));
  32.                 y(i) = y(i) + (M(1,u)*p);
  33.             end
  34.         end
  35.     end
  36. endfunction
  37.  
  38. function p=PolyLagr(m,n,x,xt,ft)
  39.     // m кол-во Х  n- кол-во узлов
  40.     //x-наши задаваемые Х     xt-узлы
  41.     // ft - знач в узлах
  42.     for k=1:m
  43.         sum=0
  44.         for i=1:n
  45.             w(i)=1
  46.             for j=1:n
  47.                 if (j ~= i)
  48.             w (i) =w(i) * (x (k) - xt (j)) / (xt (i) - xt (j));
  49.                 end
  50.             end
  51.         sum = sum + ft(i) * w(i);
  52.     end
  53.     p(k) =sum;
  54.     end
  55. endfunction
  56.  
  57. function RP=RealP(n,f,f1)
  58.     // f-данная функция   f1- интерполяц. полином
  59.     for i=1:n
  60.     RP(i)= abs(f(i)-f1(i))
  61.     end
  62. endfunction
  63.  
  64. // M- максимум в 10 производной
  65. function Pr=Proizv(x,N) //n - степень
  66.     i=0
  67.    for  j=1:2:N //четные
  68.        d1(j)=%i*((-1)^(i+1))*(factorial (j))./(1+%i*x)^(j+1)
  69.        d2(j)=%i*((-1)^(j+i+1))*(factorial (j))./(1-%i*x)^(j+1)
  70.        i=i+1
  71.    end
  72.    
  73.    c=0
  74.    for j=2:2:N //нечетное
  75.        d1(j)=((-1)^(j+c))*(factorial (j))./(1+%i*x)^(j+1)
  76.        d2(j)=((-1)^(j+c+1))*(factorial (j))./(1-%i*x)^(j+1)
  77.        
  78.      c=c+1
  79.    end
  80.    P=0
  81.    for i=1:N
  82.    P(i)=abs(0.5*d1(i)+0.5*d2(i))
  83. end
  84. Pr=P(N)
  85. endfunction
  86. //disp(Proizv(2,3))
  87. function M=Max(N,X)
  88.     for i=1:N
  89.         f(i)=Proizv(X(i),N)
  90.     end
  91.     M=f(1)
  92.     if N > 1 then
  93.         for j=2:N
  94.             if real(M) <= real(f(j))
  95.                 if imag(M)<=imag(f(j))
  96.                      
  97.             M=f(j)
  98.             end
  99.         end
  100.     end
  101.     end
  102. endfunction
  103.  
  104. function Ap=ApproximateP(nx,xx,nu,xu,M,N_Proiz) //приближенная оценка
  105.     // nx- кол-во иксов
  106.     //xx - исксы
  107.     // ny - кол-во узлов
  108.     // xy- узлы
  109.  
  110.     M=Max(N_Proiz,xx)
  111.     //disp(M)
  112.     for j=1:nx
  113.         p=1
  114.         for i=1:nu
  115.             p=abs(p*(xx(j)-xu(i)))
  116.         end
  117.        
  118.      Ap(j)=M*p/factorial(nx+1)
  119.         end
  120. endfunction
  121.  
  122. function Ch=Chebyshev(a, b, n) //узлы Чебышёва
  123. for i=1:n
  124. Ch(i)=0.5*(b+a)+0.5*(b-a)*cos((2*i+1)*%pi/(2*(n+1)));
  125. end
  126.  
  127. endfunction
  128.  
  129.  
  130. N_Proiz=10
  131. xCount = 100;
  132. xInterpolCount = 15;//кол-во узлов
  133. a = -5;
  134. b = 5;
  135. h1 = (b-a)/(xCount - 1); //шаг для иксов функции
  136. h2 = (b-a)/(xInterpolCount - 1); //шаг для узлов функции
  137.  
  138. x1 = [a:h1:b]; //иксы функции
  139. x2 = [a:h2:b]; //узлы интерполяции
  140.  
  141. Ch=Chebyshev(a,b,xInterpolCount) //узлы интерпол по Чеб.
  142.  
  143. f1 = my_func(x1) //функция в иксах
  144. F_Ch=my_func(Ch)
  145. F1 = my_func(x2) //функция в узловых точках
  146.  
  147. fN = newton_method(xCount, x1, xInterpolCount, x2, F1);//полином Ньютона
  148. fL=PolyLagr(xCount,xInterpolCount,x1,x2,F1)
  149. RealN=RealP(xCount,f1,fN)
  150. RealL=RealP(xCount,f1,fL)
  151. Ap=ApproximateP(xCount,x1,xInterpolCount,x2)
  152.  
  153. fN_Ch = newton_method(xCount, x1, xInterpolCount, Ch, F_Ch);
  154. fL_Ch=PolyLagr(xCount,xInterpolCount,x1,Ch,F_Ch)
  155. Real_N_Ch=RealP(xCount,f1,fN_Ch)
  156. Real_L=RealP(xCount,f1,fL_Ch)
  157. Ap_Ch=ApproximateP(xCount,x1,xInterpolCount,Ch)
  158.  
  159. subplot(3,4,1)
  160. plot(x1,f1,"r",x1,fN,"b");
  161. xtitle('кр.-исходная функция, Син.-Ньютон')
  162.  
  163. subplot(3,4,2)
  164. plot(x1,f1,"r",x1,fL,"g");
  165. xtitle('кр.-исходная функция, зел.-Лагранж')
  166.  
  167. subplot(3,4,3)
  168. plot(x1,RealN,"g");
  169. xtitle('реал. погр. пол.Ньютона')
  170.  
  171. subplot(3,4,4)
  172. plot(x1,RealL,'r');
  173. xtitle('реал. погр. пол.Лагранжа')
  174.  
  175. subplot(3,4,5)
  176. plot(x1,Ap,"r");
  177. xtitle('приближенная оценка погрешности')
  178.  
  179. //Для Чебыш.
  180.  
  181. subplot(3,4,6)
  182. plot(x1,f1,"r",x1,fN_Ch,"b");
  183. xtitle('кр.-исходная функция, Син.-Ньютон')
  184.  
  185. subplot(3,4,7)
  186. plot(x1,f1,"r",x1,fL_Ch,"g");
  187. xtitle('кр.-исходная функция, зел.-Лагранж')
  188.  
  189. subplot(3,4,8)
  190. plot(x1,Real_N_Ch,"g");
  191. xtitle('реал. погр. пол.Ньютона')
  192.  
  193. subplot(3,4,9)
  194. plot(x1,Real_L,'r');
  195. xtitle('реал. погр. пол.Лагранжа')
  196.  
  197. subplot(3,4,10)
  198. plot(x1,Ap_Ch,"r");
  199. xtitle('приближенная оценка погрешности')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement