theta2 = 
Having trouble using subs
Ältere Kommentare anzeigen
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 Kommentare
Steven Lord
am 29 Okt. 2020
Can you show us how you initially defined / computed theta1ddot, theta1dot, and theta1?
Bledar Ferati
am 29 Okt. 2020
Bearbeitet: Bledar Ferati
am 29 Okt. 2020
Antworten (1)
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
- While declaring a symbolic function all the dependent variables must be mentioned if no definition is given before applying “diff” function.
- 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)"))
theta2dot = diff(theta2,t,2)
theta1dot = diff(theta2,t,1)
equation = 2*theta2dot+z+theta1dot
subs(equation,{theta2},{subs(str2sym("y(t)"))})
Example 2:
syms t z y(z)
theta2 = subs(str2sym("x(t)"))
theta2dot = diff(theta2,t,2)
theta1dot = diff(theta2,t,1)
equation = 2*theta2dot+z+theta1dot
y(z) = z*t^2
subs(equation,{theta2},{subs(str2sym("y(1)"))})
For more information the following MATLAB documentation links can be referred:
- “str2sym” - https://www.mathworks.com/help/symbolic/str2sym.html
- “syms” - https://www.mathworks.com/help/symbolic/syms.html
- “symfun” - https://www.mathworks.com/help/symbolic/symfun.html
- Creating symbolic functions - https://www.mathworks.com/help/symbolic/create-symbolic-functions.html
I hope this helps in resolving the issue.
Kategorien
Mehr zu Operations on Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



