Undefined function 'abs' for input arguments of type 'function_handle'

5 次查看(过去 30 天)
function [root,rootHistory, iter,errorValue]=myNewton(fh,dfh,initialGuess, tol , maxIter )
numLoop = 0;
rootHistory = [];
x = initialGuess;
a = @(x)fh;
while abs(a) >= tol
x1 = initialGuess - fh/dfh;
initialGuess = x1;
numLoop = numLoop + 1;
rootHistory = [rootHistory x1];
if numLoop == maxIter
break
end
end
root = x1;
iter = numLoop;
errorValue = abs(fh(root));
end
I don't know why it is giving me this error statement. There are also other syntax error in my code as well. Can someone help please? The instructions are in the PDF
  1 个评论
bdlawr
bdlawr 2017-11-9
when i take out the "abs" it gives me this ==>
Undefined operator '>=' for input arguments of type 'function_handle'.
perhaps there is something wrong with my function handle? Sorry new to matlab...

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2017-11-9
You need to change
a = @(x)fh;
to
a = fh(x);
You need to change
to
x1 = initialGuess - fh(SOMEINPUT)/dfh(SOMEINPUT);
You will need to figure out what is appropriate for SOMEINPUT.
You will also need to change a somewhere in the loop.
  6 个评论
bdlawr
bdlawr 2017-11-9
编辑:Walter Roberson 2017-11-15
function [root,rootHistory, iter,errorValue]=myNewton(fh,dfh,initialGuess, tol , maxIter )
numLoop = 0;
rootHistory = [];
x = initialGuess;
a = fh(x);
while a >= tol
x1 = initialGuess - fh(x)/dfh(x);
initialGuess = x1;
numLoop = numLoop + 1;
rootHistory = [rootHistory x1];
if numLoop == maxIter
break
end
end
root = x1;
iter = numLoop;
errorValue = abs(fh(root));
end
Walter Roberson
Walter Roberson 2017-11-15
In the first position, are you passing in
x.^3 + 2*x.^2 - 10*x
or are you passing in
@(x) x.^3 + 2*x.^2 - 10*x
?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 AVEVA PI Server Access 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by