How do I fix this error with function handle?
4 views (last 30 days)
Show older comments
Hello,
I've been trying to work through this problem:
Write function myfirstzero(f,a) which takes two inputs:
f: A function handle which you may assume will represent a polynomial.
a: A real number.
Does: Uses a while loop to find the smallest n such f(n)(a) = 0. Note that this means the nth derivative at x = a and note that n = 0 is fair game, where the 0th derivative of a function is just the function itself.
Returns: This value of n.
And my attempt is:
function myfirstzero(f,a)
syms x
n = 0;
d = subs(f(x), a)
while d > 0
d = diff(f(x))
n = n + 1
if d ~= 0
f = matlabFunction(d)
d = f(a)
end
end
n + 1
However, when i put in the input like myfirstzero(@(x) x^3, 2), the function will run fine until it works to d = 6, which I want the function to stop, but it keeps running because d ~= 0, then it errors.
How do I fix this?
Thank you so much, Sea
1 Comment
Accepted Answer
Star Strider
on 11 Sep 2014
Edited: Star Strider
on 11 Sep 2014
while (d > 0) && (d <= 6)
6 Comments
Star Strider
on 12 Sep 2014
Edited: Star Strider
on 12 Sep 2014
My pleasure!
( The sincerest expression of appreciation here MATLAB Answers is to Accept the Answer that most closely solves your problem. )
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!