kaburen

lagrange

May 8th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.43 KB | None | 0 0
  1. %A = [-2 -1 0 2]
  2. %B = [0 1 1 2]
  3. function [g] = Lagrang(X,Y)
  4. lx = length(X);
  5. ly = length(Y);
  6. if(lx ~= ly)
  7.     disp('Podaj tablice o takich samych wymiarach')
  8.     return
  9. end
  10. g = 0;
  11. syms x;
  12. for j = 1: lx
  13. v = 1;
  14.  
  15. for i = 1 : lx
  16.     if(i~=j)
  17.     v = v*(x-X(i))/(X(j)-X(i));
  18.     end
  19. end  
  20. v=v*Y(j);
  21. g = g+v;
  22. end
  23. g = simplify(g);
  24. z = X(1):0.1:X(lx)+1;
  25. f = @(x)eval(g);
  26. hold on
  27. grid on;
  28. plot(z,f(z),X,Y,'v')
  29. hold off
Advertisement
Add Comment
Please, Sign In to add comment