Math311-Chapter1

3233 days ago by jimlb

 

Chapter 1: Solving linear equations:

Worksheet by Jim Brown

We will use SAGE to solve a system of linear equations. There is a command to just solve them immediately, but we will see the commands to row reduce a matrix to illustrate a complete example to see how the matrix operations work.  One thing to note is that SAGE is based on the programming language Python, so counting begins at 0.  We will see below what this means. 

Example 1:  Solve the system of linear equations given by

$$2x + 3y + 4z = 1,$$
$$2 x + 4y +7z =-1,$$
$$6x +5y + 3z = 3.$$

 

  

A=matrix(QQ,[[2,3,4,1],[2,4,7,-1],[6,5,3,3]]);A; 
       
[ 2  3  4  1]
[ 2  4  7 -1]
[ 6  5  3  3]
[ 2  3  4  1]
[ 2  4  7 -1]
[ 6  5  3  3]

We can look at particular rows of $A$.  For example, if we want the first row we use the following command.

A.row(0) 
       
(2, 3, 4, 1)
(2, 3, 4, 1)

Note that as mentioned above, we start counting at 0 so the first row is given by A.row(0).  The row command attaches to the matrix we operate on, namely, $A$.  This is typical of SAGE functions.  If you want to see the potential commands, type $A.$ and then hit tab.  This will bring up a list of possible commands.  If you are unsure what a command does, type the command followed by a question mark and this will bring up the reference with an example.

The first operation we learned was to exchange rows.  This command is given by swap_rows().  For example, if we want to exchange rows 1 and 2 we do the following:

A.swap_rows(0,1);A 
       
[ 2  4  7 -1]
[ 2  3  4  1]
[ 6  5  3  3]
[ 2  4  7 -1]
[ 2  3  4  1]
[ 6  5  3  3]

The next thing we learned was to multiple a row by a non-zero constant.  This is given by the command rescale_row(i,j)
where $i$ is the row and $j$ is what we are scaling by. For example, if we want to scale the third row by $\frac{1}{3}$ we have the following:

A.rescale_row(2,1/3);A 
       
[  2   4   7  -1]
[  2   3   4   1]
[  2 5/3   1   1]
[  2   4   7  -1]
[  2   3   4   1]
[  2 5/3   1   1]

The next thing we learned was to add one multiple of a row to another row.  This is done with the command add_multiple_of_row(i,j,a). The first argument is the index of the row to be replaced, the second argument is the row to form a multiple of, and the final argument is the scale factor. For example, we want to add (-1) times the first row to the second:

A.add_multiple_of_row(1,0,-1);A 
       
[  2   4   7  -1]
[  0  -1  -3   2]
[  2 5/3   1   1]
[  2   4   7  -1]
[  0  -1  -3   2]
[  2 5/3   1   1]
A.add_multiple_of_row(2,0,-1);A 
       
[   2    4    7   -1]
[   0   -1   -3    2]
[   0 -7/3   -6    2]
[   2    4    7   -1]
[   0   -1   -3    2]
[   0 -7/3   -6    2]

We now proceed to finish putting the matrix in row echelon form with these commands.  We scale the second row by -1:

A.rescale_row(1,-1);A 
       
[   2    4    7   -1]
[   0    1    3   -2]
[   0 -7/3   -6    2]
[   2    4    7   -1]
[   0    1    3   -2]
[   0 -7/3   -6    2]

We now remove the $-\frac{7}{3}$.

A.add_multiple_of_row(2,1,7/3);A 
       
[   2    4    7   -1]
[   0    1    3   -2]
[   0    0    1 -8/3]
[   2    4    7   -1]
[   0    1    3   -2]
[   0    0    1 -8/3]

We now recale the first row, then remove the terms in the $(1,2)$ position, the $(1,3)$ position, and the $(2,3)$ position.

A.rescale_row(0,1/2);A 
       
[   1    2  7/2 -1/2]
[   0    1    3   -2]
[   0    0    1 -8/3]
[   1    2  7/2 -1/2]
[   0    1    3   -2]
[   0    0    1 -8/3]
A.add_multiple_of_row(0,1,-2);A 
       
[   1    0 -5/2  7/2]
[   0    1    3   -2]
[   0    0    1 -8/3]
[   1    0 -5/2  7/2]
[   0    1    3   -2]
[   0    0    1 -8/3]
A.add_multiple_of_row(0,2,5/2);A 
       
[    1     0     0 -19/6]
[    0     1     3    -2]
[    0     0     1  -8/3]
[    1     0     0 -19/6]
[    0     1     3    -2]
[    0     0     1  -8/3]
A.add_multiple_of_row(1,2,-3);A 
       
[    1     0     0 -19/6]
[    0     1     0     6]
[    0     0     1  -8/3]
[    1     0     0 -19/6]
[    0     1     0     6]
[    0     0     1  -8/3]

The matrix is now in reduced row-echelon form, so we can read off the solution as $x = -\frac{19}{6}$, $y = 6$, and $z = -\frac{8}{3}$.  This shows how one would do this calculation by hand.  If we were just entering a matrix into SAGE and wanted to get echelon form, we use the following command.  (Note we must first redefine $A$ to be the matrix we started with.

A=matrix(QQ,[[2,3,4,1],[2,4,7,-1],[6,5,3,3]]);A; 
       
[ 2  3  4  1]
[ 2  4  7 -1]
[ 6  5  3  3]
[ 2  3  4  1]
[ 2  4  7 -1]
[ 6  5  3  3]
A.echelon_form() 
       
[    1     0     0 -19/6]
[    0     1     0     6]
[    0     0     1  -8/3]
[    1     0     0 -19/6]
[    0     1     0     6]
[    0     0     1  -8/3]

We now see how to do this without using matrices at all.  First we must define the variables $x$, $y$, and $z$.

var('x,y,z') 
       
(x, y, z)
(x, y, z)

Next, we define the equations we want to solve.

eqn = [2*x+3*y+4*z==1, 2*x+4*y+7*z==-1, 6*x + 5*y + 3*z==3];eqn 
       
[2*x + 3*y + 4*z == 1, 2*x + 4*y + 7*z == -1, 6*x + 5*y + 3*z == 3]
[2*x + 3*y + 4*z == 1, 2*x + 4*y + 7*z == -1, 6*x + 5*y + 3*z == 3]

 

We now solve the equations, then put them in a nice viewing form.

s=solve(eqn, x,y,z); view(s) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = \left(-\frac{19}{6}\right), y = 6, z = \left(-\frac{8}{3}\right)\right]\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = \left(-\frac{19}{6}\right), y = 6, z = \left(-\frac{8}{3}\right)\right]\right]

This has covered how to do the computations by hand, by reducing the matrix with one command, and how to solve the equations directly with SAGE.  Please feel free to play around with SAGE to get a feel for each of these.  You are welcome to check your answers with SAGE, but you do need to know how to use Gaussian elimination by hand as well!

We also give one application before ending this worksheet.  We use Gaussian elimination to fit a polynomial through a finite collection of points.

Example 2:  Consider the points $(-1,3)$, $(0,0)$, $(1,1)$, and $(4,58)$.  We want to find a degree 3 polynomial that passes through these four points.  Let $$p(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3.$$ 

Our goal is to find $a_0, a_1, a_2,$ and $a_3$ so that this polynomials passes through the four given points.  Suppose that it does.  Then we have the following equations:

$$3 = p(-1) = a_0 - a_1 + a_2 - a_3,$$

$$0 = p(0) = a_0,$$

$$1 = p(1) = a_0 + a_1 + a_2 + a_3,$$

$$58 = p(4) = a_0 + 4 a_1 + 16 a_2 + 64 a_3.$$

We immediately see $a_0 = 0$, so we can eliminate this variable from our equations.  We then set up the augmented matrix to find $a_1, a_2$, and $a_3$.

A= matrix(QQ,[[-1,1,-1,3],[1,1,1,1],[4,16,64,58]]);A 
       
[-1  1 -1  3]
[ 1  1  1  1]
[ 4 16 64 58]
[-1  1 -1  3]
[ 1  1  1  1]
[ 4 16 64 58]
A.echelon_form() 
       
[   1    0    0 -3/2]
[   0    1    0    2]
[   0    0    1  1/2]
[   1    0    0 -3/2]
[   0    1    0    2]
[   0    0    1  1/2]

We used the short-cut here to put $A$ in reduced row-echelon form, but you should make sure you can carry out the process by hand.  The matrix gives us that $a_1 = -\frac{3}{2}$, $a_2 = 2$, and $a_3 = \frac{1}{2}$.  Thus, our polynomial is

$$p(x) = -\frac{3}{2} x + 2x^2 + \frac{1}{2} x^3.$$ 

We can graph the points and polynomial to visually verify what we have found.

P=plot((-3/2)*x + 2*x^2 + (1/2)*x^3, (x,-2,5)); 
       
P+=points([(-1,3), (0,0), (1,1),(4,58)], color='black', pointsize=70, zorder=50) 
       
P.show(axes=True,gridlines=True)