MATH3036 Scientific Computation and Numerical Analysis

Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due

MATH3036 Scientific Computation and Numerical Analysis

— Coursework 1 (20%) —

Submission deadline: 3pm, Monday, 3 March Wednesday 5 March 2025

Note: This is currently version 4 of the PDF document. (19th February, 2025)

No more new questions will be added anymore.

This coursework contributes 20% towards the overall grade for the module.

Rules:
• Each student is to submit their own coursework.
• You are allowed to work together and discuss in small groups (2 to 3 people), but you must write your own coursework and program all code by yourself.
• Please be informed of the UoN Academic Misconduct Policy (incl. plagiarism, false authorship, and collusion).

Coursework Aim and Coding Environment:

• In this coursework you will develop Python code related to the (Petrov–) Galerkin method.
• You should write (and submit) plain Python code (.py), and you are encouraged to use the Spyder IDE (integrated development environment) or other IDE. Hence you should not write IPython Notebooks (.ipynb), and you should not use Jupyter.

How and Where to run Spyder:

• Spyder comes as part of the Anaconda package (recall that Jupyter is also part of Anaconda). Here are three options on how to run Spyder:
(1) You can choose to install Anaconda on you personal device (if not done already).
(2) You can open Anaconda on any University of Nottingham computer.
(3) You can open a UoN Virtual Desktop on your own personal device, which is virtually the same as logging onto a University of Nottingham computer, but through the virtual desktop. Then simply open

Anaconda. Here is further info on the UoN Virtual Desktop.

• The A18 Computer Room in the Mathematical Sciences building has a number of computers available, as well as desks with dual monitors that you can plug into your own laptop.
Support Sessions:
• Fridays 10–11am, ESLC C13 Computer Room (Time-tabled drop-in)
• Fridays 11–12noon, ESLC C13 Computer Room (Lecturer’s office hour drop-in)
• You can work on the coursework whenever you prefer.
• We especially encourage you to work on it during the (optional) drop-in sessions.
Moodle Forum/Blog (https://moodle.nottingham.ac.uk/mod/forum/view.php?id=8053188):
• You are allowed and certainly encouraged(!) to also ask questions to obtain clarification of the course
work questions, as well as general Python queries.

• However, when posting a query/response, please ensure that you are not revealing any answers.

Also, please ensure that you are not directly revealing any code that you wrote. Doing so is considered Academic Misconduct.

• When in doubt, please simply attend a drop-in session to meet with the Lecturer.
Helpful resources:
• Python 3 Online Documentation – Spyder IDE (integrated development editor) – NumPy User Guide – Matplotlib Quick Start Guide – Moodle: Core Programming 2022-2023 – Moodle: Core Programming 2021-2022
• You are expected to have basic familiarity with Python, in particular: logic and loops, functions, NumPy and matplotlib.pyplot. Note that it will always be assumed that the package numpy is imported as np, and matplotlib.pyplot as plt.
Some Further Advice:
• Write your code as neatly and read-able as possible so that it is easy to follow. Add some comments to your code that indicate what a piece of code does. Frequently run your code to check for (and immediately resolve) mistakes and bugs.
• Coursework Questions with a “? ” are more tedious, but can be safely skipped, as they don’t affect follow-up questions.
Submission Procedure:
• Submission will open after 14 Feb 2025.
• To submit, simply upload the requested .py-files on Moodle. (Your submission will be checked for plagiarism using turnitin.)
• Your work will be marked (mostly) automatically: This is done by running your functions and comparing their output against the true results.

Petrov–Galerkin discretization for first-order differential equation 

(The questions 0, 1, 2 and 3 below are warm-up exercises. No marks will be awarded for them. Their solution will be available on Moodle as file warmup solution.py .)

Recall the problem (strong form):

for some given function f.

Consider the global polynomial approximation of degree N (with N an integer ≥ 1) given by the following Petrov–Galerkin discretization (see the end of Lecture 1):

where Uh = n polynomials of degree at most N, and equal to 0 at x = 0o ,
and Vh = n polynomials of degree at most N − 1 o .
Consider the following basis functions:
0 Download from Moodle (Coursework 1 pack) the Python files (main.py and DEtools.py). Move the files into a folder, and open them in, e.g, Spyder (Ana

conda)

Note: The file DEtools.py will be a module (recall that a module can be imported and contains Python definitions and statements). The idea is that DEtools.py contains your function definitions (algorithms), while main.py is a testing script that is used to test your module by importing it and calling its functions.

1 The file DEtools.py already contains code that will correctly compute the Petrov Galerkin (PG) approximation for the case N = 2.
• Run main.py (the two parts related to Q1) to compute this PG approximation (N = 2), and to plot this approximation as well as the exact solution.
• Compare the obtained results with the hand calculated approximation (see Problem at end of Lecture 1).
• Study how the PG approximation depends on the number of subintervals n used in the numerical integration (composite mid-point rule) of integrals defining the components of the right-hand vector b.

• Next change the data f(x) and uex(x). Does it work as expected? 

Next, implement the case N = 3, by completing the following unfinished functions in DEtools.py:

AssemblePGmatrixN3() and AssemblePGvectorN3(f,n).

Test your functions by suitably adding code in the main.py file.

3 (? ) Next, implement the general case (N) by completing the unfinished functions: AssemblePGmatrix(N) and AssemblePGvector(N,f,n).

Test your functions by suitably adding code in the main.py file.

I Petrov–Galerkin for a general first-order differential equation
(Assessed questions start here. Do make sure you’ve looked at Questions 0–3)
Next, consider the more general 1st-order problem:
where c ∈ R is a parameter. The corresponding Petrov–Galerkin discretization is:
Find

where the discrete spaces are the same as before, i.e.,

Let . The resulting N × N linear system is:

in other words,

and the components bi of vector b are the same as before (see Question 1), i.e., composite mid-point rule approximations of the involved integrals
using n subintervals.

Note that the (updated) file DEtools.py contains the unfinished function: [8 / 40]
def AssemblePGmatrixGenDE (N ,c):
"""
DESCRIPTION
Parameters
----------
N : TYPE
DESCRIPTION .
c : TYPE
DESCRIPTION .
Returns
-------
A: TYPE
DESCRIPTION .
"""
return A
The function AssemblePGmatrixGenDE is to return the numpy.ndarray A, shape (N, N).
• Complete this function so that it provides the output as required.
• Test your function by running the main.py file (part Q4), which will run your function, then solve the system (using AssemblePGvector for f(x) = 1), and plot the approximation and exact solution. Try different N. (Do approximations seem to converge as N → ∞?)
Hint: When N = 3 and c = 4 the output for A should be:
[[3. 1.16666667 0.66666667]
[1.83333333 0.83333333 0.51666667]
[1.33333333 0.65 0.42222222]]
and u = [ 0.89593831 -2.33651299 1.55717532]
• Also complete the function documentation at the top of the function definitions, the so-called doc string. Simply replace DESCRIPTION and TYPE by appropriate ones. This documentation should become visible, whenever one types e.g. help(DE.AssemblePGmatrixGenDE) or DE.AssemblePGmatrixGenDE?.
Hint: See some of the docstrings already given in DEtools.py. See also the useful online pandas guide to writing a docstring.
Assessment
When submitting your coursework, you will only be asked to upload your DEtools.py file.
Marks can be obtained for your function AssemblePGmatrixGenDE for generating the required output, for certain set(s) of inputs. The correctness of the following may be checked:
• The type of outputs
• The np.shape (number of columns and rows) of outputs
• The values of outputs
• The output of ”help(. . . )” on your function(s).
Note that in marking your work, different sets of input(s) may be used.
Galerkin FEM for second-order differential equation
Next consider the following 2nd-order differential equation, with Dirichlet boundary conditions:

where c ≥ 0 is a parameter. Consider the Galerkin FEM (Uh = Vh) based on the weak form:

发表评论

电子邮件地址不会被公开。 必填项已用*标注