Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you do symbolic differentiation on function handle?

Tags:

matlab

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

like image 240
Lebron James Avatar asked Feb 21 '10 22:02

Lebron James


People also ask

Can you differentiate a function handle in MATLAB?

The short answer is "No." MATLAB has no idea what the contents of the function_handle mean in a symbolic sense. You're better off creating it using syms in first place. A longer answer would be either to use the Symbolic Math Toolbox, as suggested by @A Danesh, or an approximation, as suggested by @Andrey.

How do you convert symbolic to function handle MATLAB?

ht = matlabFunction( f ) converts the symbolic expression or function f to a MATLAB® function with handle ht . If there is an equivalent MATLAB function operating on the double data type for the symbolic expression or function, then the converted function can be used without Symbolic Math Toolbox™.

Which function is used to get symbolic differentiation?

Df = diff( f , var ) differentiates f with respect to the differentiation parameter var . var can be a symbolic scalar variable, such as x , a symbolic function, such as f(x) , or a derivative function, such as diff(f(t),t) .

How do you express a function symbolically?

The notation y=f(x) defines a function named f. This is read as “y is a function of x.” The letter x represents the input value, or independent variable. The letter y, or f(x), represents the output value, or dependent variable.


1 Answers

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.

like image 102
jmbr Avatar answered Oct 27 '22 07:10

jmbr