30 views (last 30 days)

Show older comments

I am trying to perform Newton's method over a series of values given by the array p_init.

I am trying to perform Newton's method on the jth value, check if Newton's method was convergent within 10 iterations (indexed by i), then move onto the (j+1)th value. The Newton's method code itself works for one value, but it does not seem to be properly looping and initializing over the j values in my array. Where is my problem, or how can I properly accomplish this task? Thanks!

f = @(x) x^3 - x^2 - 6*x;

df = @(x) 3*x^2 - 2*x - 6;

p_init = -3:.1:4;

tol = 1e-12; n_iter = 10;

i = 1;

converge = false;

p0 = p_init(1);

for j = 1:length(p_init)

while i<=n_iter

p = p0-f(p0)/df(p0);

disp(p);

error = abs(p-p0);

if (error< tol)

converge = true; break

else

i = i+1;

p0 = p;

end

end

p0 = p_init(j);

% Here, I want to check convergence of my jth value

end

Jim Riggs
on 9 Feb 2020

Yes, your loop is not quite right.

Notice your placement of the three lines

i=1;

converge = false;

po = p_init(1);

These three lines are brfore the loops, and thus, none of these three parameters ever gets reset.

This is why your loop does not work right.

After solving the first value, p0 never gets set to the next value, and i never gets reset to 1 for the next itteration.

Also, after the loop converges on an answer, you do not display or save the answer, so you won't know what it is.

f = @(x) x^3 - x^2 - 6*x;

df = @(x) 3*x^2 - 2*x - 6;

p_init = -3:.1:4;

tol = 1e-12;

n_iter = 10; % it's not a good ides to put more than one command on one line.

% i = 1; Move this inzide the "for" loop

% converge = false; This is not used for anything, so it is unneeded.

% if you want to use "converge" it needs to move inside the "while" loop too

% p0 = p_init(1); Move this inside the "for" loop

% create an array to save the solution data. 3 parameters will be saved

Sol = nan(3,numel(p_init));

for j = 1:length(p_init)

i=1; % initialize the "while" loop counter

p0=p_init(j); % set the initial value of p0

Sol(1,j) = p_init(j); % save the initial guess in "Sol"

while i<=n_iter

p = p0-f(p0)/df(p0);

disp(p);

error = abs(p-p0);

if (error< tol)

% converge = true; % this is never used.

Sol(2,j) = p0; % save the solution in

Sol(3,j) = i; % save the number of itterations

break % it's not a good ides to put more than one command on one line.

else

i = i+1;

p0 = p;

end

end

%p0 = p_init(j);

% Here, I want to check convergence of my jth value

end

At the end of this "while" loop, you have matrix "sol";

Sol(1,:) is the initial guess value

Sol(2,:) is the final solution value

Sol(3,:) is the number of itterations used to get the solution.

Notice that Sol(1,j) (the initial guess) is saved before entering the "for" loop. If the loop ends without finding a solution, them Sol(2,j) and Sol(3,j) wil be "nan", so you will know that convergence was not achieved for this guess within the itteration limit (n_iter).

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

Start Hunting!