My textbook says (derived from the law of cosines. Assume $|v-w| = c$. The equation below is basically $a^2 = b^2 + c^2 - 2bc \cos\theta$ rearranged:
$$|v|^2 + |w|^2 = |v-w|^2 + 2|v||w|\cos\theta$$
The $|v|$ signify norm / magnitude. The book then says this equals
$$\sum_{i=1}^n (v_i^2+w_i^2) = \sum_{i=1}^n(v_i-w_i)^2 + 2|v||w|\cos \theta$$
Then it says "After computing the squares on the right hand side of the equation and simplifying, we obtain"
$$|v||w|(\cos\theta) = \sum_{i=1}^n v_iw_i$$
How did we obtain this? My calculation is this. The right hand side is this:
$$ \sum_{i=1}^n(v_i-w_i)^2 + 2|v||w|\cos \theta = \sum_{i=1}^n(v_i^2-w_i^2) + 2|v||w|\cos \theta $$
Now if we bring $\sum_{i=1}^n (v_i^2-w_i^2)$ to the left hand side, the v's become subtraction and the w's become addition, right? So we end up with the v's cancelling each other out after bringing them to the left hand side, and then we end up with
$$2\sum_{i=1}^n(w_i^2) = 2|v||w|\cos\theta$$
and divide both sides by two, we get
$$\sum_{i=1}^nw_i^2 = |v||w|\cos\theta$$
Where am I going wrong with the calculation? (I tried my best to copy straight from the textbook, hopefully I did it correctly).
Note that $(v_i-w_i)^2 \neq v_i^2 -w_i^2$ in general.
Correction to your mistake is $$(v_i - w_i)^2 = v_i^2 \color{red}{+ w_i^2 -2v_iw_i}$$
We have
$$\sum_{i=1}^n (v_i^2+w_i^2)=\sum_{i=1}^n(v_i^2+w_i^2-2v_iw_i) + 2|v||w| \cos\theta$$
Canceling $\sum_{i=1}^n (v_i^2+w_i^2)$ from both sides and dividing by $2$, we obtain the result.