2
$\begingroup$

So this is similar to this.

Suppose I have an equation $ax^2-bx+3a-c-2x^2+7cx+x^2=0$

I can group the terms in mathematica using

eq = a*x^2-b*x+3*a-c-2*x^2+7 c*x+x^2
neweq = Collect[eq,x]

to get $x^2(a-1)+x(7c-b)+3a-c$

Here's the tricky bit. How can I save all the coefficients of this equation as new equations that equal zero and save them as variables. For small equations I'd just copy paste but when it gets to higher order say $x^{70}$ it's much faster to automate.

e.g.

eq1:= a-1==0
eq2:= 7c-b==0
eq3:= 3a-c==0

I can then use Eliminate to solve for a,b and c

e.g.

Eliminate[{eq1,eq3},c]

will solve for a.

Not only will this benefit me, but anyone in the community who deals with Lie Symmetries that needs to separate equations with regards to various variables, especially derivatives.

I know loops need to be included somehow to count for eq1,eq2 etc etc but how to separate and save is too far outside of my Mathematica expertise.

Many Thanks

$\endgroup$
  • $\begingroup$ By the way, welcome to Mathematica.SE and thanks for taking the tour. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. Thanks for accepting my answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. $\endgroup$ – rhermans Jul 16 '18 at 19:01
1
$\begingroup$

Using CoefficientList and Thread

With[
 {
  eqn = a*x^2 - b*x + 3*a - c - 2*x^2 + 7 c*x + x^2,
  var = x
  },
 Thread[CoefficientList[eqn, var] == 0]
 ]
(* {3 a - c == 0, -b + 7 c == 0, -1 + a == 0} *)

If you want to assign each equation to an "good practices" indexed variable (see here)

ClearAll[eqn];
With[
  {
   equation = a*x^2 - b*x + 3*a - c - 2*x^2 + 7 c*x + x^2,
   var = x
   },
  SetDelayed[
     Evaluate[Array[eqn, Length[#]]],
     #
     ] &@Thread[CoefficientList[equation, var] == 0]
  ];

Mathematica graphics


If you insists in {eq1,eq2,eq3} then.

ClearAll /@ Names["eqn*"];
With[
  {
   equation = a*x^2 - b*x + 3*a - c - 2*x^2 + 7 c*x + x^2,
   var = x
   },
  SetDelayed[
     Evaluate[
      Table[ToExpression["eqn" <> ToString[k]], {k, Length[#]}]],
     #
     ] &@ Thread[CoefficientList[equation, var] == 0]
  ];

Information /@ Names["eqn*"];

enter image description here

$\endgroup$
  • $\begingroup$ That's so useful! (I was actually working on something like this for a different problem) Now how do I save each equation as something new ? $\endgroup$ – Ken Jul 16 '18 at 17:21
  • $\begingroup$ The same way eqn = a*x^2 - b*x + 3*a - c - 2*x^2 + 7 c*x + x^2 I'd like to save each coefficient to a variable e.g. eq1:=3a-c==0, eq2:=-b+7c etc etc $\endgroup$ – Ken Jul 16 '18 at 17:25
  • $\begingroup$ This is perfect thanks. Yes I agree with the good practice variable names. Thank you for your assistance. $\endgroup$ – Ken Jul 16 '18 at 18:57
4
$\begingroup$

If you're just interested in solving for parameter variables, you can use SolveAlways:

SolveAlways[a x^2 - b x + 3 a - c - 2 x^2 + 7 c x + x^2 == 0, x]

{{a -> 1, c -> 3, b -> 21}}

Another version using Reduce and ForAll:

Reduce[ForAll[x, a x^2 - b x + 3 a - c - 2 x^2 + 7 c x + x^2 == 0]]

c == 3 && b == 21 && a == 1

$\endgroup$
  • $\begingroup$ Thank you for your answer. I'm aware of solving for parameters using different methods. The issue in this problem was collecting the coefficients. $\endgroup$ – Ken Jul 17 '18 at 13:58
1
$\begingroup$

Rather than using Eliminate, use Solve

eq = a*x^2 - b*x + 3*a - c - 2*x^2 + 7 c*x + x^2;

sol = Solve[
   (eqns = Thread[CoefficientList[eq, x] == 0]), 
   Variables[Level[eqns, {-1}]]][[1]]

(* {a -> 1, b -> 21, c -> 3} *)

Verifying,

And @@ (eqns /. sol)

(* True *)

or

eq /. sol

(* 0 *)
$\endgroup$
  • $\begingroup$ Thank you for your feedback. I wasn't interested in the solution, more interested in collecting all the coefficients. Appreciate the time you took to answer. $\endgroup$ – Ken Jul 17 '18 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.