Performing calculations (Newton's method) over values in loop
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
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!