Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 3.91 KB | None | 0 0
  1. /*function res = PolDeg(x)
  2.     res = 0
  3.     for i = 1:length(x)
  4.         if x(length(x)-i+1)~=0 then
  5.             res = length(x) - i
  6.             break
  7.         end
  8.     end
  9.    
  10. endfunction
  11. function res = PolS(a,b)
  12.  
  13.     sizeA = PolDeg(a)
  14.     sizeB = PolDeg(b)  
  15.     sizeRes = sizeA
  16.     if sizeB > sizeA then
  17.     c = b
  18.     b = a
  19.     a = c
  20.     sizeRes = sizeB
  21.     sizeA = PolDeg(a)
  22.         sizeB = PolDeg(b)
  23.     end
  24.     sizeRes = sizeRes + 1
  25.     res = [1:sizeRes] * 0
  26.     for i =1:sizeRes
  27.     res(i) = res(i) + a(i)
  28.     if i <= sizeB+1
  29.         res(i) = res(i) + b(i)
  30.     end
  31.     end
  32.  
  33.  
  34. endfunction
  35.  
  36. function res = PolM(a,b)
  37.     sizeA = PolDeg(a)
  38.     sizeB = PolDeg(b)  
  39.     sizeRes = sizeA + sizeB
  40.     res = [1:sizeRes+1] * 0
  41.     for i =1:sizeRes+1
  42.         for j = 1 :i
  43.             if (j <= sizeA+1) & (i-j+1<=sizeB+1) then
  44.                 res(i) =res(i)+ b(i-j+1) * a(j)
  45.             end
  46.         end
  47.     end
  48. endfunction
  49.  
  50. function res = EvaluatePol1(a,x)
  51.     sizeA = PolDeg(a)
  52.     res = 0
  53.     r = 1
  54.     for i = 1: sizeA+1
  55.         res = res + a(i) * r
  56.         r = r * x
  57.     end
  58. endfunction
  59. function res = EvaluatePol(a,x)
  60.     for i = 1: length(x)
  61.         x(i) = EvaluatePol1(a,x(i))
  62.     end
  63.     res = x
  64. endfunction
  65.  
  66. function res = Lagrange(x,y)
  67.         res = [0]
  68.         for i = 1:length(x)
  69.             yi = [y(i)]
  70.             for j = 1:length(x)
  71.                 if i ~= j then
  72.                     yi = PolM(yi ,( [-x(j)/ (x(i) - x(j)),1/ (x(i) - x(j))]  ))
  73.                 end        
  74.             end
  75.             res = PolS(res ,yi)
  76.         end
  77.     endfunction
  78. function res = DivDiff(x,y,n)
  79.         m = rand(length(x),n) * 0
  80.         for i =1:length(x)
  81.             m(1,i) = x(i)
  82.             m(2,i) = y(i)
  83.         end
  84.         for i=3:n+2
  85.             for j=1:length(x)+2-i
  86.                 m(i,j) = (m(i-1,j) - m(i-1,j+1))/(x(j) - x(j+i-2))
  87.             end
  88.         end
  89.     res = m
  90.     endfunction
  91.    
  92.     function res = Newton(x,y)
  93.         m= DivDiff(x,y,length(x))
  94.         res =[y(1)]
  95.         yi = [1]
  96.         for i = 1:length(x)-1
  97.             c = m(2+i,1)
  98.             yi = PolM(yi ,[-x(i),1])
  99.             res = PolS(res, PolM(yi,[c]) )
  100.         end
  101.     endfunction
  102. /////////
  103. */
  104. n=25
  105. from=0
  106. to=6
  107. x=[from:(to-from)/n:to]
  108. xc=[0]
  109. for i=1:n+1
  110.     xc(i)=(to+from)/2+((to-from)/2)*cos(((2*i-1)*%pi)/(2*(n+1)))
  111. end
  112. my_y=[0]
  113. //for i=1:n+1
  114.     my_y=exp(sin(x))
  115.     //disp(my_y(i))
  116. //end
  117. my_yc=[0]
  118. for i=1:n+1
  119.     my_yc(i)=exp(sin(xc(i)))
  120.     //disp(my_y(i))
  121. end
  122. function DD = DivDiff(x,y,n)
  123.         m = rand(length(x),n) * 0
  124.         for i =1:length(x)
  125.             m(1,i) = x(i)
  126.             m(2,i) = y(i)
  127.         end
  128.         for i=3:n+2
  129.             for j=1:length(x)+2-i
  130.                 m(i,j) = (m(i-1,j) - m(i-1,j+1))/(x(j) - x(j+i-2))
  131.             end
  132.         end
  133.     DD = m
  134. endfunction
  135. function Lagrange = Lag(x, y)
  136.     a=1
  137.     Lagrange=poly([0],"x","c")
  138.     for i=1:length(x)
  139.         c=1
  140.         Li=poly([a],"x","c")
  141.         for j=1:length(x)
  142.             if i~=j then
  143.                 li=poly([-x(j),1],"x", "c")/(x(i)-x(j))
  144.                 li=li
  145.                 Li=li*Li
  146.                 //disp(Li)
  147.             end
  148.         end
  149.         disp(y(i))
  150.         Lagrange=y(i)*Li+Lagrange
  151.     end
  152.     Lagrange=Lagrange/a
  153.     //disp(Lagrange)
  154. endfunction
  155. function Newtoon = Newt(x, y)
  156.     N=poly([1], "x", "c")
  157.     m= DivDiff(x,my_y,length(x))
  158.     disp(m)
  159.     Newtoon=poly([y(1)],"x","c")
  160.     for i=1:length(x)-1
  161.         Ni=poly(x(i), "x", "r")
  162.         N=N*Ni
  163.         Newtoon=Newtoon+N*m(2+i,1)
  164.     end
  165. endfunction
  166.  
  167. Newtoon=Newt(x,my_y)
  168. PolLag = Lag(x, my_y)
  169. PolLagCh = Lag(xc, my_yc)
  170. _nodes=[from:1/1000:to]
  171. value_calc = horner(PolLag,_nodes)
  172. value_calcCh = horner(PolLagCh,_nodes)
  173. //plot(_nodes,value_calc, "red")
  174. //plot(_nodes,value_calcCh, "green")
  175. plot(_nodes,exp(sin(_nodes)), "blue")
  176. value_calcN = horner(Newtoon,_nodes)
  177. plot(_nodes,value_calcN, "red")
  178. ///////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement