Having trouble using subs

4 ビュー (過去 30 日間)
Bledar Ferati
Bledar Ferati 2020 年 10 月 29 日
回答済み: Adarsh 2025 年 1 月 31 日
Trying to replace or substitude a set of variables from de1-de7 (variables) with another set of variables (variablesshort) in DE1-DE7 but for some reason the first term 'theta1ddot' - 'theta7ddot' dissepears when I use subs even though I haven't replaced it at all. Any help would be much appreciated.
de1=I*theta1ddot+B*theta1dot+K*(L^2*(theta1-theta0)-d0)+beta*(L^2*(theta1-theta0)-d0)^3-K*(L^2*(theta2-theta1)-d0)-beta*(L^2*(theta2-theta1)-d0)^3+GPE*sin(theta1);
de2=I*theta2ddot+B*theta2dot+K*(L^2*(theta2-theta1)-d0)+beta*(L^2*(theta2-theta1)-d0)^3-K*(L^2*(theta3-theta2)-d0)-beta*(L^2*(theta3-theta2)-d0)^3+GPE*sin(theta2);
de3=I*theta3ddot+B*theta3dot+K*(L^2*(theta3-theta2)-d0)+beta*(L^2*(theta3-theta2)-d0)^3-K*(L^2*(theta4-theta3)-d0)-beta*(L^2*(theta4-theta3)-d0)^3+GPE*sin(theta3);
de4=I*theta4ddot+B*theta4dot+K*(L^2*(theta4-theta3)-d0)+beta*(L^2*(theta4-theta3)-d0)^3-K*(L^2*(theta5-theta4)-d0)-beta*(L^2*(theta5-theta4)-d0)^3+GPE*sin(theta4);
de5=I*theta5ddot+B*theta5dot+K*(L^2*(theta5-theta4)-d0)+beta*(L^2*(theta5-theta4)-d0)^3-K*(L^2*(theta6-theta5)-d0)-beta*(L^2*(theta6-theta5)-d0)^3+GPE*sin(theta5);
de6=I*theta6ddot+B*theta6dot+K*(L^2*(theta6-theta5)-d0)+beta*(L^2*(theta6-theta5)-d0)^3-K*(L^2*(theta7-theta6)-d0)-beta*(L^2*(theta7-theta6)-d0)^3+GPE*sin(theta6);
de7=I*theta7ddot+B*theta7dot+K*(L^2*(theta7-theta6)-d0)+beta*(L^2*(theta7-theta6)-d0)^3-K*(L^2*(-theta7)-d0)-beta*(L^2*(-theta7)-d0)^3+GPE*sin(theta7);
variables={theta1,theta2,theta3,theta4,theta5,theta6,theta7,theta1dot,theta2dot,theta3dot,theta4dot,theta5dot,theta6dot,theta7dot};
variablesshort={str2sym('x(1)'),str2sym('x(2)'),str2sym('x(3)'),str2sym('x(4)'),str2sym('x(5)'),str2sym('x(6)'),str2sym('x(7)'),str2sym('x(8)'),str2sym('x(9)'),str2sym('x(10)'),str2sym('x(11)'),str2sym('x(12)'),str2sym('x(13)'),str2sym('x(14)')};
DE1=subs(de1,variables,variablesshort);
DE2=subs(de2,variables,variablesshort);
DE3=subs(de3,variables,variablesshort);
DE4=subs(de4,variables,variablesshort);
DE5=subs(de5,variables,variablesshort);
DE6=subs(de6,variables,variablesshort);
DE7=subs(de7,variables,variablesshort);
  2 件のコメント
Steven Lord
Steven Lord 2020 年 10 月 29 日
Can you show us how you initially defined / computed theta1ddot, theta1dot, and theta1?
Bledar Ferati
Bledar Ferati 2020 年 10 月 29 日
編集済み: Bledar Ferati 2020 年 10 月 29 日
So this would be an example of one of them as I use symbolic terms to define them. I also get an issue of some terms not being shown.
syms t real;
theta1=str2sym('theta1(t)')
theta1dot=diff(theta1,t);
theta1ddot=diff(theta1,t,2);

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

回答 (1 件)

Adarsh
Adarsh 2025 年 1 月 31 日
After going through the code, I assume you are facing the issue while substituting a differential term in the expression. After going through the MATLAB documentation, I have found the reason and the workaround for this issue.
Firstly, while using the “str2sym” function it is a good practice to use the “subs” function to substitute the expressions in the symbolic output of “str2sym” from the workspace, because the “str2sym” function just converts string to symbol but does not substitute previously assigned expression values from workspace.
For more information about “str2sym” you can refer to the MathWorks official documentation using the link below:
Secondly, the reason for the vanishing of differential term is
  1. While declaring a symbolic function all the dependent variables must be mentioned if no definition is given before applying “diff” function.
  2. Else the symbolic function must be defined with the required expression before applying “diff” function.
Here are some working code examples of the workaround achieving the required output,
Example 1:
syms t z
theta2 = subs(str2sym("x(t)"))
theta2 = 
theta2dot = diff(theta2,t,2)
theta2dot = 
theta1dot = diff(theta2,t,1)
theta1dot = 
equation = 2*theta2dot+z+theta1dot
equation = 
subs(equation,{theta2},{subs(str2sym("y(t)"))})
ans = 
Example 2:
syms t z y(z)
theta2 = subs(str2sym("x(t)"))
theta2 = 
theta2dot = diff(theta2,t,2)
theta2dot = 
theta1dot = diff(theta2,t,1)
theta1dot = 
equation = 2*theta2dot+z+theta1dot
equation = 
y(z) = z*t^2
y(z) = 
subs(equation,{theta2},{subs(str2sym("y(1)"))})
ans = 
For more information the following MATLAB documentation links can be referred:
  1. “str2sym” - https://www.mathworks.com/help/symbolic/str2sym.html
  2. “syms” - https://www.mathworks.com/help/symbolic/syms.html
  3. “symfun” - https://www.mathworks.com/help/symbolic/symfun.html
  4. Creating symbolic functions - https://www.mathworks.com/help/symbolic/create-symbolic-functions.html
I hope this helps in resolving the issue.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by