Substitute a differential expression for a variable subs()

조회 수: 7 (최근 30일)
Mario Miralles Delgado
Mario Miralles Delgado 2017년 5월 18일
답변: Adarsh 2025년 1월 29일
I want to substitute in the following matrix result: A=[diff(theta(t),t) -diff(psi(t),t); -diff(theta(t),t) diff(psi(t),t)] I want to substitute diff(theta(t),t) for a variable called thetap and diff(psi(t),t) for a variable called psip in orden to obtain the following matrix A=[thetap -psip -thetap psip]
then I tried to do this:
theta2 = sym('theta2(t)') psi2 = sym('psi2(t)'
A=[diff(theta(t),t) -diff(psi(t),t); -diff(theta(t),t) diff(psi(t),t)]
dtheta = diff(theta,t) dpsi = diff(psi,t)
Ap_s = subs(A,{dtheta,dpsi},{thetap,psip})
but it doesn't work. It returns again the dependent expression diff(theta(t),t) and diff(psi(t),t) without doing the substitution. I've tried: Ap_s = subs(A,{diff(theta(t),t),diff(psi(t),t)},{thetap,psip}) but it doesn't work. Can anybody help me?

답변 (1개)

Adarsh
Adarsh 2025년 1월 29일
Upon observing the given code, I have understood that you are trying to substitute the differential expressions in the matrix with shorter & new symbols.
I have found a workaround for the task which you are trying to accomplish, in the MATLAB documentation I have found a function named “str2sym” which converts the string version of a symbolic expression to original symbolic expression type.
Using this function, the expressions which are to be replaced can be given as input to the “subs” function as a cell array of symbolic types as shown here:
syms theta(t) t psi(t)
A=[diff(theta(t),t) -diff(psi(t),t); -diff(theta(t),t) diff(psi(t),t)] % Initial given matrix where substitution has to be done.
A = 
theta2 = str2sym('theta2(t)')
theta2 = 
psi2 = str2sym('psi2(t)')
psi2 = 
subs(A,{str2sym("diff(theta(t),t)"),str2sym("diff(psi(t),t)")},{theta2,psi2})% Using str2sym function to convert strings to symbolic expressions and pass them into subs as argument.
ans = 
by using this the required substitutions can be performed correctly as required.
Here is the link for the MATLAB documentation stating the usage of “str2sym” and “subs” functions :
  1. https://www.mathworks.com/help/symbolic/str2sym.html
  2. https://www.mathworks.com/help/symbolic/sym.subs.html
I hope this helps in achieving the desired output.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by