Plotting approximation error in Newton-Raphson
Afficher commentaires plus anciens
Hello all,
In a script I'm trying to find roots of a function by Newton-Raphson method. In each iteration I'm finding and printing the relative approximation error.
What I want is, instead of printing them I would like plot my function and my relative approximation error in each iteration.
Any help is much appreciated.
My script file:
function [r] = newton(x1)
n = 1000;
tol_error = 0.001;
r = x1;
for t=1:n
x_plus = x1 - (my_func(x1)/my_func_diff(x1));
approx_error(t) = abs((x_plus - x1)/(x_plus))*100;
if (approx_error(t) < tol_error)
display(t);
display(approx_error(t));
r = x_plus;
return;
else
x1 = x_plus;
end
end
end
function y = my_func_diff(x)
y = 3*(x^2) - 20*x - 43;
end
function y = my_func(x)
y = x^3 - 10*(x^2) - 43*x + 52;
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur App Building dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!