0% found this document useful (0 votes)
19 views

Tutorial5 and Quiz Solutions

Uploaded by

rena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Tutorial5 and Quiz Solutions

Uploaded by

rena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial5_solutions

February 9, 2023

1 Tutorial 5
1.1 Learning Objectives
• Practice to solve linear systems using:
– Gauss elimination
– LU factorization
– Inverse matrix
• Solution to a tridiagonal matrix
• Newton Rapson for solving a non-linear system of equations

1.2 Problem 1: Solution of a 3x3 linear system


We would like to solve the following system of three equations:

3𝑥0 − 6𝑥1 + 12𝑥2 = 9 − 𝑥1 + 3𝑥2 − 3𝑥3 = 04𝑥1 + 5𝑥2 + 6𝑥3 = 28

Use Gauss elimination, LU factorization and Inverse matrix to solve the system of equations.

1.2.1 Solution using Gauss elimination

[25]: import numpy as np


import linear_nonlinear_systems as lnls

# Create matrix A
A = np.matrix([[3.,-6.,12.],
[-1.,3.,-3.],
[4.,5.,6.]])
AOrg = A

# Create rhs vector


b = np.transpose([[9.,0.,28.]])
bOrg = b

# Solve the linear system using Gauss elimination


x = lnls.GaussElimination( A, b )
print(x)

1
[[3.]
[2.]
[1.]]

1.2.2 Solution using LU decomposition

[43]: # LU factorize A
A = AOrg
b = bOrg
(L,U) = lnls.LUfactorization( A )

# Print L and U
print(f'L = {L}')
print(f'U = {U}')

# Check that LU=A


Res = L.dot(U) - A
print(f'Res = {Res}')

L = [[ 1. 0. 0. ]
[-0.33333333 1. 0. ]
[ 1.33333333 13. 1. ]]
U = [[ 3. -6. 12.]
[ 0. 1. 1.]
[ 0. 0. -23.]]
Res = [[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[44]: y = lnls.GaussElimination(L, b)
x = lnls.GaussElimination(U, y)
print(x)

[[3.]
[2.]
[1.]]

[45]: x = lnls.LUsolve(L,U,b)
print(x)

[[3.]
[2.]
[1.]]

2
1.2.3 Solution using Matrix inverse

[28]: A = AOrg
b = bOrg
# Compute A^-1
Am1 = lnls.ComputeInvMat( A )
print('The inverse matrix is Am1=',Am1)

# Check that AA^-1 is equal to the identity matrix


Imat = np.matrix(np.eye(3))
Res = A.dot(Am1) - Imat
print(f'Res = {Res}')

The inverse matrix is Am1= [[-0.47826087 -1.39130435 0.26086957]


[ 0.08695652 0.43478261 0.04347826]
[ 0.24637681 0.56521739 -0.04347826]]
Res = [[ 0.00000000e+00 -4.44089210e-16 -1.11022302e-16]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 2.22044605e-16 2.22044605e-16 0.00000000e+00]]

[29]: x = Am1.dot(b)
print(x)

[[3.]
[2.]
[1.]]

1.3 Problem 2: Solution to a tridiagonal linear system


We would like to compute the solution to the following tridiagonal linear system

4 1 𝑥0 6
⎡ 2 4 1 ⎤⎡ 𝑥1 ⎤ ⎡ 13 ⎤
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ 2 4 1 ⎥⎢ 𝑥2 ⎥=⎢ 18 ⎥
⎢ 2 4 1 ⎥⎢ 𝑥3 ⎥ ⎢ 15 ⎥
⎣ 2 4 ⎦⎣ 𝑥4 ⎦ ⎣ 8 ⎦

[30]: import numpy as np


import linear_nonlinear_systems as lnls

# Create the matrix as the three vectors e,f,g


e = 2 * np.ones(5)
f = 4 * np.ones(5)
g = 1 * np.ones(5)

# Create the rhs vector


b = np.array([6, 13, 18, 15, 8])

3
# Solve the system
x = lnls.Tridiag(e, f, g, b)

print('Solution is x =\n', x)

Solution is x =
[[1.]
[2.]
[3.]
[2.]
[1.]]

1.4 Problem 3: Newton Raphson to solve a system of 2 non-linear equations


We would like to solve the following system of 2 equations:

3𝑥30 + 2𝑥0 𝑥21 = 40𝑥20 − 11𝑥1 = −18

The Jacobian matrix is defined as:

𝜕𝑓0 𝜕𝑓0
𝜕𝑥0 𝜕𝑥1
𝐽 =[ 𝜕𝑓1 𝜕𝑓1 ]
𝜕𝑥0 𝜕𝑥1

The analytical Jacobian matrix is:

9𝑥20 + 2𝑥21 4𝑥0 𝑥1


𝐽 =[ ]
2𝑥0 −11
a) Find the solution using the analytical Jacobian matrix
b) Use second-order accurate finite difference centered scheme to write the Jacobian matrix and
use it to solve the non-linear equations
[31]: import numpy as np
import linear_nonlinear_systems as lnls

def f0( x0, x1 ): return 3.* (x0**3) + 2. * x0 * (x1**2) - 40.

