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}]
SolveAlways[f[x] == g[x], x]
$\endgroup$ – Carl Woll Apr 7 at 23:44Solve[CoefficientList[#, x] & /@ (f[x] == g[x])]
$\endgroup$ – Bob Hanlon Apr 8 at 4:52