FEVAL error in implementing Newton's method.

This is M-File 1
function sol=newtonian(fn,dfn,x0,tol)
tol=0.000001;
old=x0+1;
while abs (x0 - old) > tol; old=x0;
x0=old - feval(fn, old)/feval(dfn, old);
end;
sol=x0;
end
M-File 2
function y=fn(x0,y)
x0=2;
y=(x0).^2-exp(x0)+2;
end
M-File 3
function dy=dfn(x0)
x0=-2;
dy=2*(x0)-exp(x0);
end
But I keep getting this ERROR:
??? Error using ==> feval
Argument must contain a string or function_handle.
Error in ==> newtonian at 5
x0=old - feval(fn, old)/feval(dfn, old);
What does this error mean?

1 件のコメント

Paulo Silva
Paulo Silva 2011 年 3 月 2 日
??? Error using ==> feval
Argument must contain a string or function_handle.
means that you are not providing the arguments that the function is expecting, maybe feval(@fn, old) or feval('fn', old) ?

サインインしてコメントする。

 採用された回答

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2011 年 3 月 2 日

0 投票

fn(old)
dfn(old)
Are what you want. Not feval.

9 件のコメント

Ashley Dunn
Ashley Dunn 2011 年 3 月 2 日
ok now it's saying my FN file is messed up ....like the
??? Input argument "x0" is undefined.
Error in ==> fn at 2
y=(x0).^2-exp(x0)+2;
Sean de Wolski
Sean de Wolski 2011 年 3 月 2 日
function y = fn(x0)
y=(x0).^2-exp(x0)+2;
end
or
fn = @(x0)x0.^2-exp(x0)+2;
Paulo Silva
Paulo Silva 2011 年 3 月 2 日
There's also the inline function that works great
fn=inline('x0.^2-exp(x0)+2');
Ashley Dunn
Ashley Dunn 2011 年 3 月 2 日
Ok redid using what you said
>> x0=-2
x0 =
-2
>> tol=.01
tol =
0.0100
>> sol=newtonian(fn,dfn,x0,tol)
??? Error using ==> dfn
Too many output arguments.
And here are my modified m-files
:
function dfn(x0)
dfn=2*(x0)-exp(x0);
end
function fn(x0)
fn = @(x0)x0.^2-exp(x0)+2;
end
function sol=newtonian(fn,dfn,x0,tol)
old=x0+1;
while abs (x0 - old) > tol; old=x0;
x0=old - (fn(old))/(dfn(old));
end; sol=x0;
end
Ashley Dunn
Ashley Dunn 2011 年 3 月 2 日
what is inline
Ashley Dunn
Ashley Dunn 2011 年 3 月 2 日
I did inline for both dfn and fn
>> sol=newtonian(fn,dfn,x0,tol)
??? Error using ==> dfn
Too many output arguments.
Paulo Silva
Paulo Silva 2011 年 3 月 2 日
doc inline
Ashley Dunn
Ashley Dunn 2011 年 3 月 2 日
what does doc inline mean?
Paulo Silva
Paulo Silva 2011 年 3 月 2 日
write doc inline in your matlab command line and see, same goes to other functions. You can also do:
help inline

サインインしてコメントする。

Sean de Wolski
Sean de Wolski 2011 年 3 月 2 日

0 投票

Ashley, It would be worth your time to review the syntax and methodology covered in these two documents:
doc function
doc function_handle
Matt Tearle
Matt Tearle 2011 年 3 月 2 日

0 投票

The accepted answer from Paulo looks like a great resource for the theory behind gradient methods, but I'm not sure if that's actually solved your MATLAB issues or not, so...
The issue is that you need to give the names of two functions to newtonian (the first two arguments, fn and dfn). Values like x0 and tol are easy to pass to a function as either literal values or variables that hold numeric values. For example
sqrt(pi)
or
x = pi;
sqrt(x)
But how do you pass a reference to (or name of) a function as an input to another function? The answer in MATLAB is to use a function handle. If you have a function file (like, say, fn.m) you can create a function handle to it using the @ symbol: @fn
Then, inside newtonian the variable fn will be a function handle to the function file fn.m. You can then use it as if it was a direct use of the fn function, as in Sean's answer: fn(old).
(Sorry, I realize it's a bit confusing, with the same name being used for everything.)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by