How to find all double expressions added to a very long formula?

1 回表示 (過去 30 日間)
Hamza Yusuf
Hamza Yusuf 2020 年 11 月 12 日
編集済み: Ayush Modi 2024 年 8 月 30 日
Given a large formula how do i find all expressions multiplied by two terms such as.
syms m1 m2 l1 l2 thetadotdot1 thetadotdot2 thetadot1 thetadot2 theta1 theta2 g; n=1;
t1=m2*l2^2*(thetadotdot1+thetadotdot2)+m2*l1*l2*cos(theta2)*(2*thetadotdot1+thetadotdot2)+(m1+m2)*l1^2*thetadotdot1-m2*l1*l2*sin(theta2)*(thetadot2^2)-2*m2*l1*l2*sin(theta2)*(thetadot1*thetadot2)+m2*l2*g*cos(theta1+theta2)+(m1+m2)*l1*g*cos(theta1);
The output should be
-2*m2*l1*l2*sin(theta2)
Where the coeficient is
(thetadot1*thetadot2)
Currently I am using this method:
feval(symengine, 'coeff', t1, thetadot1,n)/thetadot2;
But I want a better bethod that this, as this might not work for all cases.

回答 (1 件)

Ayush Modi
Ayush Modi 2024 年 8 月 30 日
編集済み: Ayush Modi 2024 年 8 月 30 日
Hi Yusuf,
I understand you want to get the coefficient values of all the terms which contains thetadot1*thetadot2.
You can achieve this using "coeffs" function. Here is the code to get you started:
syms m1 m2 l1 l2 thetadotdot1 thetadotdot2 thetadot1 thetadot2 theta1 theta2 g; n=1;
t1=m2*l2^2*(thetadotdot1+thetadotdot2)+m2*l1*l2*cos(theta2)*(2*thetadotdot1+thetadotdot2)+(m1+m2)*l1^2*thetadotdot1-m2*l1*l2*sin(theta2)*(thetadot2^2)-2*m2*l1*l2*sin(theta2)*(thetadot1*thetadot2)+m2*l2*g*cos(theta1+theta2)+(m1+m2)*l1*g*cos(theta1)
t1 = 
target_term = thetadot1 * thetadot2;
t1_factored = factor(t1);
[coeffs_array, terms_array] = coeffs(t1_factored, [thetadot1, thetadot2]);
index = find(terms_array == target_term);
coefficient = coeffs_array(index)
coefficient = 
Refer to the following MathWorks documentation for more information on "coeffs" function:

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by