关键词 > Python代写

Numerical Methods (2022–23) Assignment 2

发布时间:2022-12-01

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Numerical Methods (2022–23) Assignment 2


Due:  02- 12-2022 Your submission must be a  .pdf  file containing a written account of your solutions to all of the Questions below, together with any .py files created for use in Python. All figures requested must be included in the  .pdf  file and annotated with axis labels and figure captions. The source code of all Python functions specifically requested must be included in the .pdf  file. All files must be zipped together in a single  . zip  file and uploaded to Moodle by the submission deadline. Further details on the submission format are available on Moodle. Do not deviate from the submission instructions.

Question 1 In this question, you will consider the solution to the system of linear equations

3 a + 2b − 4 c = −5,                                                                  (2)

−5a + b + 4 c = −6,                                                                  (3)

using methods introduced in Chapter 5.

(a) Express the system of equations (1)–(3) in the form Ax = f, where A is a 3 × 3 matrix, x = [a,b,c]T , and f = [8, −5, −6]T . Subsequently, use Gaussian elimination and pivots to obtain an LU-factorisation in the form PA = LU , where P is a permutation matrix corresponding to the pivots used. Finally, use your LU-factorisation to find the exact solution of the system of equations. Provide all your working in your report, including a detailed step-by-step write- up of all elementary row operations and pivots used.  Marks will be awarded here for the clarity of your report [5 marks].

(b) Create a Python function SOR(A,b,x0,w,maxiter)  that implements Successive Over Re- laxation (SOR) to obtain the solution of N-linear equations expressed in the form Ax = b. The Python function should take the matrix A, the vector b, an initial guess x0 , the arbi- trary parameter w, and the maximum number of iterations as inputs. The Python function should return an N × M matrix containing your solution approximation at each iteration step xj,j = 0...M (where M is the maximum number of iterations) and a suitable measure of the convergence of your function at each iteration step (e.g. ||xj||−||xj 1 ||,j = 1...M). Provide the source code for your function in the report.  Test your scheme with a suitable set of linear equations and provide a figure that shows convergence to the solution of your test set of linear equations. [6 marks].

(c) Use your Python function to solve the system of equations (1)–(3).  Compare the exact so- lution obtained in Q1(a) with the solution obtained by your SOR function.  Investigate the dependence of the convergence of your SOR solution toward the exact solution as the arbi- trary parameter w is varied. Report your findings and include any relevant figures in your report. [4 marks]

Question 2 In this question, you will consider the solution to an ordinary differential equation with Dirichlet boundary conditions given by

uxx + 2ux + u + e x = 0,    u(0) = u(1) = 0.                                               (4)

(a) Describe in your report how a finite-difference scheme is defined to obtain the numerical solution of (4).  (Hint: Create a uniform partition of [0, 1] with (N + 1) evenly spaced nodes with u0  = uN  = 0 and unknowns u1 , u2 ,...,uN 1 , and express the unknowns in a system of linear equations Au = f) [3 marks]

(b) Use your Python function SOR(A,b,x0,w,maxiter)  to solve your finite-difference scheme

and compare it with the analytical solution of (4).  Include a figure in your report showing this comparison. [3 marks]

(c) Add to your report a discussion of alternative methods that could be used to solve your finite difference scheme [ 1 marks].

Additional information

You can use the following LATEX code to to record your EROs and pivots:

$$

% Create   array   1   containing   pivot - tracking

\ left [

\ begin { array }{ c }

P _ 1   \\   P _ 2   \\   P _ 3

\ end { array }

\ right ]

%

\ qquad   % separate   arrays   with   a   large   space

% Create   array   2   containing   entries   of  L - matrix

\ begin { array }{ c }

\\   L _ {21}   \\   L _ {31}\\

\ end { array }

% Array   3   is   the   A   undergoing   row   operations

\ left [

\ begin { array }{ ccc }

a _ {11}   &   a _ {12}   &   a _ {13}     \\

a _ {21}   &   a _ {22}   &   a _ {23}   \\

a _ {31}   &   a _ {32}   &   a _ {33}

\ end { array }\ right ]

\ begin { array }{ l }

\\

R _ 2\ rightarrow   R _ 2 - \ alpha _ 1  R _ 1\\

R _ 3\ rightarrow   R _ 3 - \ alpha _ 2  R _ 1

\ end { array }

$$

this produces the following

l P(P)2(1)    P3  

The online LATEX editor Overleaf edu/glasgow for more details).

L21

L31

l 

  a31

a12

a22

a32

 

a33   

is highly recommended

R2 → R2 − α 1R1

R3 → R3 − α2R1

(please see https://www.overleaf.com/