Introduction to Numerical Problem Solving TX00BY09-3007

Assignment: 04 Graphical analysis

Description: From the exercises 04, solve the problems 4 and 6 by using Python. Attach your solutions as a jupyter Notebook here.

Author: Joonas Forsberg

This document has been prepared for the fourth assignment for the course "Introduction to Numerical Problem Solving". It contains code examples and explanations for the written code. In addition, I've included reasoning behind the behavior where acceptable.


In [2]:
# Import required libraries 
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import *
from numpy import *

Exercise 04

Given equation system $$10.0x_1 + 2.0x_2 − x_3 = 27.0$$ $$−3.0x_1 − 6.0x_2 + 2.0x_3 = −61.5$$ $$x_1 + x_2 + 5.0x_3 = −21.5$$ (a) Solve by Gaussian elimination
(b) Substitute your solutions into the original equations in order to check answers.

In order to solve the given problems, we first need to create a function for Gaussian elimination. For that, I am using the function created in class.


In [3]:
def gaussian(a, b):
    
    # Copy variables
    a = a.copy()
    b = b.copy()
    
    n = len(b)
    x = zeros(n)
    
    # Step 1: Forward elimination
    for k in range(0, n - 1):
        
        # Pivot the rows
        r = argmax(abs(a[k:, k])) + k
        
        if r != k:
            temp = a[k, :].copy()
            a[k, :] = a[r, :]
            a[r, :] = temp
            
            temp = b[k].copy()
            
            b[k] = b[r]
            b[r] = temp
            
        for i in range(k + 1, n):
            factor = a[i, k] / a[k, k]
            for j in range(n):
                a[i, j] = a[i, j] - factor * a[k, j]
                
            b[i] = b[i] - factor * b[k]
    
    # Step 2: Backward substitution
    x[n - 1] = b[n - 1] / a[n - 1, n - 1]
    for i in range(n - 2, -1, -1):
        sum = b[i]
        for j in range(i + 1, n):
            sum = sum - a[i, j] * x[j]
        x[i] = sum / a[i, i]
    
    return (x)

Create variables for the exercise 4a and call function gaussian(a, b)


In [4]:
a = np.array(([10.0, 2.0, -1.0], [-3.0, -6.0, 2.0], [1.0, 1.0, 5.0]))
b = np.array(([27.0, -61.5, -21.5]))
result = gaussian(a, b)

For exercise 4b we need to create a function to confirm the results by substituting valous in the original equation.


In [5]:
def confirm_gaussian(result_array):
    
    # Build the equations
    e1 = a[0][0] * result_array[0] + a[0][1] * result_array[1] - result_array[2]
    e2 = a[1][0] * result_array[0] + a[1][1] * result_array[1] + (a[1][2] * result_array[2])
    e3 = a[2][0] * result_array[0] + a[2][1] * result_array[1] + (a[2][2] * result_array[2])
    
    # Check results
    if e1 == b[0] and e2 == b[1] and e3 == b[2]:
        return True
    else:
        return False

    
if(confirm_gaussian(array(result)) == True):
    print("The equations are correct!")
else:
    print("Equations don't match! :(")


The equations are correct!

Exercise 06

Solve the equations Ax = b by Gauss elimination, where $$A = \begin{pmatrix} 0.0 & 0.0 & 2.0 & 1.0 & 2.0 \\ 0.0 & 1.0 & 0.0 & 2.0 & -1.0 \\ 1.0 & 2.0 & 0.0 & -2.0 & 0.0 \\ 0.0 & 0.0 & 0.0 & -1.0 & 1.0 \\ 0.0 & 1.0 & -1.0 & 1.0 & -1.0 \end{pmatrix} $$ and $$b = \begin{pmatrix} 1.0 \\ 1.0 \\ -4.0 \\ -2.0 \\ -1.0 \end{pmatrix}$$ Hint: You need to reorder the equations before solving.

We can solve the exercise by using the gaussian function. Reordering the equation is done by pivoting the rows.


In [9]:
a = np.array(([0.0, 0.0, 2.0, 1.0, 2.0], [0.0, 1.0, 0.0, 2.0, -1.0], [1.0, 2.0, 0.0, -2.0, 0.0], [0.0, 0.0, 0.0, -1.0, 1.0], [0.0, 1.0, -1.0, 1.0, -1.0]))
b = np.array(([1.0, 1.0, -4.0, -2.0, -1.0]))

result = gaussian(a,b)
print(result)


[ 2. -2.  1.  1. -1.]

How do we know if the values are correct? We can solve the equation by using the numpy.linalg package and dot function to get the dot-product of the matrix.


In [7]:
from numpy.linalg import *
x = dot(inv(a,), b)

# Check if the arrays match by using the all() method
if result.all() == x.all():
    print("Correct!")
else: 
    print("Incorrect!")


Correct!