Azzera filtri
Azzera filtri

Newton's Method returns complex value.

12 visualizzazioni (ultimi 30 giorni)
David
David il 30 Ott 2013
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 Commenti
David
David il 31 Ott 2013
Yeah, so f and df are both printing out complex values once the initial guess is about 45 and above(remember the actual value of the root is about 13.1) and once the initial guess is about 1000, NaN is returned as the root. This is due to f being Inf + some tiny imaginary number and df being -Inf + some tiny imaginary number. Thus, f/fd will return NaN. First of all, is this due to a problem with my algorithm or is this just a shortcoming of Newton's Method? And second, how can I safeguard against getting imaginary values and values that are too large(i.e. Inf) for f and df? Like, is there a way to check before Newton's Method is carried out to see if the initial guess is valid? Thanks.
Walter Roberson
Walter Roberson il 31 Ott 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.

Accedi per commentare.

Risposte (2)

A Jenkins
A Jenkins il 31 Ott 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 Commenti
David
David il 3 Nov 2013
Any luck Walter Robertson?
Walter Roberson
Walter Roberson il 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)

Accedi per commentare.


David
David il 1 Nov 2013
@A Jenkins. Thanks for that. I'll have a look into that now.

Community Treasure Hunt

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

Start Hunting!

Translated by