Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. %Polinomio de interpolaci�n de Newton... 533
  2. format short
  3.  
  4. formula_interpolacion_lineal = 'f(x0) + ((f(x1) - f(x0)) * x - x(0))/(x1 - x0)';
  5.  
  6. formula_interpolacion_cuadratica = 'b0 + b1(x-x0)+b2(x-x0)(x-x1)';
  7.  
  8. n = 4;
  9. evaluado_en = 2;
  10. fx = 'log(x)';
  11. func = str2func(['@(x) ' vectorize(fx)]);
  12. puntos = [1 4 6 5];
  13. evals = zeros(1, n - 1);
  14.  
  15. for c=1:n
  16. evals(c) = func(puntos(c));
  17. end
  18.  
  19. N=length(puntos)-1;
  20.  
  21. M=NaN(N+1,N+2);
  22. M(:,1)=puntos;
  23. M(:,2)=evals;
  24. for i=2:N+1
  25. for j=i:N+1
  26. M(j,i+1)=(M(j,i) - M(j-1,i)) / (M(j,1) - M(j-i+1,1) );
  27. end
  28. end
  29.  
  30. % Creamos un bucle para completar hasta el polinomio de n-�simo grado
  31. vals = '';
  32. for c=0:n-1
  33. other = '';
  34. for j=0:c-1
  35. other = strcat(other, '*(x-x', string(j), ')');
  36. end
  37. vals = strcat(vals, 'b', string(c), other);
  38. if c ~= n-1
  39. vals = strcat(vals, '+');
  40. end
  41. end
  42.  
  43. %Remplazamos los valores calculados en la funcion final.
  44. for i=1:n
  45. for j=1:n+1
  46. vals = strrep(vals, strcat('b',string(i-1)), string(M(i,i+1)));
  47. vals = strrep(vals, strcat('x',string(i-1)), string(puntos(i)));
  48. end
  49. vals = strrep(vals, '+-', '-');
  50. end
  51.  
  52. func_final = str2func(strcat("@(x)", vals));
  53.  
  54. disp(strcat('La ecuaci�n de grado ', " ", string(n), " es: ", vals));
  55.  
  56. val_real = func(2);
  57. val_aprox = func_final(2);
  58.  
  59. disp(strcat("El error relativo es: ", string((val_real-val_aprox)*100/val_real), "%"));
  60.  
  61. fplot(matlabFunction(str2sym(fx)), 'r--o');
  62. hold on
  63. fplot(matlabFunction(str2sym(vals)), [0,n], 'b--*');
  64. hold off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement