Hello I am trying to fit a harmonic of the form $$y = b + c\cos(x)$$ to four data points (0,6.1) (.5,5.4) (1,3.9) (1.5,1.6) using least squares for homework. I know that the error $= Y_i - f(x_i)$ but am pretty confused with the partial derivatives and linear algebra part of this question. Apparently the professor wants only $y=b+c\cos(x)$ and not $y=b+c\cos(x)+d\sin(x)$
I get two equations from taking the derivative of $E = \sum_{i=0}^n [y_i - (b+c\cos(x_i))]^2$, with respects to $b$ and $c$.
$$ \frac{dE}{db} = bn + \sum_{i=0}^n c\cos(x_i) = \sum_{i=0}^n y_i $$ and $$ \frac{dE}{dc} = b\cos(x_i) + \sum_{i=0}^n c\cos^2(x_i) = \sum_{i=0}^n \cos(x_i) y_i $$
Putting this into matrix form gives me: $$ \left[ \begin{array}{cc|c} n & \sum_{i=0}^n (x_i) & \sum_{i=0}^n (y_i) \\ \sum_{i=0}^n \cos(x_i) & \sum_{i=0}^n \cos^2(x_i) & \sum_{i=0}^n \cos(x_i)y_i \\ \end{array}\right] $$
which lands me with $$ \left[\begin{array}{cc|c} 4 & 3 & 17 \\ 2.48862 & 2.067077 & 13.05931 \end{array}\right] $$ Sorry for the terrible formatting | is meant to make this an augmented matrix (the four data points are included at the top). Anyways I'm not really sure how to solve this matrix so I assume I did something wrong with the partial derivatives. Is there anyone out there that could be so helpful as to let me know what I did wrong or any hints on how to solve this system if it is indeed solvable?


Differentiating $E = \sum_{i=0}^n [y_i - (b+c\cos(x_i))]^2$ I get $$ \frac{\partial E}{\partial b} = -2 \sum_{i=0}^n [y_i - (b+c\cos(x_i))] $$ and $$ \frac{\partial E}{\partial c} = -2 \sum_{i=0}^n [y_i - (b+c\cos(x_i))]\cos (x_i) $$ The way you wrote up the derivatives, turning them into equations half-way through writing, is a mess.
If you are going to split the sum, do it correctly: $\sum_{i=0}^n b = (n+1)b$, not $nb$. Alternatively, you could begin indexing with $i=1$, not with $i=0$.
Keeping the present notation, I get the system $$ (n+1)b +c \sum_{i=0}^n \cos(x_i) = \sum_{i=0}^n y_i $$ $$ b\sum_{i=0}^n \cos(x_i) +c \sum_{i=0}^n \cos^2(x_i) = \sum_{i=0}^n y_i \cos (x_i) $$ Here $n=3$, so $n+1=4$. You have $\sum_{i=0}^n (x_i)$ in place of $\sum_{i=0}^n \cos(x_i)$.