**ASSESSED COURSEWORK 2A – NUMERICAL INTEGRATION**

**Rules:**

Each student is to submit their own coursework.

You are allowed to work together and discuss in small groups (2 to 4 people), but you must write all code by **yourself**.

You must submit only the .py files requested in this question paper. Details of the required filenames are given within the questions. You are strongly encouraged to use the **Spyder IDE **(integrated development environment). Hence you should **not **write IPython Notebooks (.ipynb), and you should **not **use Jupyter.

You may adapt any code that we developed in class (i.e. you do not have to start from scratch).

**Guidelines for submitting the coursework correctly:**

For full marks, your functions need to return the correct outputs for the particular input arguments.

Please be aware that **we will test your functions **with an automated marker to check that they work and produce the desired output(s), both with the data given in the question and with different undisclosed data.

There is a code testing facility available on Moodle that will help you determine if your modules are producing the correct outputs. **We strongly advise you to use this facility prior to submitting your coursework**.

- To use this facility, download the test_student_code.py file from Moodle into the same directory as your module files, and then run it through Spyder. An html file should appear in the same directory; you can view this using a web browser.
- If your code fails to produce valid outputs in the tests of this facility, then it will fail to run through the automarker, and you risk scoring
**low**marks. - The code testing facility should show clearly what the particular problem is; but if the warning/error message is not clear, contact the module teacher.
- The code testing facility does not tell you how many marks you have obtained, it simply shows what outputs your code will produce when passed through the automarker.

If your modules have filenames that differ to what we have explicitly asked for, then your modules will not run through the automarker, and you risk losing marks.

• Therefore, please do **not **add your username or student ID number to your filename. Your functions **must **have the **same **inputs and outputs as those specified in the question(s),and in the **same order **as specified.

1. **Approximating a single integral:**We have seen that the composite trapezium rule can be written as a

*weighted sum*of function

values at specified points. This representation is given below.

? h ?−1∫ ?(?)d?≈ 2(?(?0)+?(??)+2∑?(??)), (1) ? ?=1

where h = ? − ? and ?? = ? + h?. ?

**Your task 1a:**

Using template_question_1.py (on Moodle) as a template, write a module containing a function single_int_approx(a,b,f,N) to approximate the integral

?

∫ ?(?)d? (2)

using the composite trapezium rule, as stated in equation (1), for all integer values of ? between 1 and ?, i.e. for 1 to ? strips.

These approximations should be saved in an array: the first entry of the array should be the approximation calculated with 1 strip, the second entry should be the approximation calculated with 2 strips, etc, and the final entry should be the approximation calculated with ? strips.

The function should take as its inputs ?, ?, ? and ?, where ?, ? and ? correspond to the limits of integration and the integrand from equation (2), and ? is the largest number of strips to be considered.

Your module file **must **be named **Q1.py **and your function should return **one **output - the array containing the integral approximations.

2. **Approximating a double integral:**

In this question, we introduce a new quadrature rule which, similarly to the composite trapezium rule and composite Simpson’s rule, splits the region of integration into ? strips and can be written as a weighted sum of function values at specified points. This representation is given below.

?3

∫ ?(?)d?≈ 8h(?(?0)+3?(?1)+3?(?2)+2?(?3)+3?(?4)+3?(?5)+2?(?6)

+ ⋯+2?(??−3)+3?(??−2)+3?(??−1)+?(??)),

3 ⎛⎜ ?−1 ?/3−1 ⎞⎟= 8h⎜?(?0)+?(??)+3∑?(??)+2 ∑ ?(?3?)⎟. (3) ⎜ ?=1 ?=1 ⎟ ⎝ ?≠3? ⎠

To clarify notation, the first sum of function values ?(??) is for ? values starting from 1 that arenotmultiplesof3(i.e. ?=1,2,4,5,7,8,...)andthesecondsumoffunctionvalues ?(?3?) accounts for the multiples of 3. **It is important that when using this quadrature rule, ****n ****is a multiple of ****3****.**

We have seen that, in 2D, if we wish to integrate ? (?, ?) over a rectangular region ? ≤ ? ≤ ?, ? ≤ ? ≤ ?, then we can use a quadrature rule in each coordinate direction as follows:

?

∫ ∫ ?(?,?)d?d? ≈ ∫ [∑???(??,?)] d? ? ? ? ?=1 ???? ≈ ?̂ ? ? (? , ? ) = ? ?̂ ? (? , ? ). ∑?[∑? ??]∑∑?? ??

We can apply a numerical quadrature rule in both coordinate directions in this case, but notice that for each quadrature point in the ?-direction, the quadrature points in the ?-direction will change.

**Your task 2a:**

Using template_question_2a.py (on Moodle) as a template, write a module containing a function double_int_approx(c,d,x1,x2,f,n) to approximate the double integral

? ?2(?) ?(?,?)d?d?, ? ?1(?)

using the quadrature rule stated in equation (3) in both coordinate directions.

The function should take as its inputs ?, ?, ?1(?), ?2(?), ? and ?, where ?, ?, ?1(?), ?2(?) and ? correspond to the limits of integration and the integrand from equation (4), and ? is the number of strips to be used in the approximation (you may assume that the same number of strips is to be used in each coordinate direction, but remember that ? must be a multiple of 3).

**Test:**

To test that your code is working as expected, compute an approximation for

2?∫ ∫ ?2?d?d? 1 −?

for ? = 12. For the weights in the ?-direction, you should obtain the array given below

[0.03125, 0.09375, 0.09375, 0.0625 , 0.09375, 0.09375, 0.0625 [0.09375, 0.09375, 0.0625 , 0.09375, 0.09375, 0.03125]

and for the approximation of the double integral, you should obtain a value of

4.13334297839506.

**Your task 2b:**

Using template_question_2b.py (on Moodle) as a template, write a module containing a function plot_region(c,d,x1,x2,n) to create a figure containing the plot of the region of integration for the double integral

? ?2(?) ?(?,?)d?d?. ? ?1(?)

In order for the automarker to generate figures using your code, you need to return the figure as an output. To do so, give the figure a ‘handle’ when you create it, i.e. assign it to a variable. For example

fig = plt.figure()

You can then return the figure to the user using the command return fig.

Your module file **must **be named **Q2b.py **and your function should return **one **output - the figure containing the plot of the region of integration.

**Test:**

To test that your code is working as expected, plot the region of integration for the double integral

2?∫ ∫ ?2?d?d? 1 −?

with ? = 12. You should be able to verify the accuracy of your plot by sketching the region of integration by hand.

You can check that your code works for this example by running the test_student_code.py provided on Moodle. Please note that your code will be run for *several examples *when it is being marked, not just the stated test example, so you are encouraged to test your code using your own examples also.

[7 marks]