---
tag: M347T41A7J
kind: example
name: Minimizing a sum of squares term by term
msc: ["00A35", "26A06"]
topics: [X1NSTVGDNQ]
audience: ug1
not_instance_of:
  - target: KEZPDNM2NH
    note: the two terms share the variable x, so the partial results (minimizers x = 1 and x = 3) cannot be recombined into a minimizer of the sum
uses: [KEZPDNM2NH, XNCV97HTZ8]
sources:
  - url: https://en.wikipedia.org/wiki/Completing_the_square
    locator: lead section
---

## Example

We look for the minimum of
$$f(x) = (x - 1)^2 + (x - 3)^2$$
over all real $x$.
A naive [[decomposition|KEZPDNM2NH]] splits $f$ into its two terms and minimizes each term alone.
We follow it and see why the recombination fails.

1. The term $(x - 1)^2$ has its minimum $0$ at $x = 1$.
2. The term $(x - 3)^2$ has its minimum $0$ at $x = 3$.
3. The two partial answers ask for two different values of the same variable $x$, so no single $x$ gives both minima.
4. Adding the minima gives $0 + 0 = 0$, but $f(x) = 0$ would need $x = 1$ and $x = 3$ at once, so the value $0$ is never reached.

The correct solution treats $f$ as a whole.

1. Expand: $f(x) = 2x^2 - 8x + 10$.
2. [[Complete the square|XNCV97HTZ8]]: $f(x) = 2(x - 2)^2 + 2$.
3. So $f(x) \ge 2$ for all $x$, with equality exactly at $x = 2$.

The minimum is $2$, at $x = 2$, and neither partial answer is the minimizer.

## Remarks

The decomposition fails because the pieces are coupled: both terms depend on the same variable.
When the variables are separate, the same idea works.
The function $g(x, y) = (x - 1)^2 + (y - 3)^2$ has its minimum $0$ at $(1, 3)$, found term by term.

The sum of the partial minima is still useful as a lower bound, since in general $\min (u + v) \ge \min u + \min v$.
Here the bound is $0$, below the true minimum $2$.

The midpoint $x = 2$ of the partial answers is the minimizer only because the two terms have equal weight.
For unequal weights the guess fails:
$$h(x) = (x - 1)^2 + 4(x - 3)^2 = 5x^2 - 26x + 37$$
has its minimum at $x = \frac{13}{5} = 2.6$, with value $3.2$.
In general $\sum w_i (x - a_i)^2$ with positive weights has its minimum at the weighted mean $\frac{\sum w_i a_i}{\sum w_i}$.

## Explanations

### L1 human

Two friends live at kilometer $1$ and kilometer $3$ of a straight road, and they want to meet.
Each friend alone would choose to meet at home, so asking each friend alone gives two answers that cannot both be used.
There is one meeting point for both, so the problem must be solved as one problem.
If the cost is the sum of the squared distances, the best point is kilometer $2$.

### L2 human

Before you split an objective into pieces, test whether the pieces are independent.
Write the objective as a sum $u + v$ and list the variables of each piece.
If $u$ depends only on $x$ and $v$ only on $y$, then the minimum of $u + v$ is $\min u + \min v$, reached at the pair of minimizers.
If the pieces share a variable, as here, the partial minimizers may disagree, and the split fails.
Then solve the coupled problem as a whole, here by completing the square, and use $\min u + \min v$ only as a lower bound.

The partial answers $1$ and $3$ suggest the answer $2$, which is right here only because of the equal weights.
With the weights $1$ and $4$ the same guess gives $2$ again, but the minimizer is $2.6$.
