Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %Polinomio de interpolaci�n de Newton... 533
- format short
- formula_interpolacion_lineal = 'f(x0) + ((f(x1) - f(x0)) * x - x(0))/(x1 - x0)';
- formula_interpolacion_cuadratica = 'b0 + b1(x-x0)+b2(x-x0)(x-x1)';
- n = 4;
- evaluado_en = 2;
- fx = 'log(x)';
- func = str2func(['@(x) ' vectorize(fx)]);
- puntos = [1 4 6 5];
- evals = zeros(1, n - 1);
- for c=1:n
- evals(c) = func(puntos(c));
- end
- N=length(puntos)-1;
- M=NaN(N+1,N+2);
- M(:,1)=puntos;
- M(:,2)=evals;
- for i=2:N+1
- for j=i:N+1
- M(j,i+1)=(M(j,i) - M(j-1,i)) / (M(j,1) - M(j-i+1,1) );
- end
- end
- % Creamos un bucle para completar hasta el polinomio de n-�simo grado
- vals = '';
- for c=0:n-1
- other = '';
- for j=0:c-1
- other = strcat(other, '*(x-x', string(j), ')');
- end
- vals = strcat(vals, 'b', string(c), other);
- if c ~= n-1
- vals = strcat(vals, '+');
- end
- end
- %Remplazamos los valores calculados en la funcion final.
- for i=1:n
- for j=1:n+1
- vals = strrep(vals, strcat('b',string(i-1)), string(M(i,i+1)));
- vals = strrep(vals, strcat('x',string(i-1)), string(puntos(i)));
- end
- vals = strrep(vals, '+-', '-');
- end
- func_final = str2func(strcat("@(x)", vals));
- disp(strcat('La ecuaci�n de grado ', " ", string(n), " es: ", vals));
- val_real = func(2);
- val_aprox = func_final(2);
- disp(strcat("El error relativo es: ", string((val_real-val_aprox)*100/val_real), "%"));
- fplot(matlabFunction(str2sym(fx)), 'r--o');
- hold on
- fplot(matlabFunction(str2sym(vals)), [0,n], 'b--*');
- hold off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement