How to simplify a common factor in a symbolic equation

30 views (last 30 days)
My code is as follows
syms I_1 phi_1_d2 R_1 f_g mu_1 I_2 phi_2_d2 R_2 mu_2
eq_1= mu_1== I_1 * phi_1_d2 + R_1 * f_g;
eq_2= mu_2== I_2 * phi_2_d2 - R_2 * f_g;
eq_1= expand(eq_1 * (I_2 * R_1));
eq_2= expand(eq_2 * (I_1 * R_2));
eq_3= eq_1 - eq_2;
eq_3= collect(eq_3, [I_1, I_2]);
eq_3= collect(eq_3, f_g);
eq_4= eq_3 / ((I_2 * (R_1 ^2)) + (I_1 * (R_2 ^2)));
eq_4= collect(eq_4, f_g)
With the output being
eq_4 =
(R_1*mu_1*I_2 + (-R_2*mu_2)*I_1)/(I_2*R_1^2 + I_1*R_2^2) == ((I_2*R_1^2 + I_1*R_2^2)/(I_2*R_1^2 + I_1*R_2^2))*f_g + (I_1*I_2*(R_1*phi_1_d2 - R_2*phi_2_d2))/(I_2*R_1^2 + I_1*R_2^2)
This is correct as far as I can tell, however I'm unsure how to make matlab recognise that the coefficient for the f_g term is 1
Any help would be appreciated, I am new to MATLAB and unfamiliar with the symbolic toolkit
  2 Comments
John D'Errico
John D'Errico on 18 May 2020
Edited: John D'Errico on 18 May 2020
The coef of the f_g term, meaning this expression from eq_4?
((I_2*R_1^2 + I_1*R_2^2)/(I_2*R_1^2 + I_1*R_2^2))
So now you wish to (programmatically) extract that coefficient, and set it to 1 as a new equation? I'll admit to being a bit lazy. I'd probably just use cut and paste, a far faster alternative to reading the help for the function coeffs. ;-)
Ok, I think you are saying that you see this expression is just the ratio of two numbers, which happen to be the same, and you are asking how to force MATLAB to recognize that X/X is just 1? Sometimes getting a computer to recognize what you see as obvious is harder than just using pencil and paper. But is that your question?
Kieran Corrigan
Kieran Corrigan on 18 May 2020
Your edit is correct I’m trying to get matlab to recognise the coefficients of the f_g term are alike and cancel to 1.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 18 May 2020
Its still easier to just use cut and paste. ;-) Damn these computers. I want my slide rule back!
As I said, sometimes it gets complicated to tell the computer what seems obvious. What happens if I try this:
simplify(expand(eq_4))
ans =
I_2*R_1^2 + I_1*R_2^2 ~= 0 & I_2*(R_1*mu_1 + I_1*R_2*phi_2_d2) == I_2*f_g*R_1^2 + I_1*I_2*phi_1_d2*R_1 + I_1*f_g*R_2^2 + I_1*mu_2*R_2
Now we see an anded expression, thus:
I_2*R_1^2 + I_1*R_2^2 ~= 0
AND
I_2*(R_1*mu_1 + I_1*R_2*phi_2_d2) == I_2*f_g*R_1^2 + I_1*I_2*phi_1_d2*R_1 + I_1*f_g*R_2^2 + I_1*mu_2*R_2
The problem is X/X is not ALWAYS 1. When X == 0, we have a problem, and then MATLAB gets obstinate, worrying about the singular case where X == 0. It could happen.
But I think this is what you need:
combine(eq_4)
ans =
(I_2*R_1*mu_1 - I_1*R_2*mu_2)/(I_2*R_1^2 + I_1*R_2^2) == f_g + (I_1*I_2*(R_1*phi_1_d2 - R_2*phi_2_d2))/(I_2*R_1^2 + I_1*R_2^2)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!