Get the derivative of a function_handle in MATLAB - matlab

Is it possible to get the derivative of a function_handle as a other function_handle?
Like:
fun1 = #(x) x^2;
% do that ...
disp(fun2);
#(x) x*2
I know how to find the derivative of a symbolic function but I can't convert a function_handle to a symbolic function.
I'm new to matlab and I couldn't find any way on doing that. Thanks in advance.

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.
However, if you're always working with polynomials, then you can store the coefficients as an array and use the following functions:
polyval to evaluate
conv to multiply
deconv to divide
polyder to differentiate
polyint to integrate

syms x
f = #(x) x^2 + 1;
diff(f,x)
Ans:
2*x + 1

You can get an approximation function:
delta = 0.0001;
d = #(x)( (fun1(x+delta) - fun1(x))./delta)

you can't analytically from a function handle.
but if you got the symbolic math toolbox you can derivate the symbolic function and create a function handle from the result.

Related

How can I solve an implicit equation without the symbolic toolbox?

I have an equation like this:
(5+x)^2/15+(x-4)^2/10=100
Can MATLAB solve this equation directly, without having access to the symbolic toolbox? If it can not do this, how can I resolve this problem?
It's possible, but requires some hand work.
Your function is a polynomial:
x^2/6 - (2*x)/15 + 49/15 = 100
When pulling the 100 to the left hand side, we can find the roots:
roots([1/6 -2/15 -1451/15])
ans =
24.4948
-23.6948
where the argument is specified as the prefactors in decreasing order of power.
Code with which I found the polynomial (requires the Symbolic Math toolbox):
syms x
fun = (5+x)^2/15+(x-4)^2/10-100;
f = simplify(fun);
How about using an anonymous function:
f=#(x)(5+x)^2/15+(x-4)^2/10-100;
X0=1; % initial guess
x_out=fzero(f,X0);

Extract coefficients from symbolic expression

Let
syms h
g=exp(h)+h*exp(h)+h^2
so, the coefficients of g in respect to his given by the function coeffs:
coeffs(g,h)
and it returns:
[exp(h),exp(h),1]
It's like this function deals with the symbolic expression like a polynomial in h.
The problem is that this function doesnt return the zero coefficients, so if I have an
g=h*exp(h)+h^2
the function returns only:
[exp(h), 1].
But what I need is:
[0,exp(h), 1]
So, what can I do here?
I went to a lot of topics on SO and the solution is:
syms h
g=h*exp(h)+h^2
m = eval(feval(symengine,'coeff',g,h,'All'));
I hate to be one of those posters that lectures, but this is listed right in the Matlab help (help coeff) or online
coeffs(g,h, 'All')

Constructing Symbolic Functions in Matlab

I'm trying to build a symbolic function in Matlab, as follows:
syms theta
Rx(theta) = cos(theta) + sin(theta);
When I enter Rx(0.1), Matlab returns cos(1/10) + sin(1/10)
But what I'm trying to do is get Matlab to evaluate it numerically. I can accomplish that with double(Rx(0.1)), but when doing the same thing on much more complex functions in a loop, the conversion to double each time causes it to run very slowly. Is there a way to alter Rx itself to give numeric output?
You could create a standard (non-symbolic), anonymous function from your symbolic function. For this you use matlabFunction
syms theta
Rx(theta) = cos(theta) + sin(theta);
Rxd = matlabFunction(Rx);
Then
>> Rxd(0.1)
ans =
1.094837581924854
Note that you may lose precision, though, as the computations are done numerically from the beginning, as opposed to symbolically and converted to double only at the end.

Chain rule with symbolic function in Matlab

My question refers to the Matlab symbolic toolbox.
I'm trying to derive a symbolic function that is a function of another symbolic function. Say that I have a function x that is an unspecified function x=x(y(theta)). I'd like to take the derivative of x with respect to theta: dx/dtheta=dx/dy * dy/dtheta
In Matlab I write
syms theta y(theta);
x=sym('x(y(theta))');
diff(x,theta)
The answer I get is 0. I really cannot figure out what is wrong with the code.
Any help is greatly appreciated. Thanks!
Derivating f(g(y)):
syms x,y
f = symfun(sym('f(x)'), [x])
g = symfun(sym('g(y)'), [y])
diff(f(g(y)),y)

Pi, Matlab Symbolic math toolbox have a bug?

Hello i have one question. When I calculate a division in matlab: x/(pi.^2)
syms x
x/(pi.^2)
ans =
(281474976710656*v)/2778046668940015
the correct answer is x/9.8696, so why is matlab giving me this result?
Is it a bug?
You have to use the vpa() command "Variable-precision arithmetic". Check this code:
syms x real; % define x as a real symbolic variable (not a complex variable)
vpa( x/(pi.^2), 5) % second argument define number of significant digits
For trigonometric expressions involving pi, it is sometimes good to define sym('pi'):
syms x real;
pi_s = sym('pi');
expr = x/pi_s^2
I try to always use the 'real' tag when using the symbolic toolbox. If you do not use it you are going to see a lot of complex conjugates and other things that are not important for your problem, because x is probably real variable.
Hope this helps,
No it is not a bug:
2778046668940015/281474976710656 = 9.8696