Newton's Method returns complex value.
이전 댓글 표시
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
Is it possible your f and df are returning imaginary values? Try printing them out.
Remember with this algorithm it is possible to have "weird" values of x (negative, 0, etc), so you may also want to take a look at the xnew values it has printed out.
Functions like abs() or real() can hide the fact that you are getting imaginary results, but there are drawbacks in terms of linearity if you choose to do this.
David
2013년 10월 31일
Walter Roberson
2013년 10월 31일
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.
답변 (2개)
A Jenkins
2013년 10월 31일
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
David
2013년 10월 31일
Walter Roberson
2013년 10월 31일
What are you passing in for f and df ?
David
2013년 10월 31일
Walter Roberson
2013년 11월 1일
LaTeX is not directly supported here. There is at least one site that allows you to paste in a LateX expression, and it gives you back a URL that when viewed gives a rendered version of it. Other than that, you can take a snapshot and post that.
With Newton's Method, as soon as f(x) or df(x) returns a complex value, all subsequent calculations will also return complex values (rather it is very very likely.) So we would need to examine f(x) and df(x) in order to determine wear the complex values are coming from.
Per your question about how to use the toolboxes (assuming you have them):
Intead of calling:
xnew = Newton (f, df, xi, tol)
Try calling
xnew = fminsearch(f, xi, optimset('Display','iter'))
(f is the handle to your function, xi is the initial condition, display makes it print the iterations)
Or, if you like GUIs, type
optimtool
(You'll see there are lots of options. Sometimes the defaults work, but if not, then you have to start digging into your function "f" to understand a suitable way to set it up.)
David
2013년 11월 1일
David
2013년 11월 1일
David
2013년 11월 3일
Walter Roberson
2013년 11월 3일
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)
카테고리
도움말 센터 및 File Exchange에서 Surface Style에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!