Newton's Method for root approximation using multiple initial values

5 views (last 30 days)
Kevin Osborn
Kevin Osborn on 1 Oct 2021
Edited: Matt J on 2 Oct 2021
I have conducted Netwon's method for root approximation and I have a follow on question. On the interval (0,8), I want to see for what starting values of x0 will the method converge within 50 iterations. The hard way to do this would just be to copy and paste the orignal code over and over again. For example, the first block would be:
f = @(x) 2*exp(-2*x) + 4*sin(x) - 2*cos(2*x);
fp = @(x) 4*(-exp(-2*x) + sin(2*x) + cos(x));
x0 = 0;
N = 50;
tol = 1e-6;
x(1) = x0;
n = 2;
nfinal = N + 1;
while (n <= N + 1)
fe = f(x(n - 1));
fpe = fp(x(n - 1));
x(n) = x(n - 1) - fe/fpe;
if (abs(fe) <= tol)
nfinal = n;
break;
end
n = n + 1;
end
And the second run through would be exactly the same code except changing x0 to 0.1, and then to 0.2, and then to 0.3, etc. I would then have to manually go and check all 81 iterations of the code from 0 to 8 to see which ones converged and which ones, if any, didn't.
Is there a way to just change the x0 value somehow and put everything in a for loop so that I only have to actually have one block of code to do this?
Also, for the single run through case, I have the following table and graph:
figure, plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Newton''s Method')
xlabel('Iterations')
ylabel('Root Approximation')
table([1:length(x)]', x', 'VariableNames', {'Iteration' 'Root Approximation'})
x(n) = x(n - 1) - fe/fpe;
fprintf('Iteraion Root Approximation Tolerance Level\n')
fprintf('%3d: %20g %20g\n', n, x(n), abs(fe));
What would I have to change in the table to make every single one of these iterations of x0 values and their root approximation values come up in one big table?
Thank you for the help.

Answers (1)

Matt J
Matt J on 2 Oct 2021
Edited: Matt J on 2 Oct 2021
Is there a way to just change the x0 value somehow and put everything in a for loop so that I only have to actually have one block of code to do this?
Sure, just put the code in a function and call it in a loop.
x0=linspace(0,8);
for i=1:numel(x0)
[n(i),fe(i)]=runit(x0(i)); %add pre-allocation for n and fe
end
function [n,fe]=runit(x0)
f = @(x) 2*exp(-2*x) + 4*sin(x) - 2*cos(2*x);
fp = @(x) 4*(-exp(-2*x) + sin(2*x) + cos(x));
x0 = 0;
N = 50;
tol = 1e-6;
x(1) = x0;
n = 2;
nfinal = N + 1;
while (n <= N + 1)
fe = f(x(n - 1));
fpe = fp(x(n - 1));
x(n) = x(n - 1) - fe/fpe;
if (abs(fe) <= tol)
nfinal = n;
break;
end
n = n + 1;
end
end
What would I have to change in the table to make every single one of these iterations of x0 values and their root approximation values come up in one big table?
That doesn't seem advisable. But with the output collected above you could easily make a table of the final fe and n for each x0;
T=table(x0(:),n(:),fe(:),'VariableNames',{'x0', 'Iterations', 'Root Approximation'})
  6 Comments
Matt J
Matt J on 2 Oct 2021
Again it is not clear to me where you put runit. Do you have all your code in the same file, or did you put runit in its own separate file? If the latter, runit.m needs to be be somewhere that Matlab can see it, i.e., either in your current folder or somewhere on the Matlab path.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!