Benoît Legat

Lecture 1 – System of linear equations


Find x1,x2,x3x_1, x_2, x_3 such that:

3x1 + 2x2 x3 = 12x1 2x2 + 4x3 = 22x1 + 1x2 2x3 = 0 \begin{alignat*}{7} 3x_1 &&\; + \;&& 2x_2 &&\; - \;&& x_3 &&\; = \;&& 1 & \\ 2x_1 &&\; - \;&& 2x_2 &&\; + \;&& 4x_3 &&\; = \;&& -2 & \\ -2x_1 &&\; + \;&& 1x_2 &&\; - \;&& 2x_3 &&\; = \;&& 0 & \end{alignat*}
A = [
     3  2 -1
    -2 -2  4
    -2  1 -2
]

A * b

b = [1, -2, 0]

Symbolic approach

using RowEchelon
rref([A b])

Symbolic as amenable to exact arithmetic:

rref(Rational{Int}.([A b]))

How does it work ?

Ab = Float64[A b]
Ab[1, :] /= Ab[1, 1]
Ab

Ab[2, :] -= Ab[2, 1] * Ab[1, :]
Ab[3, :] -= Ab[3, 1] * Ab[1, :]
Ab

Ab[2, :] /= Ab[2, 2]
Ab

Ab[3, :] -= Ab[3, 2] * Ab[2, :]
Ab[1, :] -= Ab[1, 2] * Ab[2, :]
Ab

Ab[3, :] /= Ab[3, 3]
Ab

Ab[1, :] -= Ab[1, 3] * Ab[3, :]
Ab[2, :] -= Ab[2, 3] * Ab[3, :]
Ab

function row_echelon!(A)
    for col in axes(A, 1)
        A[col, :] /= A[col, col]
        for row in axes(A, 1)
            if row != col
                A[row, :] -= A[row, col] * A[col, :]
            end
        end
        display(A)
    end
end

Ab = Float64[A b]
row_echelon!(Ab)
row_echelon!(Ab + 0.1*rand(size(Ab)...))

https://en.wikipedia.org/wiki/Double-precisionfloating-pointformat

https://en.wikipedia.org/wiki/Zeno%27s_paradoxes

num = 1.0
while num > 0
    @show num
    num /= 2
end

Tricky example

Find x1,x2,x3x_1, x_2, x_3 such that:

x1  +  x2  =  2x1    x2  =  0 \begin{alignat*}{7} x_1 &&\; + \;&& x_2 &&\; = \;&& 2 & \\ x_1 &&\; - \;&& x_2 &&\; = \;&& 0 & \\ \end{alignat*}
ε = 2e-10

A = [
    1  1   -1
    1  11
    1 -1    1
]

b = [2, 0, 1]

Ab = [A b]

First column

Ab[2, :] -= Ab[1, :]
Ab[3, :] -= Ab[1, :]
Ab

Second column

Ab[2, :] /= Ab[2, 2]
Ab

Ab[1, :] -= Ab[2, :]
Ab[3, :] += 2Ab[2, :]
Ab

Third column

Ab[3, :] /= Ab[3, 3]
Ab

Ab[1, :] -= Ab[1, 3] * Ab[3, :]
Ab[2, :] -= Ab[2, 3] * Ab[3, :]
Ab

Numerical approach

distance = 16
while distance > 0
    @show distance
    distance /= 2
end

A \ b

ε = 0.0001
(A + ε * rand(size(A)...)) \ (b + ε * rand(size(b)...))

Overdetermined system

Find x1,x2x_1, x_2 such that:

3x1  +  2x2  =  12x1    2x2  =  22x1  +  1x2  =  0 \begin{alignat*}{7} 3x_1 &&\; + \;&& 2x_2 &&\; = \;&& 1 & \\ 2x_1 &&\; - \;&& 2x_2 &&\; = \;&& -2 & \\ -2x_1 &&\; + \;&& 1x_2 &&\; = \;&& 0 & \end{alignat*}
B = A[:, 1:2]

using RowEchelon
rref([B b])

This gives the system

x1      =  0    x2  =  0      =  1 \begin{alignat*}{7} x_1 &&\; \;&& &&\; = \;&& 0 & \\ &&\; \;&& x_2 &&\; = \;&& 0 & \\ &&\; \;&& &&\; = \;&& 1 & \\ \end{alignat*}

The system has no solution.

x = B \ b

e = B * x - b

The system has no solution. We want to minimize the error ee : multi-objective optimization. Reduce to one objective with sum of squares.

minx1,x2e12+e22+e32s.t. e=Bxb \begin{aligned} \min_{x_1,x_2} & \, e_1^2 + e_2^2 + e_3^2\\ \text{s.t. }e = & \, Bx - b \end{aligned}

In general, BRm×nB \in \mathbb{R}^{m \times n}, bRmb \in \mathbb{R}^m:

minxRnBxb22 \begin{aligned} \min_{x \in \mathbb{R}^n} & \|Bx - b\|_2^2 \end{aligned}

where e22=e12+e22+em2\|e\|_2^2 = e_1^2 + e_2^2 + \cdots e_m^2 is the Euclidean norm.

Unconstrained mathematical optimization program:

Optimal solution: solution of BBx=BbB^\top B x = B^\top b. If BBB^\top B is invertible, x=(BB)1Bbx = (B^\top B)^{-1} B^\top b.

B' * e

(B' * B) \ B' * b

Underdetermined system

Find x1,x2,x3x_1, x_2, x_3 such that:

3x1  +  2x2    x3  =  12x1    2x2  +  4x3  =  2 \begin{alignat*}{7} 3x_1 &&\; + \;&& 2x_2 &&\; - \;&& x_3 &&\; = \;&& 1 & \\ 2x_1 &&\; - \;&& 2x_2 &&\; + \;&& 4x_3 &&\; = \;&& -2 & \\ \end{alignat*}
C = A[1:2, :]

c = b[1:2]

using RowEchelon
rref([C c])

This gives the system

x1      +  0.6x3  =  0.2    x2    1.4x3  =  0.8 \begin{alignat*}{7} x_1 &&\; \;&& &&\; + \;&& 0.6x_3 &&\; = \;&& -0.2 & \\ &&\; \;&& x_2 &&\; - \;&& 1.4x_3 &&\; = \;&& 0.8 & \\ \end{alignat*}

The system has infinitely solutions: It has a solution (0.20.6x3,0.8+1.4x3)(-0.2 - 0.6x_3, 0.8 + 1.4x_3) for any x3x_3.

x = C \ c

C * x - c

Suppose we want to minimize x2\|x\|_2:

minxRnx2s.t. Cx=c \begin{aligned} \min_{x \in \mathbb{R}^n}& \|x\|_2\\ \text{s.t. }Cx & = c \end{aligned}

Mathematical optimization program:

Optimal solution: x=Cλx = C^\top \lambda where λ\lambda is a solution of CCλ=cCC^\top \lambda = c. If CCCC^\top is invertible, x=C(CC)1cx = C^\top (CC^\top)^{-1} c.

C \ c

C' * ((C * C') \ c)

This page was generated using Literate.jl.