1
$\begingroup$

Two polynomials of the same degree are equal if the they have the same coefficients. I would like to use that to find all coefficients of a polynomial:

f[x_] := a*x^2 + b*x + c; 
g[x_] := (x + 1)^2; 
Solve[f[x] == g[x], {a, b, c}]

But Mathematica returns

{{c -> 1 + 2 x - b x + x^2 - a x^2}}

I would like the answer to be $a=1, b=2, c=1$. Solve seems not to be the right function to do that. I need to indicate somewhere that the answer cannot depend on the variable $x$ as the coefficients are constant. Any idea which is the right function ?

The solution must be flexible to also solve variants of the problem such as:

f[x_] := a*(x-5)^2 + b*(x+1) + c; 
g[x_] := (x + 1)^2; 
Solve[f[x] == g[x], {a, b, c}]
$\endgroup$
2
  • 4
    $\begingroup$ Could use SolveAlways[f[x] == g[x], x] $\endgroup$ – Carl Woll Apr 7 at 23:44
  • $\begingroup$ Also Solve[CoefficientList[#, x] & /@ (f[x] == g[x])] $\endgroup$ – Bob Hanlon Apr 8 at 4:52
2
$\begingroup$

As Carl mentioned, SolveAlways is the perfect solution here:

Clear[f, g, h, i]

f[x_] := a*x^2 + b*x + c;
g[x_] := (x + 1)^2;
SolveAlways[f[x] == g[x], x]

(* Out: {{a -> 1, b -> 2, c -> 1}} *)


h[x_] := a*(x - 5)^2 + b*(x + 1) + c;
i[x_] := (x + 1)^2;
SolveAlways[h[x] == i[x], x]

(* Out: {{a -> 1, b -> 12, c -> -36}} *)
$\endgroup$

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.