Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n=19
- from=0
- to=6
- x=[from:(to-from)/n:to]
- xch=[0]
- for i=1:n+1
- xch(i)=((to+from)/2)+((to-from)/2)*cos(((2*i-1)*%pi)/(2*(n+1)))
- end
- f=exp(sin(x))
- my_y=[0]
- for i=1:length(x)
- my_y(i)=exp(sin(x(i)))
- end
- my_ych=[0]
- for i=1:length(xch)
- my_ych(i)=exp(sin(xch(i)))
- end
- function P = PP(a, b)
- for i=1:length(a)
- for j=1:length(b)
- P(i+j-1)=0
- end
- end
- for i=1:length(a)
- for j=1:length(b)
- P(i+j-1)=P(i+j-1)+a(i)*b(j)
- end
- end
- endfunction
- function sump = Sum(a, b)
- m=abs(length(a)-length(b))
- for i=1:length(a)
- for j=1:length(b)
- if i==j then
- sump(i)=a(i)+b(j)
- end
- end
- end
- k=length(sump)
- if m~=0 then
- for s=(k+1):(k+m)
- if length(a)>length(b) then
- sump(s)=a(s)
- end
- if length(b)>length(a) then
- sump(s)=b(s)
- end
- end
- end
- endfunction
- function difp = Diff(a, b)
- m=abs(length(a)-length(b))
- for i=1:length(a)
- for j=1:length(b)
- if i==j then
- difp(i)=a(i)-b(j)
- end
- end
- end
- k=length(difp)
- if m~=0 then
- for s=(k+1):(k+m)
- if length(a)>length(b) then
- difp(s)=a(s)
- end
- if length(b)>length(a) then
- difp(s)=-b(s)
- end
- end
- end
- endfunction
- function Lagrange = Lag(x, y)
- Lagrange=1
- for i=1:length(x)
- Li=1
- for j=1:length(x)
- if i~=j then
- li=[-x(j)/(x(i)-x(j)),1/(x(i)-x(j))]
- Li=PP(li,Li)
- end
- end
- Lagrange=Sum(Lagrange,PP(y(i),Li))
- end
- endfunction
- function DD = DivDiff(x,y,n)
- for i =1:length(x)
- DD(1,i) = x(i)
- DD(2,i) = y(i)
- end
- for i=3:n+2
- for j=1:length(x)+2-i
- DD(i,j) = (DD(i-1,j) - DD(i-1,j+1))/(DD(j) - DD(j+i-2))
- end
- end
- endfunction
- function Newtoon = Newt(x, y)
- N=1
- m= DivDiff(x,y,length(x))
- Newtoon=(1)
- for i=1:length(x)-1
- Ni=[-x(i),1]
- N=PP(N,Ni)
- Newtoon=PP(Newtoon,PP(N,m(2+i,1)))
- end
- endfunction
- function Error = Err(func)
- Error=abs(func-exp(sin(_nodes)))
- endfunction
- function mydeg = Mydeg (point, n)
- mydeg=1
- for i=1:n
- mydeg=mydeg*point/(2^(256/n))
- end
- mydeg=mydeg*2^(256)
- endfunction
- function myhorn = mhorner(pol, _nodes)
- for k=1:length(_nodes)
- myhorn(k)=1
- end
- for j=1:length(_nodes)
- myhorn(j)=0
- for i=1:n
- myhorn(j)=myhorn(j)+pol(i+1)*Mydeg(_nodes(j), i)
- //disp(myhorn(j))
- end
- end
- endfunction
- Newtoon=Newt(x,my_y)
- Newtoonch=Newt(xch,my_ych)
- PolLag=Lag(x, my_y)
- //disp(PolLag)
- PolLagCh=Lag(xch, my_ych)
- _nodes=[from:1/1000:to]
- value_calc = mhorner(PolLag,_nodes)
- value_calc_R=abs(Diff(value_calc,exp(sin(_nodes))))
- //plot(_nodes,value_calc_R, "red")
- value_calcCh = mhorner(PolLagCh,_nodes)
- value_calcN = mhorner(Newtoon,_nodes)
- value_calcNch = mhorner(Newtoonch,_nodes)
- plot(_nodes,value_calc, "red")
- //plot(_nodes,value_calcCh, "green")
- plot(_nodes,exp(sin(_nodes)), "blue")
- //plot(_nodes,value_calcN, "red")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement