I have to create a function m file called 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.
This is my code:
function f = myfirstzero(f,a);
syms x
n = 0;
d=abs(subs(f(x),a));
while(d >0); d=subs(diff(f(x),n),a); n=n+1; end f=n; end
Here is the sample data I have been using along with the correct answer.
myfirstzero(@(x) 2*x^3-3*x^2-12*x+6,2)
ans = 1
myfirstzero(@(x) x^3,0) ans=0
myfirstzero(@(x) x^3+2,0)
ans = 1
myfirstzero(@(x) x^6-5*x^5-2*x^4-x^3+x^2-x+10,3)
ans = 7
My code is not providing the correct answers for all of these, these are the answers my code is getting respectfully, 1,0,2,1. If someone could please let me know what is wrong with my code I would greatly appreciate it.
Thanks
Best Answer