Symbolic équation factorisation : how to identify automatically the factor in front one variable and put it into a variable

7 views (last 30 days)
Hello,
I have a system of very long symbolic equations with many different symbolic variables which could be factorized that way:
B1=A11*V1+A12*V2+.....+A1n*Vn
:
Bm=Am1*V1+Am2*V2+.....+Amn*Vn
The coefficients Aij are a sum and multiplication of different symbolic variables and I would like to extra it to put it in a symbolic array. Is there a matlab function that I don,t know of capable of doing this?
Thank you for your time, Rémy
PS: Here is a simple example: The two equations I could have :
a=e*c*V1-c*V2+d*V1+f
b=d*e*V2+e*V1+f*d*V1+e*f*V2+e
I would like to be able to identify automatically the factors in front of Vi and put them into an array such as:
B(1)=a-f
B(2)=b-e
A(1,1)=e*c+d
A(1,2)=-c
A(2,1)=e+f*d
A(2,2)=d*e+e*f
  2 Comments
Walter Roberson
Walter Roberson on 22 Jun 2018
Your example is confusing, as you define a in terms of c and then define c in term of a, which would requires that a be expanded, and so end up defining c in terms of c . When you do that kind of thing, you are almost certain to end up with nonlinear equations, whereas your B1..Bm form is strictly linear forms.
Rémy Bretin
Rémy Bretin on 22 Jun 2018
Edited: Rémy Bretin on 22 Jun 2018
Oh yeah excuse me, I tried to make up a simple example, and I haven't realized that mistake, I will correct it.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Jun 2018
syms a b c d V1 V2 x
E1 = b*c*V1-c*V2+d*V1+b;
E2 = d*x*V2+a*V1+b*d*V1+a*b*V2+a;
E3 = d*b*V2 + c; %suppose V1 coefficient is 0?
eqns = [E1;E2;E3];
neqns = length(eqns);
vars = [V1;V2];
nvars = length(vars);
A = zeros(neqns, nvars + 1, 'sym');
extvars = [sym(1); vars];
for eqidx = 1 : neqns
[P,Q] = coeffs(eqns(eqidx), vars);
[tf, exvidx] = ismember(Q, extvars);
A(eqidx, exvidx(tf)) = P(tf);
end
B = A(:,1);
A = A(:,2:end);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!