Newton's Method returns complex value.
14 views (last 30 days)
Show older comments
So, I've written a program that carries out Newton's method. The root of the equation which I am trying to find is approximately 13.1. This is fine and my program returns the correct value when my initial guess is around this value (up to about x = 30 as my initial guess), however when I start using values such as 100+ it returns a complex root. The real part will be approximately 13.1 and the imaginary part will be VERY close to 0. Why is this and is there any way that I can fix/safeguard against this? Thanks.
function xnew = Newton (f, df, xi, tol)
xold = xi; %x(old) is assigned the value of the initial guess
xnew = xold - f(xold)/df(xold); %Implement newtons method to find x(new)
k = 0; %Assigns k(the counter) an initial value
fprintf('\nTable of Iteration No.(k) and Depth(h)\n')
fprintf('\nIteration No.\tDepth\n')
fprintf('%5u\t\t%2.6e\n',k,xi)
while ((abs(xnew - xold))/(abs(xnew)) > tol) %Running condition
if (k <= 100) %Max number of iterations
xold = xnew; %x(old) get's x(new)'s calculated value as per Newton Method's
xnew = xold - f(xold)/df(xold);
k = k + 1; %Increment k
else
warning('Maximum number of iterations has been reached')
break;
end
fprintf('%5u\t\t%2.6e\n',k,xnew)
end
3 Comments
Walter Roberson
on 31 Oct 2013
Imagine a function that looks nice and smooth and gives every indication that you can go ahead and extrapolate a value. But at some place a little before the location that would be extrapolated at, put in a discontinuity, or make the function go non-real. Newton's Method will not be able to deal with that function.
Answers (2)
A Jenkins
on 31 Oct 2013
If this is a school project requiring that you use Newton's method on some non-smooth function, than your professor probably is looking for you to notice this limitation of Newton's Method, as Walter described above.
MATLAB has lots of other built in functions that allow you to set bounds, or to try to solve functions that are non-differentiable, so if you are interested, you can take a look at those:
The regular Optimization Toolbox
fminbnd()
fminsearch()
or the Global Optimization Toolbox
9 Comments
Walter Roberson
on 3 Nov 2013
If h goes negative then P(h) could go negative and then P0/P(h) would be negative, and you would be raising that negative value to a power, which is going to give you a complex result.
You do not show the line invoking your Newton function. Keep in mind that your Newton function is not going to be restricted to invoking the function handles on h in the range you assign in your script (h = 0:25)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!