# Matching polynomial coefficients when solving an equation

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}]

• Could use SolveAlways[f[x] == g[x], x] Apr 7 at 23:44
• Also Solve[CoefficientList[#, x] & /@ (f[x] == g[x])] Apr 8 at 4:52

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}} *)