Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 0.53 KB | None | 0 0
  1. function[P] = lagrange(X, Y)
  2.     n = length(X);
  3.     x = poly(0,"x");
  4.     P = 0;
  5.    
  6.     for i = 1:n, L=1;
  7.         for j = [1:i-1,i+1:n]
  8.             L = L*(x-X(j)) / (X(i)-X(j));
  9.         end
  10.  
  11.         P = P + L*Y(i);
  12.     end
  13. endfunction
  14.  
  15. clear;
  16. clc;
  17.  
  18. //x_in = input("Entre com o vetor x: ");
  19. //y_in = input("Entre com o vetor y: ");
  20. x_in = [0, 2, 4, 5];
  21. y_in = [0, 4, 16, 25];
  22. p = lagrange(x_in, y_in);
  23.  
  24. printf("Polinômio: ");
  25. disp(p);
  26.  
  27.  
  28. x = linspace(x_in(1), x_in($));
  29. y = linspace(y_in(1), y_in($));
  30.  
  31. plot(x, y);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement