How can I differentiate a symbolic function?
1 回表示 (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (2 件)
Bjorn Gustavsson
2019 年 6 月 17 日
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!