Section 1.2 - Systems of Linear Equations PDF
Section 1.2 - Systems of Linear Equations PDF
Theorem:
Ax = b.
1. has no solution;
2. has exactly one solution;
3. has infinitely many solutions.
AB = BA = I,
where I is the identity matrix, then we say that B is the inverse of A, and we write
B = A −1 .
Theorem:
1. A−1 exists.
2. There is no nonzero y such that Ay = 0.
3. The columns of A are linearly independent.
4. The rows of A are linearly independent.
5. det( A) 6= 0.
6. For any b ∈ Rn , there is exactly one x ∈ Rn such that Ax = b.
If A is nonsingular, then
1
Ax = b =⇒ A−1 Ax = A−1 b (1)
=⇒ Ix = A−1 b (2)
=⇒ x = A −1 b . (3)
(4)
Out[1]:
inv(M)
Matrix inverse. Computes matrix N such that M * N = I, where I is the identity matrix. Com-
puted by solving the left-division N = M \ I.
Example
julia> M = [2 5; 1 3]
2×2 Array{Int64,2}:
2 5
1 3
julia> N = inv(M)
2×2 Array{Float64,2}:
3.0 -5.0
-1.0 2.0
In [2]: ?eye
Out[2]:
2
eye([T::Type=Float64,] m::Integer, n::Integer)
eye(m, n)
eye([T::Type=Float64,] n::Integer)
eye(A)
julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> eye(A)
3×3 Array{Int64,2}:
1 0 0
0 1 0
0 0 1
In [3]: n = 5
A = rand(n, n)
In [4]: B = inv(A)
3
In [5]: C = [1.0 1.0; 1 1.0]
inv(C)
Base.LinAlg.SingularException(2)
Stacktrace:
In [6]: A*B
In [7]: I = eye(n)
In [8]: A*B - I
In [9]: norm(A*B - I)
Out[9]: 9.332729843012912e-16
4
The \ command
In Julia or MATLAB, the linear system Ax = b is solve using the "backslash" command: \.
In [10]: ?\
search: \ .\
Out[10]:
\(x, y)
Left division operator: multiplication of y by the inverse of x on the left. Gives floating-point
results for integer arguments.
julia> 3 \ 6
2.0
julia> inv(3) * 6
2.0
julia> A \ x
2-element Array{Float64,1}:
-4.0
4.5
julia> inv(A) * x
2-element Array{Float64,1}:
-4.0
4.5
\(A, B)
Matrix division using a polyalgorithm. For input matrices A and B, the result X is such that
A*X == B when A is square. The solver that is used depends upon the structure of A. If A is upper
or lower triangular (or diagonal), no factorization of A is required and the system is solved with
either forward or backward substitution. For non-triangular square matrices, an LU factorization
is used.
For rectangular A the result is the minimum-norm least squares solution computed by a piv-
oted QR factorization of A and a rank estimate of A based on the R factor.
When A is sparse, a similar polyalgorithm is used. For indefinite matrices, the LDLt factoriza-
tion does not use pivoting during the numerical factorization and therefore the procedure can fail
even for invertible matrices.
5
Example
julia> A = [1 0; 1 -2]; B = [32; -4];
julia> X = A \ B
2-element Array{Float64,1}:
32.0
18.0
julia> A * X == B
true
In [11]: n = 5
A = rand(n, n)
x = rand(n)
In [12]: b = A*x
In [13]: A*x - b
In [14]: det(A)
Out[14]: -0.008517080142206717
6
Out[15]: 5-element Array{Float64,1}:
0.968922
0.210697
0.700709
0.576107
0.328604
In [16]: A*x - b
A*x - b
A = rand(n, n)
x = rand(n)
b = A*x;
7
0.572811 seconds (14 allocations: 68.711 MiB, 15.62% gc time)
|A*x - b| = 7.392862e-11
The winner: \
Never use inv to solve a linear system.
1
h= and ti = ih, i = 0, 1, . . . , m.
m
For h small,
x ( ti + h ) − x ( ti − h ) x ( t i +1 ) − x ( t i −1 )
x 0 ( ti ) ≈ =
2h 2h
and
x (ti+1 ) − 2x (ti ) + x (ti−1 )
x 00 (ti ) ≈ .
h2
Let xi ≈ x (ti ) and substitute into the differential equation:
Ax = b
where x = ( x0 , x1 , . . . , xm ) and
1 0 2
m2 + m −2m2 + 1 m2 − m 2/m
A= . .. . .. . .. b= ..
, .
.
2 2 2
m + m −2m + 1 m − m 2(m − 1)/m
−m m −4
8
Notice that A is a tridiagonal matrix.
In [21]: m = 10
e = ones(m-1)
dm1 = [(m^2 + m)*e; -m]
d0 = [1; (-2m^2 + 1)*e; m]
dp1 = [0; (m^2 - m)*e]
@time x = A\b;
In [22]: A
t = linspace(0, 1, m+1)
plot(t, x, label=L"x")
plot(t, xtrue, label=L"x_\mathrm{true}")
xlim([0, 1])
ylim([0, 2])
legend();
9
WARNING: exp{T <: Number}(x::AbstractArray{T}) is deprecated, use exp.(x) instead.
Stacktrace:
[1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
[2] exp(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}) at .
[3] include_string(::String, ::String) at ./loading.jl:515
[4] include_string(::Module, ::String, ::String) at /Users/yunier/.julia/v0.6/Compat/src/Compat
[5] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/yunier/.julia/v0.6/IJulia/src/execute
[6] (::Compat.#inner#17{Array{Any,1},IJulia.#execute_request,Tuple{ZMQ.Socket,IJulia.Msg}})() a
[7] eventloop(::ZMQ.Socket) at /Users/yunier/.julia/v0.6/IJulia/src/eventloop.jl:8
[8] (::IJulia.##14#17)() at ./task.jl:335
while loading In[23], in expression starting on line 5
WARNING:
10
[5] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/yunier/.julia/v0.6/IJulia/src/execute
[6] (::Compat.#inner#17{Array{Any,1},IJulia.#execute_request,Tuple{ZMQ.Socket,IJulia.Msg}})() a
[7] eventloop(::ZMQ.Socket) at /Users/yunier/.julia/v0.6/IJulia/src/eventloop.jl:8
[8] (::IJulia.##14#17)() at ./task.jl:335
while loading In[23], in expression starting on line 15
11