Notebook 1 – Math 2121, Fall 2020

This is a Pluto notebook, using the Julia programming language.

Julia is at the cutting edge of programming languages for scientific computing.

It combines many of the best features of Matlab and Python but is also very fast. The language has a lot of built-in functions that will be useful for demonstrating concepts in Math 2121.

1.9 ms

Installing Pluto (optional)

Pluto is an interactive notebook for running Julia code.

You can access this notebook as a static HTML page on our course website. But if you install Julia and Pluto on your home computer, then you can also modify and run the code here yourself.

I can't improve upon the instructions at this link for installing both:

https://mitmath.github.io/18S191/Fall20/installation/

2.4 ms

Running this Pluto notebook

Once you have Pluto up and running, you can access the notebook you we are currently viewing by entering this link (right click -> Copy Link) in the Open from file menu in Pluto.

17 μs

Working with matrices

The syntax for working with matrices in Julia is much like Matlab.

3.8 μs
Y
3×5 Array{Int64,2}:
 1  2  3   0    6
 4  1  6  -1    8
 7  8  1   2  200
26.9 ms
-1
5.2 ms

Make it interactive

row_index = 1

col_index = 1

117 ms
3×5 Array{Int64,2}:
 1  2  3   0    6
 4  1  6  -1    8
 7  8  1   2  200
585 ns
1
3.6 μs
3.8 ms
3×5 Array{Int64,2}:
 1  2  3   0    6
 4  1  6  60    8
 7  8  1   2  200
916 ns
2×2 Array{Int64,2}:
 3   0
 6  60
56.4 ms
1×2 Array{Int64,2}:
 3  0
5.2 μs
3×1 Array{Int64,2}:
  0
 60
  2
7.7 μs
15.3 μs
Z
3×4 Array{Float64,2}:
 1.1                2.2               3.0   0.0
 3.141592653589793  2.23606797749979  6.0  -1.0
 7.0                8.0               1.0   2.0
77.2 ms

Later in the course we will see many other operations involving matrices. Most of these will also be easily available in the Julia programming language.

2.3 μs

Augmented matrices

We can write our own functions to work with matrices.

(This requires some programming background, however.)

6.3 μs

The next cell implements a function called print_linear_system which takes a single input parameter. You can probably guess what it does.

(To see the code, click the Show/Hide icon on the left.)

3.9 μs
print_linear_system (generic function with 1 method)
68 μs

 x_1 + 2 x_2 + 3 x_3 = 6

 4 x_1 + x_2 + 6 x_3 + 60 x_4 = 8

 7 x_1 + 8 x_2 + x_3 + 2 x_4 = 200

100 ms

 0 = 0

 0 = 60

 0 = 2

22.1 μs

 4 x_1 + x_2 + 6 x_3 = 60

 7 x_1 + 8 x_2 + x_3 = 2

22.9 μs

Here's another function (code hidden) that returns true or false depending on whether a given list s is a solution to the linear system with augemented matrix A

13.4 μs
is_solution (generic function with 2 methods)
37.2 μs
T
3×4 Array{Int64,2}:
 1  -2   1   0
 0   2  -8   8
 5   0  -5  10
9.4 μs

 x_1 - 2 x_2 + x_3 = 0

 2 x_2 - 8 x_3 = 8

 5 x_1 - 5 x_3 = 10

18.7 μs
U
3×4 Array{Int64,2}:
 1  -2   1   0
 0   1  -4   4
 0   0   1  -1
6.3 μs

 x_1 - 2 x_2 + x_3 = 0

 x_2 - 4 x_3 = 4

 x_3 = -1

20.6 μs
true
224 ms
true
6.9 μs

Row operations

A handy feature of these notebooks is that we can very easily explore different ranges of inputs and parameters. Let's try this out with our elementary row operations.

5.9 μs

Set some parameters

a = b = c = p =

d = e = f = q =

g = h = i = r =

11.5 ms
A
3×4 Array{Int64,2}:
 1  2   3  -1
 0  0   0  -2
 3  8  12   0
11.3 μs

 x_1 + 2 x_2 + 3 x_3 = -1

 0 = -2

 3 x_1 + 8 x_2 + 12 x_3 = 0

18.7 μs

Below are functions implementing our three row operations. Their syntax is:

  • rowop_replace(A, i, j, v): adds v times row i in matrix A to row j

  • rowop_scale(A, i, v): multiplies row i in matrix A by nonzero constant v

  • rowop_swap(A, i, j): swaps rows i and j in matrix A

Each function returns a new matrix without modifying the matrix A.

6.2 μs
rowop_replace (generic function with 1 method)
57.4 μs
rowop_scale (generic function with 1 method)
49.4 μs
rowop_swap (generic function with 1 method)
35.7 μs

Another parameter

v = 1

77.8 μs
3×4 Array{Int64,2}:
 1  2   3  -1
 0  0   0  -2
 3  8  12   0
1.4 μs
3×4 Array{Int64,2}:
 1   2   3  -1
 0   0   0  -2
 4  10  15  -1
20 ms
true
8.8 ms
3×4 Array{Int64,2}:
 1  2   3  -1
 0  0   0  -2
 3  8  12   0
16.8 ms
true
18 ms
3×4 Array{Int64,2}:
 1  2   3  -1
 3  8  12   0
 0  0   0  -2
18.4 ms
true
8.5 μs

Solving a linear system by row operations

By applying a sequence of row operations, we can transform the augmented matrix of a linear system to a simpler matrix, whose associated system has the same solutions as the one we started with.

6.7 μs
M
3×4 Array{Int64,2}:
 1  2   3  -1
 0  9   0  -2
 3  8  12   0
9.6 μs

 x_1 + 2 x_2 + 3 x_3 = -1

 9 x_2 = -2

 3 x_1 + 8 x_2 + 12 x_3 = 0

34.2 μs
B
3×4 Array{Int64,2}:
 1  2  3  -1
 0  9  0  -2
 0  2  3   3
5.1 μs
C
3×4 Array{Int64,2}:
 1  0  0  -4
 0  9  0  -2
 0  2  3   3
8.1 μs
D
3×4 Array{Int64,2}:
 1  0    0   -4
 0  1  -12  -14
 0  2    3    3
4.9 μs
E
3×4 Array{Int64,2}:
 1  0    0   -4
 0  1  -12  -14
 0  0   27   31
4.2 μs
316 ns
F
3×4 Array{Float64,2}:
 1.0  0.0    0.0   -4.0
 0.0  1.0  -12.0  -14.0
 0.0  0.0   27.0   31.0
99 ms
G
3×4 Array{Float64,2}:
 1.0  0.0    0.0   -4.0
 0.0  1.0  -12.0  -14.0
 0.0  0.0    1.0    1.1481481481481481
16.5 ms
H
3×4 Array{Float64,2}:
 1.0  0.0  0.0  -4.0
 0.0  1.0  0.0  -0.22222222222222143
 0.0  0.0  1.0   1.1481481481481481
17.8 ms

 x_1 = -4.0

 x_2 = -0.22222222222222143

 x_3 = 1.1481481481481481

149 ms
sol
41.8 ms
true
225 ms
true
148 ms