Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Numpy and Python. a) Implement back substitution in Python, using Numpy. Use sim

ID: 3733439 • Letter: N

Question

Numpy and Python.

a) Implement back substitution in Python, using Numpy. Use simple numpy function, f.ex. numpy.dot. Solve for Rx = b, where R = numpy.array([[1,4,1], [0,6,4], [0,0,2]]) is the upper triangle matrix and b = numpy.array([3,2,1]) is the lower triangle matrix.

Use the following code:

def backsub(R,b):
    """ back substitution
    input: n x n upper triangle matrix R (treated as a normal matrix)
            n-vector b
    output: The solution x for Rx=b """
    n=R.shape[0]
    x=np.zeros(n)
    # enter code here
    # ...
    return x

b) Implement an algorithm that solves the linear square simultaneous equation Ax = b with QR decomposition and back substitution. Use the following code and the qr and backsub functions from a), as well as Numpy. Solve the simulataneous equation Ax = b, with A = numpy.array([1,2,-1],[-1,1,1],[1,-4,1]]) and b = numpy.array([1,2,3]) with your code.

Begin with this code:

def linsolve(A,b):
    """ Solves Ax=b"""
    # enter code here
    # ...
    return x

Algorithm 11.1 BACK SUBSTITUTION given an n x n upper triangular matrix R with nonzero diagonal entries, and an n-vector b. Fori,1,

Explanation / Answer

A)
code:
import numpy as np
import numpy.linalg as lin
#function for back substitution
def backsub(R, b):
n = R.shape[0]
#print n
x = np.zeros(n)
# as b is 1 by 3 , b.T is used to change the shape to 3 by 1
#taking inverse of R and calculating the dot product
x = np.dot(lin.inv(R),b.T)
print x
R = np.array([[1, 4, 1], [0, 6, 4], [0, 0, 2]])
b = np.array([[3,2,1]])
backsub(R,b)
output:
[[ 2.5]
[ 0. ]
[ 0.5]]