tags:

views:

241

answers:

1

Hi all,

Say if i define the following:

g = @(x) x/sqrt(x^2+1)

How do i get the derivative function for g, which i can then use to evaluate at different points?

I tried the symbolic math toolkit, and tried the following:

>> syms x

>> f = x/sqrt(x^2+1)

f =

x/(x^2 + 1)^(1/2)

>> diff(f)

ans =

1/(x^2 + 1)^(1/2) - x^2/(x^2 + 1)^(3/2)

However, i cannot figure out how to turn this into a function handle/evaluate at different points. However, i prefer doing differentiation on function_handle.

Thank you very much!

Jason

+2  A: 

You can use matlabFunction to convert a symbolic equation to a function. For example:

syms x1 x2;
f1 = x1^2+x2^2;
Df1 = jacobian(f1, [x1 x2]);
Df1 = matlabFunction(Df1);

Then Df1(0, 0) returns [0 0] as expected.

The function matlabFunction was introduced in version 5.2 (R2009a) of the Symbolic Math Toolbox.

jmbr
@jmbr: Since our answers were so similar, I just deleted mine and added its contents to yours.
gnovice
Excellent. Thank you, gnovice.
jmbr

related questions