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

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

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개)

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일
편집: bdlawr 2017년 11월 9일
wait fh is function that i define as x^3 + 2x^2 - 10x. i need to plug in initialGuess into fh to test whether the while loop condition is true. thats why i used an anonymous fcn or am i using it incorrectly. there should be an image of the instruction under 'latest activity'. thank you so much for helping me
No, fh is a handle to a function. It says so right in your instructions.
To invoke a function handle, you name the function handle, then ( then the list of parameters then ) -- just like the fh(x) that I showed.
it now says
"Index exceeds matrix dimensions.
Error in myNewton (line 5) a = fh(x);"
but the autograder does give me 4 extra points
Please post your current code.
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
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
?

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

카테고리

도움말 센터File Exchange에서 Big Data Processing에 대해 자세히 알아보기

질문:

2017년 11월 9일

댓글:

2017년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by