using coeff(f,[])

2 views (last 30 days)
elio jabbour
elio jabbour on 12 Feb 2022
Answered: Shishir Reddy on 10 Jan 2025
Hello
so i am using this function.
syms theta1(t) psi1(t) xh(t) theta2(t) psi2(t) yh(t) zh(t) m J l
%position of each center of mass of each cylinder
x1 = xh+2*l+l*cos(theta1)*cos(psi1);
y1 = yh+l*sin(psi1);
z1 = zh+l*sin(theta1);
x2 = xh+2*l+2*l*cos(theta1)*cos(psi1)+l*cos(theta2)*cos(psi2);
y2 = yh+2*l*sin(psi1)+l*sin(psi2);
z2 = zh+2*l*sin(theta1)+l*sin(theta2);
%derivatives of each position
x1_dot = diff(x1,t);
x2_dot = diff(x2,t);
y1_dot = diff(y1,t);
y2_dot = diff(y2,t);
z1_dot = diff(z1,t);
z2_dot = diff(z2,t);
% Lagrange equation, calculate dT/dq of each state space :
T=1/2*m*(x1_dot^2)+0.5*m*diff(xh,t)^2+(1/2)*m*(y1_dot^2)+0.5*m*diff(yh,t)^2+(1/2)*m*(z1_dot^2)+0.5*m*diff(zh,t)^2+(1/2)*J*diff(theta1,t)+(1/2)*J*diff(psi1,t);
% calculating each dT-dq
dT_xhdot=diff(T,diff(xh,t));
DdT_xhdot_dT=diff(dT_xhdot,t)
[ coeff,state]=coeffs(DdT_xhdot_dT,[diff(xh,t,t) diff(theta1(t),t,t) diff(psi1(t), t,t) diff(theta1(t), t)*diff(psi1(t), t)])
%diff(psi1(t), t,t) diff(theta1(t),t,t) (diff(theta1(t),t))^2 (diff(psi1(t),t))^2
where i get this results :
m*diff(xh(t), t, t) - (m*(2*l*cos(psi1(t))*cos(theta1(t))*diff(psi1(t), t)^2 - 2*diff(xh(t), t, t) + 2*l*cos(psi1(t))*cos(theta1(t))*diff(theta1(t), t)^2 + 2*l*cos(theta1(t))*sin(psi1(t))*diff(psi1(t), t, t) + 2*l*cos(psi1(t))*sin(theta1(t))*diff(theta1(t), t, t) - 4*l*sin(psi1(t))*sin(theta1(t))*diff(psi1(t), t)*diff(theta1(t), t)))/2
Error using symengine
Invalid indeterminate.
Error in sym/mupadmexnout (line 1092)
out = mupadmex(fcn,args{:});
Error in sym/coeffs (line 63)
[cSym,tSym] = mupadmexnout('symobj::coeffsterms', p, args{:});
Error in snakelagrange (line 25)
[ coeff,state]=coeffs(DdT_xhdot_dT,[diff(xh,t,t) diff(theta1(t),t,t) diff(psi1(t), t,t) diff(theta1(t), t,
t)*diff(xh(t), t, t)],'All')
the code give me coeff for diff(xh,t,t) diff(theta1(t),t,t) diff(psi1(t), t,t) but once i am using diff(theta1(t),t)*diff(psi1(t), t) i get this error. i believe it has do with my syms definition. Does anyone know how to solve this ?
i also need the coeff expressed in diff(theta1(t),t)^2 which is not working either

Answers (1)

Shishir Reddy
Shishir Reddy on 10 Jan 2025
Hi Elio
The issue you're encountering is related to the coeffs function in MATLAB, which is designed to extract coefficients of polynomials. However, if it is used with non-polynomial expressions (like products of derivatives), it can lead to errors.
Kindly refer to the following code snippets to understand where the changes have to be made in the provided code to address this issue.
1. Define Temporary Variables
syms dtheta1 dpsi1 dxh
2. Replace the derivative terms in your expression with these temporary variables
DdT_xhdot_dT_sub = subs(DdT_xhdot_dT, ...
[diff(xh, t, t), diff(theta1(t), t, t), diff(psi1(t), t, t), diff(theta1(t), t), diff(psi1(t), t)], ...
[dxh, dtheta1, dpsi1, dtheta1, dpsi1]);
3. Simplify the expression after substitution
DdT_xhdot_dT_simplified = simplify(DdT_xhdot_dT_sub);
4. Extract the coefficients by substituting back to isolate each term
coeff_xh_tt = subs(DdT_xhdot_dT_simplified, [dtheta1, dpsi1, dxh], [0, 0, 1]);
coeff_theta1_tt = subs(DdT_xhdot_dT_simplified, [dtheta1, dpsi1, dxh], [1, 0, 0]);
coeff_psi1_tt = subs(DdT_xhdot_dT_simplified, [dtheta1, dpsi1, dxh], [0, 1, 0]);
coeff_theta1_dpsi1 = subs(DdT_xhdot_dT_simplified, [dtheta1, dpsi1, dxh], [1, 1, 0]);
5. Finally, display the coefficients as needed
coeff_xh_tt
coeff_theta1_tt
coeff_psi1_tt
coeff_theta1_dpsi1
For more information regarding the subs function, kindly refer the following documentation - https://www.mathworks.com/help/symbolic/sym.subs.html
I hope this resolves the issue.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!