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
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.
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™.
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) .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With