Files
equation-solver/newtonr.m
T
2022-05-30 15:25:03 +02:00

55 lines
1.1 KiB
Matlab
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function [x1, tab] = newtonr (f, x0, tol, max, time)
% default arguments
if nargin < 3
tol = 1e-4;
max = 20;
time = 1;
end
tab = (max);
y = sym(f);
fp = matlabFunction(diff(y));
x = linspace(x0-10, x0+10, 1000);
figure
set(gca,'DataAspectRatio',[1 1 1])
plot(x, f(x), "LineWidth",2)
axis([x0-10 x0+10 -50 50])
title("Newton-Raphson Method")
xlabel("𝑥")
ylabel("f(𝑥)")
grid on
hold on
shg
for i = 1:max
% add element to the table
tab(i) = x0;
h1 = plot(x0, f(x0), 'go', 'MarkerFaceColor', 'g', 'MarkerSize', 6, 'LineWidth',2);
text(x0, f(x0)+0.5, " 𝑥_{"+ i + "}")
pause(time)
delete(h1)
plot(x0, f(x0), 'bo', 'MarkerFaceColor', 'b', 'MarkerSize', 6, 'LineWidth',2)
if fp(x0) == 0
close all
errordlg('x1 is local min/max','Error')
return
end
x1 = x0 - f(x0)/fp(x0);
h2 = plot(x, (-f(x0)).*(x-x0)/(x1-x0) + f(x0), '--k', 'LineWidth', 1);
pause(time)
delete(h2)
if abs(x1 - x0) < tol
return
else
x0 = x1;
end
end
end