关键词 > Python代写

Numerical Methods (2021–22) Assignment 2

发布时间:2021-12-04

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



Numerical Methods (2021–22) Assignment 2

 

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. Please, do not deviate from the submission instructions.

Question 1

Given the function f (x) = ex  with x ∈ [ − 1, 1],

 

1. compute its numerical derivatives on x ∈ ( − 1, 1) using centred-difference, second order accurate finite difference stencils.   Additionally,  for the numerical deriva- tive  at x  =  − 1  and x  =  1 use  appropriately the first order  accurate forward and backward-difference schemes.  Save your result into a single array.  Use n = [11, 31, 101, 301, 1001] equally spaced grid points in [ − 1, 1]. For each n plot the ab- solute difference between the exact and numerical derivative against x in the same figure. To distinguish the different lines in your plot use a different linestyle and/or

linewidth. Add labels and a legend.                                                                           (Marks: 27)

2. at x = 0 compute centred-difference, second order accurate finite difference deriva- tive of f (x) using the data points {−dx, 0, dx} for dx = 1/2p and p = [0, 1, 2, . . . 20]. Compute the absolute difference between your numerical result and the exact deriva- tive.   Repeat  using the  first  order  forward-difference  method with  data  points {0, dx} and the same dx as above.  Plot both results in a double logarithmic plot, add a grid and labels. Use different line styles for your two lines. Comment on your results.

(Marks:  19)

 

Question 2

Given the system of linear equations

6x + 2y − z   =    7

x + 2y + 8z   =   11

2x − 6y + z   =   −3

(1)

1. use the Gauss-Jacobi and Gauss-Seidel method to find an approximate solution with starting guess (x , y , z ) = (0, 0, 0) for up to n = 20 iterations.  Does any of them converge? Plot the values xi , yi  and zi  with respect to the iteration i in two plots, one for Gauss-Seidel and one for Gauss-Jacobi. Add a legend and labels and use different line styles for x, y and z .

(Marks: 25)

2. compute Gerhsgorin’s circles for Gauss-Jacobi and Gauss-Seidel and plot them in one figure for each method. Use different line styles, add a legend and labels.

Hint: You can plot a circle located at e.g. (2, 0) with radius 2 as

import numpy  as np

import matplotlib.pylab  as plt

 

theta  = np .linspace(0 ,  2*np .pi,  1000)

x  =  2*np .cos(theta)  +  2

y  =  2*np .sin(theta)

plt .plot(x,  y)

(Marks:  17)

 

Question 3

Write a function that takes a n × n matrix (numpy array) and determines if it is diagonally dominated. Your function should return True or False. Check if the input array is indeed of shape n × n.

(Marks:  12)

 

Notes

1. Use of inbuilt Python functions or functions imported from libraries that would solve significant parts of the questions, except the ones explicitly mentioned, is prohibited.

2. All plots must contain labels for the axis.

3. The programming component is a significant part of this assignment.  All Python code should be fully commented and included in your submission both as .py files as well as printed out in your final .pdf report along with figures, tables and discussion of your solutions.

4. You can save a figure after it is plotted using the command plt.savefig(’my plot.pdf’). If you use the  .pdf ending instead of .png or  .jpg then your figure file will be in

vector graphics format, which is of much higher quality than a pixel based image that can easily look blurry.

5. You can represent matrices in LATEX as:

\begin{equation}

A  =

\left(

\begin{array}{ccc}

1  &  2  &  3  \\

4  &  5  &  6  \\

7  &  8  &  9

\end{array}

\right)

\end{equation}

 

 


which results into

 

 

(2)