Plotting approximation error in Newton-Raphson
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!