Notebook 3 – Math 2121, Fall 2020
In today's notebook we'll try to get some feeling for vectors in
xxxxxxxxxx
begin
using PlutoUI
using Plots
using LinearAlgebra
# Returns true if row has all zeros.
function is_zero_row(A, row, tolerance=10e-16)
(m, n) = size(A)
return all([abs(A[row,col]) < tolerance for col=1:n])
end
# Returns first index of nonzero entry in row, or 0 if none exists.
function find_leading(A, row=1, tolerance=10e-16)
(m, n) = size(A)
for col=1:n
if abs(A[row, col]) > tolerance
return col
end
end
return 0
end
function find_nonzeros_in_column(A, row_start, col, tol=10e-12)
(m, n) = size(A)
return [i for i=row_start:m if abs(A[i,col]) > tol]
end
function columns(A)
(m, n) = size(A)
return n
end
function rowop_replace(A, source_row, target_row, scalar_factor)
source_row != target_row
for j=1:columns(A)
A[target_row,j] += A[source_row,j] * scalar_factor
end
return A
end
function rowop_scale(A, target_row, scalar_factor)
scalar_factor != 0
for j=1:columns(A)
A[target_row,j] *= scalar_factor
end
A
end
function rowop_swap(A, s, t)
for j=1:columns(A)
A[t,j], A[s,j] = A[s,j], A[t,j]
end
A
end
function RREF_with_rowop_count(A, tol=10e-12)
rowop_count = 0
A = float(copy(A))
(m, n) = size(A)
row = 1
for col=1:n
nonzeros = find_nonzeros_in_column(A, row, col, tol)
if length(nonzeros) == 0
continue
end
i = nonzeros[1]
if abs(A[i, col] - 1) < tol
A[i, col] = 1
else
A = rowop_scale(A, i, 1 / A[i, col])
rowop_count += 1
end
for j=nonzeros[2:end]
A = rowop_replace(A, i, j, -A[j, col])
rowop_count += 1
end
if i != row
A = rowop_swap(A, row, i)
rowop_count += 1
end
row += 1
end
for row=m:-1:1
l = find_leading(A, row, tol)
if l > 0
for k=1:row - 1
if abs(A[k,l]) > tol
rowop_replace(A, row, k, -A[k,l])
rowop_count += 1
end
end
end
end
# round entries to give nicer output; could omit this step
for row=1:m
for col=1:n
if abs(A[row,col] - round(A[row,col])) < tol
A[row,col] = round(A[row,col])
end
end
end
return A, rowop_count
end
function RREF_RowOpCount(A, tol=10e-12)
A, ops = RREF_with_rowop_count(A, tol)
return ops
end
function RREF(A, tol=10e-12)
A, ops = RREF_with_rowop_count(A, tol)
return A
end
md"# Notebook 3 -- Math 2121, Fall 2020
In today's notebook we'll try to get some feeling for vectors in $\mathbb{R}^2$ and $\mathbb{R}^3$ and their linear combinations. We'll also think about what a solution to a vector equation means. The code here involves a lot of new plotting functions in Julia."
end
Running this notebook (optional)
If you have Pluto up and running, you can access the notebook we are currently viewing by entering this link (right click -> Copy Link) in the Open from file menu in Pluto.
xxxxxxxxxx
Vectors in
Let's start out by drawing some vectors in
xxxxxxxxxx
md"## Vectors in $\mathbb{R}^2$
Let's start out by drawing some vectors in $\mathbb{R}^2$."
Create some vectors in
u1
= v1
= w1
=
u2
= v2
= w2
=
x
begin
u1_slider = u1 Slider(-10:0.1:10, default=4, show_value=false)
u2_slider = u2 Slider(-10:0.1:10, default=8, show_value=false)
v1_slider = v1 Slider(-10:0.1:10, default=-10, show_value=false)
v2_slider = v2 Slider(-10:0.1:10, default=6, show_value=false)
w1_slider = w1 Slider(-10:0.1:10, default=2, show_value=false)
w2_slider = w2 Slider(-10:0.1:10, default=-9, show_value=false)
md"""**Create some vectors in $$\mathbb{R}^2$$**
`u1` = $(u1_slider) `v1` = $(v1_slider) `w1` = $(w1_slider)
`u2` = $(u2_slider) `v2` = $(v2_slider) `w2` = $(w2_slider)
"""
end
The vectors we created:
x
md"The vectors we created:"
u = [ 6.7] v = [ -5] w = [ 10]
[-3.7] [-5.1] [ -10]
xxxxxxxxxx
Display: u
v
w
xxxxxxxxxx
md"""**Display**:
`u` $(@bind uon html"<input type=checkbox >") `v` $(@bind von html"<input type=checkbox >") `w` $(@bind won html"<input type=checkbox >")
"""
xxxxxxxxxx
begin
lims = (
-(maximum([1, uon + von + won])) * 10,
maximum([1, uon + von + won]) * 10
)
p1 = scatter([0], [0], xlims = lims, ylims = lims,
legend = false, label = "origin", aspect_ratio=:equal,
title = "Vectors u, v, w")
if uon
quiver!(quiver = ([u1],[u2]), [0], [0], color = [:blue],)
end
if von
quiver!(quiver = ([v1],[v2]), [0], [0], color = [:orange],)
end
if won
quiver!(quiver = ([w1],[w2]), [0], [0], color = [:green],)
end
p2 = scatter([0], [0], xlims = lims, ylims = lims,
legend = false, label = "origin", aspect_ratio=:equal,
title = "Sum of vectors")
quiver!(
quiver = ([uon*u1 + von*v1 + won*w1],[uon*u2 + von*v2 + won*w2]),
[0], [0], color = [:black],
)
quiver!(
quiver = ([uon*u1],[uon*u2]),
[0], [0],
color = [:blue],
linestyle = :dashdot,
)
quiver!(
quiver = ([von*v1],[von*v2]),
[uon*u1], [uon*u2],
color = [:orange],
linestyle = :dashdot,
)
quiver!(
quiver = ([won*w1],[won*w2]),
[uon*u1 + von*v1], [uon*u2 + von*v2],
color = [:green],
linestyle = :dashdot,
)
plot(p1, p2, layout=2)
end
The vector
The right hand graph shows the sum of the displayed vectors on the left as the solid arrow. The dotted arrows are just copies of u, v, and w moved around.
x
md"The vector $u$ is shown in blue, $v$ in orange, and $w$ in green.
The right hand graph shows the sum of the displayed vectors on the left as the solid arrow. The dotted arrows are just copies of u, v, and w moved around."
Linear combinations
Now that we have chosen some vectors
x
md"## Linear combinations
Now that we have chosen some vectors $u$, $v$, $w$, let's think about the meaning of the linear combination $au + bv + cw$ where $a,b,c \in \mathbb{R}$ are scalars."
Choose some scalars in
a
= b
= c
=
x
begin
a_slider = a Slider(-10:0.1:10, default=1, show_value=false)
b_slider = b Slider(-10:0.1:10, default=1, show_value=false)
c_slider = c Slider(-10:0.1:10, default=1, show_value=false)
md"""**Choose some scalars in $$\mathbb{R}$$**
`a` = $(a_slider) `b` = $(b_slider) `c` = $(c_slider)
"""
end
The scalars we chose:
xxxxxxxxxx
md"The scalars we chose:"
a = -1.4 b = -2.1 c = 0
xxxxxxxxxx
xxxxxxxxxx
begin
p3 = scatter([0], [0], xlims = lims, ylims = lims,
legend = false, label = "origin", aspect_ratio=:equal,
title = "a•u, b•v, and c•w")
if uon
quiver!(quiver = ([a*u1],[a*u2]), [0], [0], color = [:blue],)
end
if von
quiver!(quiver = ([b*v1],[b*v2]), [0], [0], color = [:orange],)
end
if won
quiver!(quiver = ([c*w1],[c*w2]), [0], [0], color = [:green],)
end
p4 = scatter([0], [0], xlims = lims, ylims = lims,
legend = false, label = "origin", aspect_ratio=:equal,
title = "a•u + b•v + c•w")
quiver!(
quiver = (
[a*uon*u1 + b*von*v1 + c*won*w1],
[a*uon*u2 + b*von*v2 + c*won*w2]
),
[0], [0], color = [:black],
)
quiver!(
quiver = ([a*uon*u1],[a*uon*u2]),
[0], [0],
color = [:blue],
linestyle = :dashdot,
)
quiver!(
quiver = ([b*von*v1],[b*von*v2]),
[a*uon*u1], [a*uon*u2],
color = [:orange],
linestyle = :dashdot,
)
quiver!(
quiver = ([c*won*w1],[c*won*w2]),
[a*uon*u1 + b*von*v1], [a*uon*u2 + b*von*v2],
color = [:green],
linestyle = :dashdot,
)
plot(p3, p4, layout=2)
end
Varying
If
The same thing happens to the arrows representing
xxxxxxxxxx
md"Varying $a$ among positive values scales the length of the arrow that represents the vector $u$.
If $a$ is negative then this scalaing reverses the direction of $u$.
The same thing happens to the arrows representing $v$ and $w$ when we change $b$ and $c$."
Vector equations
Let's now think of
Choose a target vector
What does a solution to the equation
We know that the solutions
xxxxxxxxxx
md"## Vector equations
Let's now think of $a,b,c$ as the variables $x_1=a$ and $x_2=b$ and $x_3=c$.
Choose a target vector $t =\left[\begin{array}{l} t_1 \\ t_2\end{array}\right] \in \mathbb{R}^2$.
What does a solution to the equation $x_1 u + x_2 v + x_3 w = t$ mean?
We know that the solutions $x = \left[\begin{array}{l} x_1 \\ x_2 \\ x_3\end{array}\right]$ to this equation are the same as the solutions to the linear system whose augmented matrix is
$$A = \left[\begin{array}{ccc|c} u_1 & v_1 & w_1 & t_1 \\ u_2 & v_2 & w_2 & t_2 \end{array}\right]$$."
Let's create a random target vector
xxxxxxxxxx
md"Let's create a random target vector $t$."
2×1 Array{Float64,2}:
1.0
15.4
xxxxxxxxxx
target = rand(-20:0.1:20, 2, 1)
Here's our coefficient matrix.
xxxxxxxxxx
md"Here's our coefficient matrix."
2×3 Array{Float64,2}:
6.7 -5.0 10.0
-3.7 -5.1 -10.0
coefficient_matrix = [u1 v1 w1; u2 v2 w2]
Here's our augmented matrix.
xxxxxxxxxx
md"Here's our augmented matrix."
2×4 Array{Float64,2}:
6.7 -5.0 10.0 1.0
-3.7 -5.1 -10.0 15.4
augmented_matrix = hcat(coefficient_matrix, target)
To solve the corresponding linear system we compute RREF(augmented_matrix)
.
x
md"To solve the corresponding linear system we compute `RREF(augmented_matrix)`."
2×4 Array{Float64,2}:
1.0 0.0 1.917600151889121 -1.3651034744636414
0.0 1.0 0.5695842035314223 -2.02923865578128
xxxxxxxxxx
rref = RREF(augmented_matrix)
For typical values of
This means
To get a solution, choose any value for
x
md"For typical values of $t$, the pivot positions are (1,1) and (2,2).
This means $x_1$ and $x_2$ are basic variables and $x_3$ is a free variable.
To get a solution, choose any value for $x_3$, then solve for $x_1$, $x_2$."
0
x
free_variable_value = 0
-1.3651
-2.02924
0
xxxxxxxxxx
solution = (
rref[1,4] - free_variable_value * rref[1,3],
rref[2,4] - free_variable_value * rref[2, 3],
free_variable_value
)
The meaninng of this solution is that if we set
We can see this visually below.
xxxxxxxxxx
md"The meaninng of this solution is that if we set $x_1$, $x_2$, and $x_3$ to the corresponding values, then the linear combination $x_1 u + x_2 v + x_3 w$ should match up with the target vector $t$.
We can see this visually below."
x1
= x2
= x3
=
xxxxxxxxxx
begin
md"""
`x1` = $(a_slider) `x2` = $(b_slider) `x3` = $(c_slider)
"""
end
x1 = -1.4 x2 = -2.1 x3 = 0
xxxxxxxxxx
Text(join([string("x1 = ", a), string("x2 = ", b), string("x3 = ", c)], " "))
xxxxxxxxxx
begin
p5 = scatter([0 target[1]], [0 target[2]], xlims = (-20,20), ylims = (-20,20),
legend = true, label = ["origin" "target"],
# aspect_ratio=:equal,
title = "x1•u + x2•v + x3•w = ?",
)
quiver!(
quiver = ([a*uon*u1 + b*von*v1 + c*won*w1],[a*uon*u2 + b*von*v2 + c*won*w2]),
[0], [0], color = [:black],
)
quiver!(
quiver = ([a*uon*u1],[a*uon*u2]),
[0], [0],
color = [:blue],
linestyle = :dashdot,
)
quiver!(
quiver = ([b*von*v1],[b*von*v2]),
[a*uon*u1], [a*uon*u2],
color = [:orange],
linestyle = :dashdot,
)
quiver!(
quiver = ([c*won*w1],[c*won*w2]),
[a*uon*u1 + b*von*v1], [a*uon*u2 + b*von*v2],
color = [:green],
linestyle = :dashdot,
)
plot(p5)
end
Vectors in
Let's also try to visualize vectors in
This is a little more challenging to display.
x
md"## Vectors in $\mathbb{R}^3$
Let's also try to visualize vectors in $\mathbb{R}^3$ and think about their span.
This is a little more challenging to display."
Display parameters (changing these rotates our view):"
phi
=
psi
=
xxxxxxxxxx
begin
p_slider = _phi Slider(-360:12:360, default=0, show_value=true)
q_slider = _psi Slider(-360:12:360, default=0, show_value=true)
md"""**Display parameters** (changing these rotates our view):"
`phi` = $(p_slider)
`psi` = $(q_slider)
"""
end
Create some vectors in
v1
= w1
=
v2
= w2
=
v3
= w3
=
xxxxxxxxxx
begin
a1_slider = a1 Slider(0:1:20, default=4, show_value=false)
a2_slider = a2 Slider(0:1:20, default=8, show_value=false)
a3_slider = a3 Slider(0:1:20, default=0, show_value=false)
b1_slider = b1 Slider(0:1:20, default=10, show_value=false)
b2_slider = b2 Slider(0:1:20, default=6, show_value=false)
b3_slider = b3 Slider(0:1:20, default=0, show_value=false)
md"""**Create some vectors in $$\mathbb{R}^3$$**
`v1` = $(a1_slider) `w1` = $(b1_slider)
`v2` = $(a2_slider) `w2` = $(b2_slider)
`v3` = $(a3_slider) `w3` = $(b3_slider)
"""
end
The vectors we created:
xxxxxxxxxx
md"The vectors we created:"
v = [ 9] w = [ 0]
[ 0] [ 20]
[ 5] [ 7]
xxxxxxxxxx
show shadows in
x
md"""show shadows in $xy$-plane:
$(@bind nothide html"<input type=checkbox >")
"""
draw span:
x
md"""draw span:
$(@bind span html"<input type=checkbox >")
"""
xxxxxxxxxx
begin
phi = (_phi - 90) / 360 * pi
psi = (_psi - 270) / 360 * pi
A = [ sin(psi) -cos(psi) 0
cos(phi) * cos(psi) cos(phi) * sin(psi) -sin(phi)]
md""
end
x
begin
nlims = (-20,20)
p6 = scatter([0], [0],
legend = false,
label = "origin",
aspect_ratio=:equal,
grid=nothing,
axis=nothing,
foreground_color_subplot="white",
)
for i=6:6:60
quiver!(
quiver = ([100 * A[1,2]], [100 * A[2,2]]),
[i * A[1,1]], [i * A[2,1]],
color=[:lightgray],
linestyle = :dashdot,
)
quiver!(
quiver = ([100 * A[1,1]], [100 * A[2,1]]),
[i * A[1,2]], [i * A[2,2]],
color=[:lightgray],
linestyle = :dashdot,
)
end
quiver!(
quiver = (100 * A[1:1,1:3], 100 * A[2:2,1:3]),
[0, 0, 0],
[0, 0, 0],
xlims = nlims,
ylims = nlims,
color = [:grey],
)
if nothide
x = A * [a1; a2; 0]
quiver!(
quiver = ([x[1]], [x[2]]), [0], [0],
color = [:blue],
linestyle = :dashdot,
)
y = A * [b1; b2; 0]
quiver!(
quiver = ([y[1]], [y[2]]), [0], [0],
color = [:orange],
linestyle = :dashdot,
)
end
x = A * [a1; a2; a3]
y = A * [b1; b2; b3]
if norm(x) != 0
nx = x / norm(x)
else
nx = x
end
if norm(y) != 0
ny = y / norm(y)
else
ny = y
end
if span
for i=0:20
for j=0:20
scatter!(
[i * nx[1] + j * ny[1]], [i * nx[2] + j * ny[2]],
color=[:lightgray],
)
end
end
end
quiver!(quiver = ([x[1]], [x[2]]), [0], [0], color = [:blue],)
quiver!(quiver = ([y[1]], [y[2]]), [0], [0], color = [:orange],)
p7 = scatter([0], [0],
legend = false,
label = "origin",
aspect_ratio=:equal,
grid=nothing,
axis=nothing,
foreground_color_subplot="white"
)
for i=6:6:60
quiver!(
quiver = ([100 * A[1,2]], [100 * A[2,2]]),
[i * A[1,1]], [i * A[2,1]],
color=[:lightgray],
linestyle = :dashdot,
)
quiver!(
quiver = ([100 * A[1,1]], [100 * A[2,1]]),
[i * A[1,2]], [i * A[2,2]],
color=[:lightgray],
linestyle = :dashdot,
)
end
quiver!(
quiver = (100 * A[1:1,1:3], 100 * A[2:2,1:3]),
[0, 0, 0],
[0, 0, 0],
xlims = nlims,
ylims = nlims,
color = [:grey],
)
if nothide
x = A * [a1; a2; 0]
y = A * [b1; b2; 0]
quiver!(
quiver = ([x[1]], [x[2]]), [0], [0],
color = [:grey],
linestyle = :dashdot,)
quiver!(
quiver = ([y[1]], [y[2]]), [x[1]], [x[2]],
color = [:grey],
linestyle = :dashdot,)
quiver!(
quiver = ([x[1] + y[1]], [x[2] + y[2]]), [0], [0],
color = [:grey])
end
x = A * [a1; a2; a3]
y = A * [b1; b2; b3]
quiver!(
quiver = ([x[1]], [x[2]]), [0], [0],
color = [:blue],
linestyle = :dashdot)
quiver!(
quiver = ([y[1]], [y[2]]), [x[1]], [x[2]],
color = [:orange],
linestyle = :dashdot)
quiver!(
quiver = ([x[1] + y[1]], [x[2] + y[2]]), [0], [0],
color = [:black])
plot(p6, p7, layout=2)
end
The left picture shows that vectors
The right picture shows their sum.
We can also display the span of the two vectors, denoted
For different inputs, we can get
xxxxxxxxxx
md"The left picture shows that vectors $v$ and $w$.
The right picture shows their sum.
We can also display the span of the two vectors, denoted $\mathbb{R}\text{-span}\{v,w\}$.
For different inputs, we can get $\mathbb{R}\text{-span}\{v,w\}$ to be either a plane, a line, or a single point."
Some remarks
Of course, these pictures are not actually in
On a flat surface like this screen, we can only draw vectors in
Secretly, we are drawing many vectors
x
md"## Some remarks
Of course, these pictures are not actually in $\mathbb{R}^3$!
On a flat surface like this screen, we can only draw vectors in $\mathbb{R}^2$.
Secretly, we are drawing many vectors $\mathbb{R}^2$ to simulate the appearance of $\mathbb{R}^3$. If we erase those extra vectors and display the usual axes, the plots below are what we are actually showing."
x
begin
p8 = scatter([0], [0],
legend = false,
label = "origin",
aspect_ratio=:equal,
xlims = nlims,
ylims = nlims,
)
quiver!(quiver = ([x[1]], [x[2]]), [0], [0], color = [:blue],)
quiver!(quiver = ([y[1]], [y[2]]), [0], [0], color = [:orange],)
p9 = scatter([0], [0],
legend = false,
label = "origin",
aspect_ratio=:equal,
xlims = nlims,
ylims = nlims,
)
quiver!(
quiver = ([x[1]], [x[2]]), [0], [0],
color = [:blue],
linestyle = :dashdot)
quiver!(
quiver = ([y[1]], [y[2]]), [x[1]], [x[2]],
color = [:orange],
linestyle = :dashdot)
quiver!(
quiver = ([x[1] + y[1]], [x[2] + y[2]]), [0], [0],
color = [:black])
plot(p8, p9, layout=2)
end
How are these vectors in
Some kind of transformation is used to change vectors in
x
md"How are these vectors in $\mathbb{R}^2$ computed from $v$ and $w$, which we chose to be in $\mathbb{R}^3$?
Some kind of transformation is used to change vectors in $\mathbb{R}^3$ to vectors in $\mathbb{R}^2$ that we can display. We'll talk about these kinds of transformations next lecture."