def f1( x0, x1 ): return x0**2 - 11. * x1 + 18.

def func2by2( x ):

f = np.transpose([np.zeros(2)])
f[0] = f0( x[0], x[1] )
f[1] = f1( x[0], x[1] )

return f

4
def J2by2_approx( x, h ):

J = np.matrix(np.zeros((2,2)))
J[0,0] = ( f0( x[0]+h, x[1]) - f0( x[0]-h, x[1]) ) / ( 2 * h )
J[0,1] = ( f0( x[0], x[1]+h) - f0( x[0], x[1]-h) ) / ( 2 * h )
J[1,0] = ( f1( x[0]+h, x[1]) - f1( x[0]-h, x[1]) ) / ( 2 * h )
J[1,1] = ( f1( x[0], x[1]+h) - f1( x[0], x[1]-h) ) / ( 2 * h )

return J

def J2by2( x, h ):

J = np.zeros((2,2))
J[0,0] = 9 * x[0]**2 + 2 * x[1]**2
J[0,1] = 4 * x[0] * x[1]
J[1,0] = 2 * x[0]
J[1,1] = -11

return J

# Initialize solution vector


xinit = np.zeros(2)
xinit[0] = 1
xinit[1] = 3

# Solve with Newton-Raphson


( x_abs, normf, eps_r, iteration ) = lnls.MultiNewton( func2by2, J2by2, xinit,␣
↪0, 1e-8 )

print('Solution is x =\n', x_abs)

Iteration Norm($ f(x_r) $) $ \epsilon_r $


0 0 7.355831986 54.57463884
1 1 0.4551003593 8.188833722
2 2 0.001841652665 0.4914995032
3 3 3.039273039e-08 0.001986831709
4 4 0 3.276756772e-08
5 5 0 0
Solution is x =
[[2.]
[2.]]

[32]: # Solve with Newton-Raphson and approximate Jacobian matrix


( x_app, normf, eps_r, iteration ) = lnls.MultiNewton( func2by2, J2by2_approx,␣
↪xinit, 0.001, 1e-8 )

print('Solution is x =\n', x_app)

Iteration Norm($ f(x_r) $) $ \epsilon_r $

5
0 0 7.355825629 54.57464059
1 1 0.455100143 8.188828004
2 2 0.001841680065 0.4914992071
3 3 3.051266716e-08 0.001986858961
4 4 3.552713679e-15 3.28881674e-08
5 5 7.105427358e-15 1.426271926e-14
Solution is x =
[[2.]
[2.]]

1.5 Solution of Quiz


[58]: def f0( x0, x1 ): return np.exp(x0) + x1**2 - 5.

def f1( x0, x1 ): return x0**3 + np.cos(x0*x1) - 1.

def func2by2( x ):

f = np.transpose([np.zeros(2)])
f[0] = f0( x[0], x[1] )
f[1] = f1( x[0], x[1] )

return f

def J2by2_approx( x, h ):

J = np.matrix(np.zeros((2,2)))
J[0,0] = ( f0( x[0]+h, x[1]) - f0( x[0]-h, x[1]) ) / ( 2 * h )
J[0,1] = ( f0( x[0], x[1]+h) - f0( x[0], x[1]-h) ) / ( 2 * h )
J[1,0] = ( f1( x[0]+h, x[1]) - f1( x[0]-h, x[1]) ) / ( 2 * h )
J[1,1] = ( f1( x[0], x[1]+h) - f1( x[0], x[1]-h) ) / ( 2 * h )

return J

# Initialize solution vector


xinit = np.zeros(2)
xinit[0] = 1
xinit[1] = 3

# Solve with Newton-Raphson


( x_abs, normf, eps_r, iteration ) = lnls.MultiNewton( func2by2, J2by2_approx,␣
↪xinit, 0.001, 1e-8 )

print('Solution is x =\n', x_abs)

Iteration Norm($ f(x_r) $) $ \epsilon_r $


0 0 1.850774268 72.6576267
1 1 0.2962960932 20.78442051

6
2 2 0.03798219849 9.837558337
3 3 0.001156450023 1.76961688
4 4 1.189320795e-06 0.05723708497
5 5 2.1599367e-12 5.899505248e-05
6 6 2.220446049e-16 1.079090944e-10
Solution is x =
[[0.9734681 ]
[1.53391362]]

[59]: # Initialize solution vector


xinit = np.zeros(2)
xinit[0] = 0
xinit[1] = -5

# Solve with Newton-Raphson


( x_abs, normf, eps_r, iteration ) = lnls.MultiNewton( func2by2, J2by2_approx,␣
↪xinit, 0.001, 1e-8 )

print('Solution is x =\n', x_abs)

Iteration Norm($ f(x_r) $) $ \epsilon_r $


0 0 4.41 100
1 1 0.5781242568 35.53585818
2 2 0.01825134256 6.739518332
3 3 2.072490474e-05 0.22762248
4 4 2.684430456e-11 0.0002590606381
5 5 0 3.35553807e-10
Solution is x =
[[-3.55271309e-15]
[-2.00000000e+00]]

[ ]:

You might also like