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

2 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Mischa Kim
Mischa Kim el 13 de Feb. de 2014
Editada: Mischa Kim el 13 de Feb. de 2014
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.

Más respuestas (1)

Roger Stafford
Roger Stafford el 13 de Feb. de 2014
Editada: Roger Stafford el 13 de Feb. de 2014
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 comentarios
Hana
Hana el 13 de Feb. de 2014
Editada: Hana el 13 de Feb. de 2014
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 el 13 de Feb. de 2014
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.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by