Description
Please answer all the question as described in the file I attached
Unformatted Attachment Preview
MAGIC099 – Exam (2022–2023)
Numerical Methods in Python
Solve the following questions by using your preferred Python platform/IDE, like Jupyter Notebook,
PyCharm or simply a Python script. Then produce a single PDF file containing your codes and
outputs, making sure it is ordered question by question and the codes and outputs are clear and
legible. Submit the PDF file on the MAGIC website as for any other MAGIC exam.
Question 1
The numerical calculation of the irrational number ⇡ = 3.14159 . . . can be performed, among others,
in the following ways.
(a) Compute the area of the regular polygon of n sides that is inscribed in a circle of unity radius,
hence of area equal to ⇡. Take the limit n ! 1.
(b) Compute the area of the regular polygon of n sides that is circumscribed in a circle of unity
radius, hence of area equal to ⇡. Take the limit n ! 1.
(c) Compute it via the Leibniz formula
1 1 1 1
⇡
+
+
··· =
3 5 7 9
4
taking the limit n ! 1, where n is the number of terms in the series.
p
(d) Compute it as the area of the semicircle of radius 2 centred in the origin, that is
Z p2 p
2 x2 dx = ⇡ .
p
1
2
p p
Approximate the integrand on a uniform grid of n points in the interval [
2, 2] and compute
the integral using the method of trapezoids. Take the limit n ! 1.
p
(e) Compute it as the area of the semicircle C of radius 2 centred in the origin, that is
ZZ
dxdy = ⇡ .
C
p p
p
Compute the integral in the x y region given by the intervals x 2 [
2, 2] and y 2 [0, 2]
using the Monte Carlo method with n random points. Take the limit n ! 1.
Compute the approximation of ⇡ for each of the five cases above. Compute the errors (the absolute
value of the di↵erence between ⇡ and the approximation) obtained for all cases versus n, where
4 n 100. Plot the errors versus n in linear, semi-log and log scales.
Question 2
In classical mechanics, point-like particles moving in two physical dimensions subject to gravitational
force follow the set of equations of motion
pi
q̇i =
,
mi
N
X
qi qj
ṗi = G mi
mj
.
|qi qj |3
j=1, j6=i
where G is the gravitational constant, N is the total number of particles, and mi , qi = (qi,x , qi,y ) and
pi = (pi,x , pi,y ) are the mass, position and momentum of the ith particle, respectively. For simplicity,
let us treat all physical variables as non-dimensional (no units) and set G = 1.
Consider first the case of two particles, that is N = 2, with m1 = 1 and m2 = 5.
1/2
MAGIC099 – Exam (2022–2023)
Numerical Methods in Python
(a) Implement your own version of the Runge–Kutta 4 (also called RK45) method to evolve the
initial condition at t = 0 given by
q1 = (3, 0) ,
p1 = (0, 0.8) ,
until t = 10 using a time-step of
to t = 10.
(b) In the x
q2 = (0, 0) ,
p2 = (0.2, 0.4)
t = 0.001. Plot the trajectories of both particles from t = 0
y plane, plot the trajectory from t = 0 to t = 10 of the center of mass defined as
N
1 X
qCM =
mi qi ,
M
where
i=1
M=
N
X
mi .
i=1
(c) Compute the trajectory from t = 0 to t = 10 of the particle #2 with respect to the center of
(CM )
mass, that is q2
⌘ q2 qCM . Use the Splines technique to interpolate the trajectory and,
hence, plot its first and second derivatives with respect to time (both x and y components),
corresponding to particle #2’s velocity and acceleration, respectively.
Consider finally the case of three particles, that is N = 3, with m1 = m2 = 2 and m3 = 3.
(d) Evolve numerically using the Runge–Kutta 4 method the initial condition at t = 0 given by
q1 = ( 3, 0) ,
p1 = (0, 1) ,
q2 = (2, 0) ,
p2 = (0, 1) ,
until t = 5 using a time-step of t = 0.001. In the x
three particles from t = 0 to t = 5.
q3 = (0, 0) ,
p3 = (0, 0)
y plane, plot the trajectories of all the
Question 3
Consider the function
⇣
✓ 3◆
x ⌘2
x
f (x) =
+ exp
sin(x) , x 2 R .
2⇡
10
⇥ 15⇡ 3⇡ ⇤
(a) Plot the function in the interval x 2
and find numerically all its roots within this
14 , 2
interval.
Consider from now on the interval x 2 [0, ⇡] and define there a uniform grid of 101 points.
(b) Using finite di↵erences, compute numerically the first derivative of f (x) and plot it together
with the exact derivative. Plot also the numerical error (absolute value of the di↵erence between
the numerical derivative values and the exact derivative values) versus x. What is the maximum
value of the numerical error?
(c) Using finite di↵erences, compute numerically the second derivative of f (x) and plot it together
with the exact derivative. Plot also the numerical error (absolute value of the di↵erence between
the numerical derivative values and the exact derivative values) versus x. For which value of x
on the numerical grid is the numerical error maximised?
(d) Using an explicit method and choosing a suitable time-step t satisfying the von Neuman
(stability) condition, solve numerically the heat equation for the function u(x, t)
@u
@2u
=
,
@t
@x2
with boundary conditions
u(x = 0, t) = 0
and
u(x = ⇡, t) = 0 ,
for the initial condition given by u(x, t = 0) = f (x), by . Plot the evolution at times t = 0.5 and
t = 1.
2/2
Purchase answer to see full
attachment