MATLAB: How to get Derivative of an inserted function

derivative

I want user insert the function and the program get Derivative of it by using the given code but it wouldn't work
and show me this message :
Undefined function 'diff' for input arguments of type 'inline'.
Error in Untitled (line 8)
m=diff(f)
…………………….
my code :
clc
clear
str = input('Give an equation in x: ','s') ;
x = input('Type in a value of x: ') ;
f = inline(str,'x') ;
y = feval(f,x) ;
m=diff(f)
disp(['"' str '", for x = ' num2str(x) ', equals ' num2str(y)]) ;

Best Answer

str = input('Give an equation in x: ','s') ;
x = input('Type in a value of x: ') ;
f = str2sym(str);
y = subs(f, sym('x'), x);
m = diff(f);
disp(['"' str '", for x = ' num2str(x) ', equals ' char(y)]) ;
Notice that you take the derivative but you do not display the derivative or calculate anything with it. I suspect that what you want to do is evaluate the derivative at x, not evaluate the function at x.