# Systems of linear equations to calculate $\alpha$ and $\beta$

Point $1$: When there is $1$ car passing the road, the average speed is $50$ km/h.

Point $2$: When there are $5$ cars passing the road, the average speed is $45$ km/h.

Point $3$: When there are $12$ cars passing the road, the average speed is $38$ km/h.

A traffic engineering company decides to model the average speed (shown by $u$) as a linear function of the number of cars (shown by $n$). So we want to have

$u(n)=\alpha+\beta n$ .

• Using Point $1$ , Point $2$ and Point $3$ information: write a system of linear equations to calculate $\alpha$ and $\beta$ using all three points; i.e. we will have three equations with two unknowns.
• Looks like you already have figured out the answer ! – Sudarsan Sep 30 '13 at 22:48

Using only Point 1 and 2 you have:

\begin{align} \alpha&=50-\beta\\ \alpha+5\beta&=45\\ \Rightarrow 50+4\beta&=45\\ \beta&=-1.25 \end{align} and then using the first line you get $\alpha=50-(-1.25)=51.25$. Using MATLAB, it's a simple linear system which you can solve using linsolve.

A = [1 1; 1 5]
B = [50 ; 45]

linsolve(A, B)


What this command does is that it solves the system but written in matrix form:

\begin{align} \mathbf{Ax}&=\mathbf{B}\\ \begin{pmatrix}1 &1\\1 & 5\end{pmatrix}\begin{pmatrix}\alpha \\ \beta\end{pmatrix}&=\begin{pmatrix}50 \\ 45\end{pmatrix} \end{align}

I think MATLAB for computational reasons uses LU decomposition, but you could also premultiply by $\mathbf{A}^{-1}$: \begin{align} \mathbf{x}&=\mathbf{A}^{-1}\mathbf{B}\\ \begin{pmatrix}\alpha \\ \beta\end{pmatrix}&=\frac{1}{4}\begin{pmatrix}5 &-1\\-1 & 1\end{pmatrix}\begin{pmatrix}50 \\ 45\end{pmatrix}=\begin{pmatrix}\frac{205}{4}\\-\frac{5}{4}\end{pmatrix}=\begin{pmatrix}51.25\\-1.25\end{pmatrix} \end{align}

Using this approach, you could run the following in MATLAB instead of the linsolve command:

A = [1 1; 1 5]
B = [50 ; 45]

inv(A)*B


EDIT: If you were to do the same for Point 2 and Point 3, the system is instead:

\begin{align}\alpha+5\beta&=45\\ \alpha+12\beta&=38 \end{align} Rewriting this in matrix notation gives us:

$$\begin{pmatrix}\alpha+5\beta\\\alpha+12\beta\end{pmatrix}=\begin{pmatrix}45\\38\end{pmatrix}\Leftrightarrow \begin{pmatrix}1 & 5 \\ 1 & 12 \end{pmatrix}\begin{pmatrix}\alpha\\\beta\end{pmatrix}=\begin{pmatrix}45\\38\end{pmatrix}$$

This means that $$\mathbf{A}=\begin{pmatrix}1 & 5 \\ 1 & 12 \end{pmatrix}, \quad \mathbf{B}=\begin{pmatrix}45\\38\end{pmatrix}$$ which in MATLAB then is

A = [1 5; 1 12]
B = [45 ; 38]

linsolve(A, B)

• If I were to do the same for point 2 and 3, does A = [1 1; 1 5] remain the same? In the linear equations, where did you get 4 and 5 from? Thank you! – Lidya Amos Sep 29 '13 at 12:54
• @LidyaAmos No it does not. You need to change $\mathbf{A}$ such that it multiplied with the vector of coefficients ($\alpha$ and $\beta$) gives you the left hand sides of your system of equations. See my updated post. Which 4 and 5 are you thinking of? $\frac{1}{4}$ is $\frac{1}{\det(\mathbf{A})}$ if that's what you mean, and $5$ is the number of cars. – hejseb Sep 29 '13 at 13:17
• I see what you mean. Thank you so much for your help. I have one more question to ask if that's okay! I'm just really illiterate when it comes to MATLAB. - Now use Point 1, Point 2 and Point 3 information: write a system of linear equations to calculate and using all three points; i.e. we will have three equations with two unknowns. Use MATLAB to solve this system. – Lidya Amos Sep 30 '13 at 0:49
• @LidyaAmos Then you need to change $\mathbf{A}$ and $\mathbf{B}$ accordingly. Add the missing rows. A = [1 1; 1 5; 1 12] and B = [50 ; 45; 38]. – hejseb Sep 30 '13 at 6:31
• Thank you @hejseb. How would I write out the system of linear equations? – Lidya Amos Sep 30 '13 at 10:00

You are given the equation, and also a few data points: for each of the three scenarios, you have the number of cars ($n$) and the observed average speed ($u(n)$).

So your three equations are \begin{align*} 50 &= \alpha + \beta\\ 45 &= \alpha + 5\beta\\ 38 &= \alpha + 12\beta \end{align*}

1. Can you write this in matrix form, i.e. can you write down a matrix $M$ and vector $b$ such that $$M\left[\begin{array}{c}\alpha\\\beta\end{array}\right] = b?$$

2. An overconstrained system of equations does not always have a solution. But you can always find an $\alpha$ and $\beta$ that minimizes the error, i.e. minimizes the square residual: $$\min_{\alpha, \beta}\quad \left\|M\left[\begin{array}{c}\alpha\\\beta\end{array}\right] - b\right\|^2.$$ Finding this minimizier is called solving the least squares problem. Do you need more help for how to solve it here?

For Point $1$ you plug in $n=1$ and $u(1)=50$ to get $50=\alpha +\beta (1)$, or just $50=\alpha +\beta$. You can do the same with the rest.

EDIT: Here is Point $2$: Here you have $n=5$ and $u(5)=45$. So, you get $45=\alpha +5\beta$. Can you do Point $3$ now?

• I'm still a little confused. How do I write all 3? – Lidya Amos Sep 30 '13 at 13:08

I guess you are to solve it with the best fit, as in Linear Fit or in general linear regression. Have you done that part in your course yet?

In short: To solve such $Ax=b$ solve $A^T Ax=A^T b$ instead.

$\left[\begin{array}{cc}1 & 1\\ 1 & 5\\ 1 & 12\end{array}\right]\left[\begin{array}{c}\alpha\\ \beta\end{array}\right] = \left[\begin{array}{c}50\\45\\38\end{array}\right]$

You have three equations u(n) = alpha + beta * n and three data points. Since the model is not perfect (your three points do not perfectly align), you can define an error function which could write

Error = (alpha + beta - 50)^2 + (alpha + 5 * beta - 45)^2 + (alpha + 12 * n - 38)^2

This Error function represents the sum of squares of the vertical distances between the exact speeds and the approximate speeds; this is the principle of linear least square fit method.

In order to minimize the errors, derive Error with respect to alpha and to beta and set them to zero. This will lead to two linear equations for the two unknowns alpha and beta.

Are you able to continue with this ?

$$\left(% \begin{array}{cc} 1 & 1 \\ 1 & 5 \\ 1 & 12 \end{array}\right) \left(% \begin{array}{c} \alpha \\ \\ \beta \end{array}\right) = \left(% \begin{array}{cc} 50 \\ 45 \\ 38 \end{array}\right)\,, \qquad\qquad \left(% \begin{array}{ccc} 1 & 1 & 1 \\ 1 & 5 & 12 \end{array}\right) \left(% \begin{array}{cc} 1 & 1 \\ 1 & 5 \\ 1 & 12 \end{array}\right) \left(% \begin{array}{c} \alpha \\ \\ \beta \end{array}\right) = \left(% \begin{array}{ccc} 1 & 1 & 1 \\ 1 & 5 & 12 \end{array}\right) \left(% \begin{array}{cc} 50 \\ 45 \\ 38 \end{array}\right)$$

$$\left(% \begin{array}{cc} 3 & 18 \\ 18 & 169 \end{array}\right) \left(% \begin{array}{c} \alpha \\ \beta \end{array}\right) = \left(% \begin{array}{cc} 133 \\ 731 \end{array}\right)\,, \qquad\qquad \left\{% \begin{array}{rcrcl} 3\alpha & + & 18\beta & = & 133 \\ 18\alpha & + & 169\beta & = & 731 \end{array}\right.$$

$$\alpha = {9,319 \over 183} = 50.9234\ldots\qquad\qquad \beta = -\,{67 \over 61} = -1.098\ldots$$

This is the best compromise !!!. How about the method ?. Let's assume we have a system, for an unknown vector $\vec{v}$, ${\bf A}\vec{v} = \vec{b}$ which 'we can not satisfy'. ${\bf A}$ is a given matrix and $\vec{b}$ is a given vector. Then, we are glad whenever we keep ${\bf A}\vec{v}$ as close as possible to $\vec{b}$. It means that we minimize ${\cal F} \equiv\left({\bf A}\vec{v} - \vec{b}\right)^{2}$:

$${\cal F} = \left(\vec{v}^{\rm T}{\bf A}^{\rm T} - \vec{b}^{\rm T}\right) \left({\bf A}\vec{v} - \vec{b}\right) = \vec{v}^{\rm T}{\bf A}^{\rm T}{\bf A}\vec{v} - \vec{v}^{\rm T}{\bf A}^{\rm T}\vec{b} - \vec{b}^{\rm T}{\bf A}\vec{v} + \vec{b}^{\rm T}\vec{b}$$

$$0 = \delta{\cal F} = \delta\vec{v}^{\rm T} \left({\bf A}^{\rm T}{\bf A}\vec{v} - {\bf A}^{\rm T}\vec{b}\right) + \left(\vec{v}^{\rm T}{\bf A}^{\rm T}{\bf A} - \vec{b}^{\rm T}{\bf A}\right)\delta\vec{v}$$

$$\mbox{which yields}\quad {\bf A}^{\rm T}{\bf A}\vec{v} = {\bf A}^{\rm T}\,\vec{b}$$

• I think that there are some typo's in your calculations. What I found ia alpha = 50.8172 and beta = -1.08065. – Claude Leibovici Oct 1 '13 at 4:59
• @ClaudeLeibovici Yes. You are right. I already checked and put the correct answer. Thanks. – Felix Marin Oct 1 '13 at 5:50