Euler Hall — free maths guides

Set up A and b for a straight-line fit

Write one equation C+D ti = yi per data point and read A and b off them. The idea in plain words, a worked example, and a check with the answers shown — part of the free Linear Algebra guides.

Plain words first

You havemm points(t1,y1),…,(tm,ym)(t_1,y_1),\dots,(t_m,y_m) and you want the liney=C+Dty=C+Dt that passes as close as possible to all of them.

Start from what "the point is on the line" would mean. For pointii it would mean C+D ti=yi.C+D\,t_i=y_i.

That is one equation per point, in the two unknownsCC andDD. Gather themm equations:

C+D t1=y1,C+D t2=y2,…,C+D tm=ym.C+D\,t_1=y_1,\quad C+D\,t_2=y_2,\quad\dots,\quad C+D\,t_m=y_m.

Now read them as a matrix. The unknowns are ordered(C,D)(C,D), so column 1 holds the coefficients ofCC and column 2 holds the coefficients ofDD. The coefficient ofCC is11 in every equation. The coefficient ofDD is that point's time:

Two things to notice, because they are the two marks on a "set up" question:

  • - One row per point. The number of rows is the number of data points, never the number of unknowns.
  • - One column per coefficient. A line has two, soAA has two columns whatever the number of points.

Andb\mathbf b holds the observed heights, in the same order as the rows. Reorderingb\mathbf b while leavingAA alone is invisible in every shape check and wrong in every answer.

Why the design matrix has this shape

Each row ofAx=bA\mathbf{x}=\mathbf b is one dot product, and that dot product must reproduce one equation.

Rowii ofAA dotted withx=[CD]\mathbf{x}=\begin{bmatrix}C\\D\end{bmatrix} givesAi1C+Ai2DA_{i1}C+A_{i2}D. For this to equalyiy_i — which is what the data point demands — we needAi1C+Ai2D=C+DtiA_{i1}C+A_{i2}D=C+D t_i. Matching coefficients:Ai1=1A_{i1}=1 andAi2=tiA_{i2}=t_i.

So the row is forced. There is no choice to make once the model and the unknown order are fixed. That is why "set up" is a markable question: it has exactly one right answer, and it can be checked by substituting any data point back in.

Why the number of columns is what it is. Every coefficient in the model needs a multiplier, and each multiplier is a number you already know 11 ortit_i). Unknowns become columns; known functions of the data become entries. A model linear in its coefficients works the same way even when it is not linear intt: fory=C+Dt+Et2y=C+Dt+Et^{2} the entriesti2t_i^{2} are known, soAA simply has three columns.

Worked example in the exam's exact shape

Fity=C+Dty=C+Dt to(1,2)(1,2),(3,1)(3,1),(4,5)(4,5), and set up only.

Step 1, write the equations, one per point, unknowns in the order(C,D)(C,D): C+D=2,C+3D=1,C+4D=5.C+D=2,\qquad C+3D=1,\qquad C+4D=5.

Step 2, read off the coefficient of each unknown.

| point | coefficient ofCC | coefficient ofDD | height | |---|---|---|---| |(1,2)(1,2) |11 |11 |22 | |(3,1)(3,1) |11 |33 |11 | |(4,5)(4,5) |11 |44 |55 |

Step 3, stack. A=[111314],b=[215].A=\begin{bmatrix}1&1\\1&3\\1&4\end{bmatrix},\qquad \mathbf b=\begin{bmatrix}2\\1\\5\end{bmatrix}.

Step 4, the checks that cost nothing.

  • -AA is3×23\times2: three points, two coefficients ✓.
  • - Row 2 dotted with(C,D)(C,D) givesC+3DC+3D, which is the second equation ✓.
  • -b\mathbf b's second entry is11, and the second point is(3,1)(3,1) ✓.

Step 5, what comes next. The normal equations would beATAx^=ATbA^{T}A\hat{x}=A^{T}b. Unless the fit is asked for, stop here: solving a correct setup earns nothing extra, and solving a wrong one earns nothing at all.

By the end you can
  • Write one equation C+D t_i = y_i per data point and read A and b off them
  • State the rule: one row per point, one column per coefficient, heights in the same order
  • Reproduce a data point by dotting the matching row of A with the unknown vector
  • Recognise that swapping the columns of A requires swapping the order of the unknowns
Test yourself without looking back

Points(1,4)(1,4),(2,4)(2,4),(6,1)(6,1). Modely=C+Dty=C+Dt.

  1. 1. WriteAA andb\mathbf b.
  2. 2. What is row 3 ofAA dotted with(C,D)(C,D), and does it match the third point?
  3. 3. What wouldAA be for the modely=D ty=D\,t — no intercept?
Answers
  1. 1.A=[111216]A=\begin{bmatrix}1&1\\1&2\\1&6\end{bmatrix} andb=[441]\mathbf b=\begin{bmatrix}4\\4\\1\end{bmatrix}.
  2. 2.1⋅C+6⋅D=C+6D1\cdot C+6\cdot D=C+6D, which is the equation for the point(6,1)(6,1) ✓.
  3. 3.A=[126]A=\begin{bmatrix}1\\2\\6\end{bmatrix} andb\mathbf b is unchanged — one column, because there is one unknown. The fit is then forced through the origin, which is a different model and generally gives a different slope.
Work out where to start — free, no accountAnswer six real questions — free, no account