How can find all expressions multiplied by a certain variable in a long formula?

5 次查看(过去 30 天)
Let us say we have this big expression:
t=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);
And I want all terms multiplied by thetadot1 and thetadot2 out but Matlab doesnt seem to be able to do that.
The output i seek is (from a textbook):
-m2*l1*l2*sin(theta2)*thetadot2^2-2*m2*l1*l2*sin(theta2)*thetadot1*thetadot2
The first term (-m2*l1*l2*sin(theta2)*thetadot2^2) and the subsequent one are both from the original but we have only taken what we wish i.e all terms with thetadot1 and thetadot2.
I have used
n=1;
p=feval(symengine, 'coeff', t, thetadot1,n)
and my output for the first part is.
-2*l1*l2*m2*thetadot2*sin(theta2)
So I have lost the exponent. I am hoping someone can help me troubleshoot this. I need to find a universial method as i need to implement it on a 40 microsoft word page long formula

采纳的回答

Walter Roberson
Walter Roberson 2020-11-11
Your question is ambiguous as to what you are trying to retain, whether you are trying to retain terms that involve either thetadot1 or thetadot2, or only terms that involve multiplying by both of those variables.
You start by asking for terms that involve thetadot1. There is a single such term in the original expression, namely -2*m2*l1*l2*sin(theta2)*(thetadot1*thetadot2) . The coefficient associated with thetadot1 in that term is -2*m2*l1*l2*sin(theta2)*thetadot2 which is exactly the output MATLAB returned. You did not ask it for the coefficients for thetadot2.
You are also asking only about the first coefficient, power 1, but one of the terms you want in the result involves power 2
coeffn = @(poly, var, n) feval(symengine, 'coeff', poly, var, n);
syms m1 m2 l1 l2 thetadotdot1 thetadotdot2 thetadot1 thetadot2 theta1 theta2 g
t = 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);
c10 = coeffn(t, thetadot1, 0);
c20 = c10 - coeffn(c10, thetadot2, 0);
result = t - c10 + c20;
disp(char(result))
- l1*l2*m2*thetadot2^2*sin(theta2) - 2*l1*l2*m2*thetadot1*thetadot2*sin(theta2)
That is, we look in t for all the terms that do not involve the first variable. t minus that would leave you with terms that only involve any power of the first variable. We fish through the terms that did not involve the first variable to find any terms that involve the second variable: those would be terms that involve the second variable but not the first variable. Terms that involved both variables would not have been selected by coeff 0 of t. When we take the terms that did not involve the first variable and subtract out the terms of that that did not involve the second variable either, then we are left with the terms that involve only the second but not the first in c20. So now we take the original expression, subtract out the terms that did not involve the first variable, add back in the terms that involved the second variable but not the first, and we get our result.
This process can obviously be extended to any number of variables with a small loop.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by