Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.71 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 F=FormulaP()
  66.    
  67. endfunction
  68.    
  69.  
  70. xCount = 100;
  71. xInterpolCount = 10;
  72. a = -5;
  73. b = 5;
  74. h1 = (b-a)/(xCount - 1); //шаг для иксов функции
  75. h2 = (b-a)/(xInterpolCount - 1); //шаг для узлов функции
  76.  
  77. x1 = [a:h1:b]; //иксы функции
  78. x2 = [a:h2:b]; //узлы интерполяции
  79.  
  80. f1 = my_func(x1); //функция в иксах
  81. F1 = my_func(x2); //функция в узловых точках
  82. fN = newton_method(xCount, x1, xInterpolCount, x2, F1);//полином Ньютона
  83. fL=PolyLagr(xCount,xInterpolCount,x1,x2,F1)
  84. RealN=RealP(xCount,f1,fN)
  85. RealL=RealP(xCount,f1,fL)
  86. subplot(3,4,1)
  87. plot(x1,f1,"r",x1,fN,"b");
  88. xtitle('кр.-исходная функция, Син.-Ньютон')
  89.  
  90. subplot(3,4,2)
  91. plot(x1,f1,"r",x1,fL,"g");
  92. xtitle('кр.-исходная функция, зел.-Лагранж')
  93.  
  94. subplot(3,4,3)
  95. plot(x1,RealN,"g");
  96. xtitle('реал. погр. пол.Ньютона')
  97.  
  98. subplot(3,4,4)
  99. plot(x1,RealL,'r');
  100. xtitle('реал. погр. пол.Лагранжа')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement