How do I apply Newton's method to this problem?

I need to write a MATLAB program that uses the Newton's Method to find the root of this function:
x^2-e^2-2=0
Xo=-2
Root has to be accurate to the 10^-6
I know how to do the Newton's method. The MATLAB file requires three files: function, function derivative, and newton function. It also requires using the FEVAL method to find the solution to the function derivative and function.

5 comentarios

Sean de Wolski
Sean de Wolski el 2 de Mzo. de 2011
It won't require feval if you do it properly...
Anyway, post what you've done so far because no one here will do your homework for you.
Ashley Dunn
Ashley Dunn el 2 de Mzo. de 2011
1st function file
function fun(x)
x^2-exp^x+2; %#ok<VUNUS>
end
2nd function file
function functionderivative(x)
2*x-exp(x);
end
3rd function file
function result = newtroot (fun, functionderivative, x)
delta = tol = sqrt (eps);
maxit = 50;
fx = feval (fun, x);
deriv=feval(functionderivative, x);
for i = 1:maxit
if (abs (fx) < tol)
result = x;
return;
else
fx_new = feval (fun, x + delta);
x = x - fx / deriv;
fx = fx_new;
endif
endfor
***I want to use feval...that was the objective of the problem...the error says that there are too many output arguments for the "fun" function.
Paulo Silva
Paulo Silva el 2 de Mzo. de 2011
function [y]=fun(x)
y=x^2-exp^x+2;
end
Sean de Wolski
Sean de Wolski el 2 de Mzo. de 2011
or
fun = @(x)x^2-exp^x+2;
Anyway, no reason for feval, tell your teacher/professor to use function handles!
Ashley Dunn
Ashley Dunn el 2 de Mzo. de 2011
Lol....I know how to input a function into an m-file....how am I supposed to use feval to Newtonian method up the original function??

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 3 de Mzo. de 2011

0 votos

"Lol....I know how to input a function into an m-file" ... The "fun" function you constructed has no return value defined, which is what the error message was complaining about. Paulo shows how to define it with an output argument. Do likewise for your functionderivative function.
Now, when you invoke your newtroot, be sure to pass @fun or 'fun' as the first argument rather than fun itself without any adornment. Likewise, in newtroot, do not feval(functionderivative,x) and instead feval(@functionderivative,x) or feval('functionderivative',x) . What you have now will evaluate functionderivative with no arguments, and expects that function to return a value that is the name string or function handle to be evaluated.

Categorías

Más información sobre Debugging and Analysis en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Mzo. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by