How can I differentiate a symbolic function?

1 vue (au cours des 30 derniers jours)
Federico Avino
Federico Avino le 17 Juin 2019
I'm in trouble with a problem and I couldn't find any solution for my specific case. I have this code:
syms l1 l2
f = xp
xp_l1 = diff(f,l1)
xp_l2 = diff(f,l2)
l1 and l2 are the two my independent variables depending on time, previously in the code i elaborate the expression of xp, that is dependent on l1 and l2 by a very complex expression. I need the partial derivatives, so i let matlab elaborate them, and it works. The problem come when i need to differentiate xp_l1 and xp_l2 by time. i tried in different ways but nothing allows me to obtain the differentiation automatically. This works but it is too complex and long
syms t l1(t) l2(t)
f = (5*l2(t))/4 - (5*l1(t))/2 + 5*l1(t)*(((200*l1(t) - 100*l2(t))/(25*(100*(l1(t))^2 - 100*l1(t)*l2(t) + 25*(l2(t))^2 + 4)) + ((200*l1(t) - 100*l2(t))*(- 100*(l1(t))^2 + 100*l1(t)*l2(t) - 25*(l2(t))^2 + 252))/(25*(100*(l1(t))^2 - 100*l1(t)*l2(t) + 25*(l2(t))^2 + 4)^2))/(4*((- 100*(l1(t))^2 + 100*l1(t)*l2 - 25*(l2(t))^2 + 252)/(2500*(l1(t))^2 - 2500*l1(t)*l2(t) + 625*(l2(t))^2 + 100))^(1/2)) + 1/2) - (5*l2(t)*(((200*l1(t) - 100*l2(t))/(25*(100*(l1(t))^2 - 100*l1(t)*l2(t) + 25*(l2(t))^2 + 4)) + ((200*l1(t) - 100*l2(t))*(- 100*(l1(t))^2 + 100*l1(t)*l2(t) - 25*(l2(t))^2 + 252))/(25*(100*(l1(t))^2 - 100*l1(t)*l2(t) + 25*(l2(t))^2 + 4)^2))/(4*((- 100*(l1(t))^2 + 100*l1(t)*l2(t) - 25*(l2(t))^2 + 252)/(2500*(l1(t))^2 - 2500*l1(t)*l2(t) + 625*(l2(t))^2 + 100))^(1/2)) + 1/2))/2 - (5*((- 100*(l1(t))^2 + 100*l1(t)*l2(t) - 25*(l2(t))^2 + 252)/(25*(100*(l1(t))^2 - 100*l1(t)*l2(t) + 25*(l2(t))^2 + 4)))^(1/2))/2
xp_l1_t= diff(f,t) % first derivative of wrt t
I wrote the dependance by t by hands. Are there any solutions to do it automatically?

Réponses (2)

Bjorn Gustavsson
Bjorn Gustavsson le 17 Juin 2019
After doing this
syms t
syms l1(t)
syms l2(t)
f = ... % your rather lengthy expression
I had no problem getting an output from diff:
dfdt = diff(f,t)
...and I run a rather old version of matlab. Do you get an error or just no output? It wasn't necessary to split up the symbolic definitions either.
HTH

Federico Avino
Federico Avino le 17 Juin 2019
Thank you, but my problem is slightly different. I had no problems with the long expression I wrote, but I'm searching for a less wasting time method to do the task. I would like to calculate the derivative wrt time directly from the first part of code, without defining again the syms and expliciting the very long expression, because i have to repeat this tasks a lot of times with a lot of different functions, and this is very time wasting.
I tried this, that teoretically solves my problem, but it doesn't work. Is there any similar solution?
syms t l1(t) l2(t)
f = xp %xp is from another part of code and has the very long expression you read up and has variables l1 and l2, and not l1(t) and l2(t)
xp_l1 = diff(f,l1)
xp_l2 = diff(f,l2)
xp_l1_t= diff(f,t)
xp_l2_t= diff(f,t)
Il presents two issues: the first is that xp has an expression elaborated by another part of code automatically and the expression xp_l1 = diff(f,l1) gives me an error due to the fact that l1 is a symbolic function itself, and the diff can't be performed (only the first object can be a symbolic function). The second issue is that the derivative wrt time gives me always 0

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by