I have the following data for a least squares problem: $$x: 0 , 1 , 3, 5 , 7, 9 , 12$$ $$y: 10 ,12, 18 ,15, 20, 25, 36$$
I have already found the normal equations and the pseudoinverse for the problem. my question is, how can I compute and plot the resulting fit for this problem using matlab?
This is a forum about mathematics, not Matlab. I will try to give you a mathematical answer and the relative code to solve this problem with Matlab.
First of all, we need to define your problem in a matricia fashion.
The equation $y = mx+ q$ contains two parameters: $m$ and $q$. Then, I can write the previous equation in a matricial form:
$$y = [x ~~~ 1]\left[\begin{array}{c}m\\q\end{array}\right].$$
Since you have a lot of $x$s and $ys$, then:
$$\left[\begin{array}{c}10\\12\\18\\\ldots\end{array}\right] = \left[\begin{array}{cc}0 ~ 1\\1 ~ 1\\ 3 ~ 1 \\ \ldots\end{array}\right]\left[\begin{array}{c}m\\q\end{array}\right].$$
Let's pose
$$U = \left[\begin{array}{cc}0 ~ 1\\1 ~ 1\\ 3 ~ 1 \\ \ldots\end{array}\right] ~\text{and}~ \theta = \left[\begin{array}{c}m\\q\end{array}\right].$$
Then, the problem is: $$y = U \theta$$ and the solution is given by calculating the pseudoinverse of $U$, that is:
$$\hat{\theta} = U^{\#}y = (U^\top U)^{-1} U^\top y.$$
Matlab code
1) If $x$ and $y$ are column vector, go to step 3.
2) If $x$ and $y$ are row vector, then transform them to column vector as follows:
3) Build up the matrix $U$:
where $7$ is the number of elements of $x$.
4) Evaluate the pseudoinverse $U^{\#}$ of U:
5) Finally, evaluate your results:
The first element of theta is $m$, while the second is $q$.
6) To plot the data and the estimated line, use the following:
7) The fitting error can be obtained as follows: