Advertisement
Guest User

Untitled

a guest
Nov 30th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 3.33 KB | None | 0 0
  1. n=19
  2.  
  3. from=0
  4. to=6
  5. x=[from:(to-from)/n:to]
  6. xch=[0]
  7. for i=1:n+1
  8.     xch(i)=((to+from)/2)+((to-from)/2)*cos(((2*i-1)*%pi)/(2*(n+1)))
  9. end
  10. f=exp(sin(x))
  11. my_y=[0]
  12. for i=1:length(x)
  13.     my_y(i)=exp(sin(x(i)))
  14. end
  15.  
  16. my_ych=[0]
  17. for i=1:length(xch)
  18.     my_ych(i)=exp(sin(xch(i)))
  19. end
  20. function P = PP(a, b)
  21.     for i=1:length(a)
  22.         for j=1:length(b)
  23.             P(i+j-1)=0
  24.         end
  25.     end
  26.     for i=1:length(a)
  27.         for j=1:length(b)
  28.             P(i+j-1)=P(i+j-1)+a(i)*b(j)
  29.         end
  30.     end
  31. endfunction
  32. function sump = Sum(a, b)
  33.     m=abs(length(a)-length(b))
  34.     for i=1:length(a)
  35.         for j=1:length(b)
  36.             if i==j then
  37.                 sump(i)=a(i)+b(j)
  38.             end
  39.         end
  40.     end
  41.     k=length(sump)
  42.     if m~=0 then
  43.         for s=(k+1):(k+m)
  44.             if length(a)>length(b) then
  45.                 sump(s)=a(s)
  46.             end
  47.             if length(b)>length(a) then
  48.                 sump(s)=b(s)
  49.             end
  50.         end
  51.     end
  52. endfunction
  53. function difp = Diff(a, b)
  54.     m=abs(length(a)-length(b))
  55.     for i=1:length(a)
  56.         for j=1:length(b)
  57.             if i==j then
  58.                 difp(i)=a(i)-b(j)
  59.             end
  60.         end
  61.     end
  62.     k=length(difp)
  63.     if m~=0 then
  64.         for s=(k+1):(k+m)
  65.             if length(a)>length(b) then
  66.                 difp(s)=a(s)
  67.             end
  68.             if length(b)>length(a) then
  69.                 difp(s)=-b(s)
  70.             end
  71.         end
  72.     end
  73. endfunction
  74.  
  75. function Lagrange = Lag(x, y)
  76.     Lagrange=1
  77.     for i=1:length(x)
  78.         Li=1
  79.         for j=1:length(x)
  80.             if i~=j then
  81.                 li=[-x(j)/(x(i)-x(j)),1/(x(i)-x(j))]
  82.                 Li=PP(li,Li)
  83.             end
  84.         end
  85.         Lagrange=Sum(Lagrange,PP(y(i),Li))
  86.     end
  87. endfunction
  88. function DD = DivDiff(x,y,n)
  89.         for i =1:length(x)
  90.             DD(1,i) = x(i)
  91.             DD(2,i) = y(i)
  92.         end
  93.         for i=3:n+2
  94.             for j=1:length(x)+2-i
  95.                 DD(i,j) = (DD(i-1,j) - DD(i-1,j+1))/(DD(j) - DD(j+i-2))
  96.             end
  97.         end
  98. endfunction
  99.  
  100. function Newtoon = Newt(x, y)
  101.     N=1
  102.     m= DivDiff(x,y,length(x))
  103.     Newtoon=(1)
  104.     for i=1:length(x)-1
  105.         Ni=[-x(i),1]
  106.         N=PP(N,Ni)
  107.         Newtoon=PP(Newtoon,PP(N,m(2+i,1)))
  108.     end
  109. endfunction
  110. function Error = Err(func)
  111.     Error=abs(func-exp(sin(_nodes)))
  112. endfunction
  113. function mydeg = Mydeg (point, n)
  114.     mydeg=1
  115.     for i=1:n
  116.         mydeg=mydeg*point/(2^(256/n))
  117.     end
  118.     mydeg=mydeg*2^(256)
  119. endfunction
  120. function myhorn = mhorner(pol, _nodes)
  121.     for k=1:length(_nodes)
  122.         myhorn(k)=1
  123.     end
  124.     for j=1:length(_nodes)
  125.         myhorn(j)=0
  126.         for i=1:n
  127.             myhorn(j)=myhorn(j)+pol(i+1)*Mydeg(_nodes(j), i)
  128.             //disp(myhorn(j))
  129.         end
  130.     end
  131. endfunction
  132. Newtoon=Newt(x,my_y)
  133. Newtoonch=Newt(xch,my_ych)
  134. PolLag=Lag(x, my_y)
  135. //disp(PolLag)
  136. PolLagCh=Lag(xch, my_ych)
  137. _nodes=[from:1/1000:to]
  138. value_calc = mhorner(PolLag,_nodes)
  139. value_calc_R=abs(Diff(value_calc,exp(sin(_nodes))))
  140. //plot(_nodes,value_calc_R, "red")
  141. value_calcCh = mhorner(PolLagCh,_nodes)
  142. value_calcN = mhorner(Newtoon,_nodes)
  143. value_calcNch = mhorner(Newtoonch,_nodes)
  144. plot(_nodes,value_calc, "red")
  145. //plot(_nodes,value_calcCh, "green")
  146. plot(_nodes,exp(sin(_nodes)), "blue")
  147. //plot(_nodes,value_calcN, "red")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement