How do I find a solution to (x.^2)/2-10*atan(x) using the Newton-Raphson method?

조회 수: 6 (최근 30일)
How do I find a solution to (x.^2)/2-10*atan(x) using the Newton-Raphson method? Is there a general 'body' to this method?

채택된 답변

Mischa Kim
Mischa Kim 2014년 2월 13일
편집: Mischa Kim 2014년 2월 13일
Hana, there are a couple of issues with your code. This should work:
function Xs = newtonroot(Xest)
imax = 100;
Err = 1e-10;
for i = 1:imax
Xi = Xest - (Xest^2/2-10*atan(Xest))/(Xest-10/(1+Xest^2));
if (abs(Xi - Xest) < Err)
Xs = Xi;
break
end
Xest = Xi;
end
if i == imax
fprintf('Solution was not obtained within %i iterations.\n',imax)
Xs = ('No answer');
end
end
  • In the function definition you need to return Xs (rather than x), don't forget the equal sign.
  • The loop does not get executed. First, I do not see imax defined (globally?). Second, even if it is defined i is set to imax.
  • Lastly, you probably will converge faster (and more consistenly) with abs((Xi - Xest) < Err). Just think of a scenario, where the root is close to zero, the error terms will blow up.
  • Of course, you can also pass alogrithm parameters to the function rahter than define them locally.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2014년 2월 13일
편집: Roger Stafford 2014년 2월 13일
See the Wikipedia article at
http://en.wikipedia.org/wiki/Newton's_method
If you plot a graph of your function f(x) = x^2/2-10*atan(x), you will see that it has two solutions, one exactly at x = 0 and one somewhere in the vicinity of x = 5. For Newton's method to work, you need an initial estimate to start the iteration going. In this case since you are undoubtedly seeking the root near x = 5, you should start x at that crossing place you see in the plot. You will also need the derivative of the above function which you can easily figure out from your classes in calculus. Then use a while-loop to do your iteration as explained in the Wikipedia article and stop when you see that the correction being made is sufficiently small in absolute value to give you an accurate answer.
  댓글 수: 2
Hana
Hana 2014년 2월 13일
편집: Hana 2014년 2월 13일
function x newtonroot(Xest,f,fd)
for i = imax
Xi = Xest - f(Xest)/fd(Xest);
if abs((Xi - Xest)/Xest) < Err
Xs = Xi;
break
end
Xest = Xi;
end
if i == imax
fprintf('Solution was not obtained within %i iterations.\n',imax)
Xs = ('No answer');
end
This is what I wrote. The function and it's derivative is in a separate function file. However Matlab won't let me execute the above code as a function file. I'm confused. I can't possibly execute a script either right? Please help.
Roger Stafford
Roger Stafford 2014년 2월 13일
I would recommend that first you express your function directly in the code to see if the code is good. (It looks valid to me on a quick glance.)
Xi = Xest - (Xest^2/2-10*atan(Xest))/(Xest-10/(1+Xest^2));
When you get that working, try to use names for your functions that are more likely to be unique, rather than 'f' and 'fd'. It is too easy for you to have accidentally used these for some other purpose. See if that doesn't work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by