Symbolic math- substitute a derivative function with subs()?

19 ビュー (過去 30 日間)
Maria Perdomo
Maria Perdomo 2014 年 2 月 13 日
回答済み: Andrew Horchler 2014 年 11 月 18 日
Hello everyone,
I've got a symbolic derivative :
syms J phi(t) Ja J0 x1 x2
J=J0-Ja*cos(2*phi);
dJ=diff(J)
This yields:
dJ(t)=2*Ja*D(phi)(t)*sin(2*phi(t))
Now, in this function I want to substitute phi(t) for x1 and D(phi)(t) for x2.
subs(dJ(t),phi,x1)
ans =
2*Ja*D(phi)(t)*sin(2*x1)
but this doesnt work for D(phi)(t)
>> subs(dJ(t),D(phi)(t),x2)
Error: ()-indexing must appear last in an index expression.
>> subs(dJ(t),D(phi),x2)
Undefined function 'D' for input arguments of type 'symfun'.
>> subs(dJ(t),D(phi(t)),x2)
Undefined function 'D' for input arguments of type 'sym'.
How could it be substituted by x2?

回答 (3 件)

Andrew Horchler
Andrew Horchler 2014 年 11 月 18 日
A late answer, but maybe this will help others. See my answer here on StackOverflow.com/Matlab. In this case you can substitute both variable at once via:
syms phi(t) Ja J0 x1 x2
J = J0-Ja*cos(2*phi);
dJ = diff(J,t)
dphi = diff(phi,t)
subs(dJ,{phi,dphi},{x1,x2})
You also don't need to define J as symbolic using syms since it gets allocated as such when you create the equation. I do usually like to make explicit the variable that I'm differentiating with respect to as well.

Kobi
Kobi 2014 年 2 月 13 日
why don't you sub one char at a time?
  2 件のコメント
Maria Perdomo
Maria Perdomo 2014 年 2 月 13 日
Thats what I do, the problem is the syntaxis for
D(phi)(t)
that is a derivative function
Maria Perdomo
Maria Perdomo 2014 年 2 月 13 日
I do one at a time, you see it works for phi(t):
dJ(t)=2*Ja*D(phi)(t)*sin(2*phi(t))
subs(dJ(t),phi,x1)
ans =
2*Ja*D(phi)(t)*sin(2*x1)

サインインしてコメントする。


Walter Roberson
Walter Roberson 2014 年 2 月 13 日
syms x3
subs( subs(dJ(t),D(phi),x3), x3(t), x2 )
  2 件のコメント
Maria Perdomo
Maria Perdomo 2014 年 2 月 14 日
syms J phi(t) Ja J0 x1 x2 x3
J=J0-Ja*cos(2*phi);
dJ=diff(J);
subs( subs(dJ(t),D(phi),x3), x3(t), x2 )
>>Undefined function 'D' for input arguments of type 'symfun'.
Walter Roberson
Walter Roberson 2014 年 2 月 14 日
subs( subs(dJ(t),sym('D(phi)'),x3), x3(t), x2 )

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by