MATLAB: How to fix this error with function handle

functionhandle

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

Best Answer

while (d > 0) && (d <= 6)