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

1 view (last 30 days)
Hamza Yusuf on 11 Nov 2020
Answered: Walter Roberson on 11 Nov 2020
Let us say we have this big expression:
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):
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;
and my output for the first part is.
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 on 11 Nov 2020
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);
c20 = c10 - coeffn(c10, thetadot2, 0);
result = t - c10 + c20;
disp(char(result))
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.

R2019b

### Community Treasure Hunt

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

Start Hunting!