Linear Algebra
Linear Algebra
Vectors
A vector is an ordered set of scalars. We mostly used column vectors (n rows, 1 column) by
convention. Vector dimension equals the number of elements. Geometrically, a vector represents
a point in space. Vectors have both direction and norm (magnitude)
A = [1;2]
Rank:
Maximum number of linear independent rows/columns: The rank tells you how many of these
vectors are truly "going in different directions" and are not just combinations of the others.
Condition Number
Ratio of largest to smallest eigenvalue of a matrix. High condition number indicates an ill-
conditioned matrix. Ill-conditioned matrices meaning small changes in input cause large changes
in output.
Whereas,
Where:
𝑥₁, 𝑥₂, 𝑥₃ are the variables.
aᵢⱼ represents the coefficient of the j-th variable (xⱼ) in the i-th equation.
bᵢ represents the constant term in the i-th equation.
Outer loop (k): Iterates through each pivot position from the first row to the second-to-last
row (k = 1 to n-1). We stop at n-1 because the last row doesn't need any rows below it to be
eliminated.
Inner loop (i): For each pivot position k, we iterate through all rows below it (i = k+1
to n). This loop creates zeros beneath each pivot element.
Elimination factor: We calculate factor = Ab(i,k) / Ab(k,k) which is the factor needed
to make the element at position (i,k) zero.
Row operation: We replace row i by subtracting factor times row k from it:
Ab(i,:) = Ab(i,:) - factor * Ab(k,:).
This creates a zero at position (i,k) while maintaining the system's equivalence. This process
systematically transforms the augmented matrix Ab into row echelon form, allowing us to solve
the system using back substitution.
𝐴11 𝐴12 𝐴13 𝑏1
𝐴𝑏 = [ 0 𝐴22 𝐴23 𝑏2 ]
0 0 𝐴33 𝑏3
Converted into Ax=b form
𝐴11 𝐴12 𝐴13 𝑥1 𝑏1
[ 0 𝐴22 𝐴23 ] [𝑥2 ] = [𝑏2 ]
0 0 𝐴33 𝑥3 𝑏3
This is equal to
𝐴11 𝑥1 + 𝐴12 𝑥2 + 𝐴13 𝑥3 = 𝑏1
𝐴22 𝑥2 + 𝐴23 𝑥2 = 𝑏2
𝐴33 𝑥3 = 𝑏3
Use Back Substitution
𝑏(3) 𝐴𝑏(3, 𝑒𝑛𝑑)
𝑥3 = =
𝐴𝑏(3,3) 𝐴𝑏(3,3)
𝑏(2) − 𝐴𝑏(2,3)𝑥3
𝑥2 =
𝐴𝑏(2,2)
𝑏(1) − 𝐴𝑏(1,2)𝑥2 − 𝐴𝑏(1,3)𝑥3
𝑥1 =
𝐴𝑏(1,1)
This 𝑥1 can be written as
𝑥2
(𝑏(1) − [𝐴𝑏(1, 2) 𝐴𝑏(1,3)] [𝑥 ])
3
𝑥1 =
𝐴𝑏(1,1)
OR
𝑥2
(𝐴𝑏(1, 𝑒𝑛𝑑) − [𝐴𝑏(1, 2) 𝐴𝑏(1,3)] [𝑥 ])
3
𝑥1 =
𝐴𝑏(1,1)
LU Decomposition
Matrix A can be written as a product of the lower and upper triangular matrix
𝐴 = 𝐿𝑈
U is the matrix obtained after gauss elimination, and L is the below matrix
1 0 … 0
𝑚
𝐿 = [ 2,1 1 … :]
𝑚3,1 𝑚3,2 … :
m is the multiplier
for i = 1:20
x(1) = 4 + x(2)
x(2) = 0.5*(1-x(1))
end
This is converging.
𝑥₁ + 𝑥₂ + 𝑥₃ = 4
2𝑥₁ + 𝑥₂ + 3𝑥₃ = 7
3𝑥1 + 4𝑥2 − 2𝑥₃ = 9
we need to rearrange the equation as first equation 2 then 3 then 1. This arrangement is to get
diagonally dominant matrix.
A = [ Ab(2,:); Ab(3,:); Ab(1,:)];
Now apply Gauss Siedel Method,
First initialize the number of variables n
n=3;
x= zeros(n,1); % initial solutions
err = zeros(n,1); % to calculate error
k: will iterate for the n number of variables
j: will iterate for the number of columns.
𝑗=𝑘−1 𝑗=𝑛
𝑏𝑘 − (∑𝑗=1 𝐴𝑘,𝑗 𝑥𝑗𝑖+1 + ∑𝑗=𝑘+1 𝐴𝑘,𝑗 𝑥𝑗𝑖 )
𝑥𝑘𝑖+1 =
𝐴𝑘,𝑘
Outer loop: This loop runs for a fixed number of iterations (25 in this case) to allow the Gauss-
Seidel method to converge to a solution.
For iterations = 1:50
for k = 1:n
% apply gauss Seidel method
end
Display the solution or iteration or error using disp or fprintf
end