How to compute a derivative of a function and to solve it with f'(x)=0 !

27 views (last 30 days)
Hi there!
I'm working on a simple project in order to learn how to correctly use Matlab since a few days and I've a little problem with an optimisation process:
I've this function, very simple one: f(x)= x² -2x -3 I would like to compute the derivative of this function, therefore I use the function "diff" of Matlab in this way:
f =x^2 - 2*x - 3
diff(f)
f=
2*x - 2
then I'd like to set this derivative equal to 0 in order to find x:
f(x) = 2*x - 2 = 0 <=> x = 1
But I wish to do it automatically, without typing the computed derivative in Matlab everytime. I actually tried several times with fzero but it never worked...
f =x^2 - 2*x - 3
f2 = diff(f)
fzero(f2,0)
??? Undefined function or method 'isfinite' for input arguments of
type 'sym'.
Error in ==> fzero at 317
elseif ~isfinite(fx) || ~isreal(fx)
Do you have any idea how can I write a mat file (or function file I can't remember the expression again ;)) in order to compute derivative and this derivative equal to 0 automatically? I hope this is understable, sorry for my mistakes.
yours Aurélien

Answers (1)

Walter Roberson
Walter Roberson on 11 May 2012
solve(f2,x)
Or alternately,
f3 = matlabFunction(f2);
fzero(f3, 0)
fzero() takes a function handle. A symbolic expression is not a function handle.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!