Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.76 KB | None | 0 0
  1. Lagrange
  2. function lagrange
  3.     x=[-1 0 1];
  4.     y=[10 7 6];
  5.    
  6.     p=lagint1(x,y)
  7.     polyval(p,x)
  8.     p=lagint2(x,y)
  9.     polyval(p,x)
  10.    
  11. function p=lagint1(x,y)
  12.     p=zeros(1,length(x));
  13.     t=-1:0.1:1;
  14.     for i=1:length(x)
  15.         pi=deconv(poly(x),poly(x(i)));
  16.         Li=pi/polyval(pi,x(i));
  17.         plot(t,polyval(Li,t),t,y(i)*polyval(Li,t));
  18.         hold on;
  19.         p=p+y(i)*Li;
  20.  
  21.     end
  22.     plot(t,polyval(p,t));
  23.    
  24. function p=lagint2(x,y)
  25.     n=length(x);
  26.     X=repmat(x',1,n).^repmat((n-1):-1:0,n,1);   %wandermond mátrix
  27.     p=zeros(1,n);
  28.     for i=1:n;
  29.         temp=X;
  30.         temp(:,i)=y;
  31.         p(i)=det(temp)/det(X);  %wandermond determináns
  32.     end
  33.     hold off;
  34.     t=-1:0.1:1;
  35.     plot(t,polyval(p,t));
  36.     hold on;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement