ation plan to notebook 7 to learn about the new features and the actio
Search for question
Question
ation plan to Notebook 7 to learn about the new features and the actions to take if you are using extensions - Please note that updating to Notebook 7 might break some of your extensions.
Jupyter MTHS1008 Coding Coursework 2 (unsaved changes)
Logout
[
File
Edit
View
Insert
Cell
Kernel Widgets
Help
+ X 1
Run
C▸ Code
Not Trusted
Python 3 (ipykernel) O
Instructions
This year we have studied the Midpoint Method, the Trapezium Rule and Simpson's Rule for approximating the integral of a function, f(x), of a single
variable. If you haven't carefully studied the content of Guided Programming 8 and the associated exercise, I strongly advise you to do so before
attempting this coursework.
In this coursework you will investigate a closely related method called TW's Method, for approximating the integral of a strictly positive function, f(x).
Here's how TW's method works.
In order to approximate for f(x) dx, the range of integration, a ≤ x ≤ b, is divided up into N segments of length Ax = (b- a)/N, just as we did for the
Trapezium Rule. Now consider a single strip from xo to x0 + Ax.
For the Trapezium Rule, we approximate the function using a straight line joining the points (x), fo) and (x + Ax, f₁), where fo = f(x0) and
f₁ = f(x0+ Ax). The area under the straight line is then ½ ½ (fo + f₁) Ax, and the Trapezium Rule uses the sum of the areas of all these strips,
b
N
Ax Σ {ƒ (a + (i − 1)Ax) + ƒ (a + iAx)}.
I =
== S
f(x) dx ≈
1/ AX
i=1
In TW's Method, instead of using a straight line to approximate the function, we use an exponential function, f
=
Aekx, on each strip.
This gives
b
N
I =
= S.°
f(x) dx ≈ Ax
* Σ
{f(a+(i-1)Ax) − f(a+iAx)}
(Equation 1)
Inf (a+(i-1)Ax) - In f (a + iAx)*
a
i=1
Note that you do not need to derive this formula.
The function that you will be using in this coursework is
1+ (2r+3x²)e-rlx² for x>0
f(x)=
=
{1+
(Equation 2)
for x = 0
Scanned with CamScanner localhost:8888/notebooks/Downloads/MTHS1008%20Coding%20Coursework%202.ipynb
→]
*
migration plan to Notebook 7 to learn about the new features and the actions to take if you are using extensions - Please note that updating to Notebook 7 might break some of your extensions.
Dc
Jupyter MTHS1008 Coding Coursework 2 (unsaved changes)
File
Edit
View
Insert
Cell
Kernel Widgets
Help
+
Run
C▸> Code
8
Logout
Not Trusted
Python 3 (ipykernel) O
where r is a number unique to you.
Note that the variable 'r' is available to any code you write below here.
Run the cell below to see your value.
In []: print(r)|
1. Write a Python function that takes x as input and returns f(x) (given by Equation 2) as output. [1 mark]
2. Plot a graph of f(x) for 0 ≤ x ≤ 1 to show that it is strictly positive for this range. [2 marks]
3. Write a Python function that returns the exact value of I = for f(x) dx.
Your function should take inputs a and b. [4 marks]
Note that the integral of f(x) is x + x³ e−rlx² for x > 0.
4. Write a Python function that implements TW's method to approximate I = f f(x) dx.
Your function should take inputs a, b and N - the number of integration strips in TW's method.
Your function should return the approximate value of for f(x) dx as output.
Equation 1 fails if f (a + (i-1)Ax) = f(a + iAx). In this case, you can approximate the area under the strip as the area under a rectangle. You
should incorporate this into your code. [6 marks]
5. Run your function with a = = 0, b = 1 and N =
€ 1000, print out the approximate value of I and compare it with the exact value. These should be
approximately equal if you have implemented the method correctly. [1 mark]
6. Write a Python function that plots on a log-log graph the absolute value of the difference between the exact and approximate values of fo f(x) dx as
a function of Ax = (ba)/N. What can you deduce from this plot? [6 marks]
[Total marks =20]
Any function you write should have enough comments to allow your code to be understood.
Scanned with CamScanner tion plan to Notebook 7 to learn about the new features and the actions to take if you are using extensions - Please note that updating to Notebook 7 might break some of
Jupyter MTHS1008 Coding Coursework 2 (unsaved changes)
File
Edit
View
Insert
Cell
Kernel Widgets
Help
50
H
+
1
↑
►Run
C▸>> Code
your extensions.
Logout
Not Trusted
Python 3 (ipykernel)
6. Write a Python function that plots on a log-log graph the absolute value of the difference between the exact and approximate values of fo) f(x) dx as
a function of Ax = (ba)/N. What can you deduce from this plot? [6 marks]
[Total marks = 20]
Any function you write should have enough comments to allow your code to be understood.
All variables used should be carefully explained in comments.
If you want to, you can base the structure of your code on the contents of Guided Programming 8.
In [] # Part 1
In []: # Part 2
In [] # Part 3
In []: # Part 4
In [] # Part 5
In [] # Part 6
I
In [] # Add more cells if necessary
Scanned with CamScanner