0% found this document useful (0 votes)
15 views3 pages

Assignment Ex04

This document describes an exercise on numerical methods for solving ordinary differential equations (ODEs). It involves implementing Euler's method, a predictor-corrector method, and the fourth-order Runge-Kutta method to solve the ODE for an anharmonic oscillator. Students are asked to assess the scaling of the endpoint error with decreasing time step for each method and to monitor the conservation of energy for different values of the anharmonicity parameter.

Uploaded by

Melbin Siby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Assignment Ex04

This document describes an exercise on numerical methods for solving ordinary differential equations (ODEs). It involves implementing Euler's method, a predictor-corrector method, and the fourth-order Runge-Kutta method to solve the ODE for an anharmonic oscillator. Students are asked to assess the scaling of the endpoint error with decreasing time step for each method and to monitor the conservation of energy for different values of the anharmonicity parameter.

Uploaded by

Melbin Siby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Computer Exercises for Lecture ”Computational Photonics” (Date: 2023-06-06)

Exercise 04 - Numerical methods for the solution of ODEs

Before you start working on the assignments and before you hand in a solution, read the instructions below.
• You can work on these assignments in groups of up to 3 students. Note, however, that everybody has to
hand in his/her own solution.
• Please personalize your completed script main ex04 ID.py by replacing the handle “ID” by your student-
id. For redundancy, make sure to also include your name and student-id in the completed scripts that you
hand in. We therefore provided three metadata variables

__STUDENT_NAME__ = "YOUR_NAME_HERE"
__STUDENT_ID__ = "YOUR_STUDENT_ID_HERE"
__GROUP_MEMBERS__ = "YOUR_TEAM_HERE"

at the beginning of the script.


• To complete the code, look out for TODO statements, they will provide further hints!
• If you did not discuss your solution with one of the tutors, make sure to hand in the completed script into
the assignment folder Exercise04/Solutions.

Problem 1: A nonlinear ODE: the anharmonic oscillator (2+2+2+2+2=10 points)

The computational problem that we will study in this exercise is a classical mechanics problem for which
Newtons second law, i.e. the equation of motion, reads

d2
m x = −kx(1 − αx). (1)
dt2
It describes the motion of a particle with mass m in the anharmonic potential
 
1 2 2
V (x) = kx 1 − αx , (2)
2 3

see Fig. 1. In Eq. (2), k > 0 is the usual suspension rate and α controls the deviation from the harmonic
oscillator potential. Note that for small displacements x, V (x) is effectively a harmonic oscillator potential. For
larger values of x, the second term in Eq. (2) adds a nonlinear perturbation to the equation of motion. The
right hand side of Eq. (1) is simply the force acting on the particle, given by

d
F (x) = − V (x). (3)
dx
Download the Python script main ex04 ID.py from folder Exercise04. Wade through the script and make
sure you understand it. Get familiar with the fragmented routines that help setting up the solver for the two
coupled ODEs in the two unknowns y (1) and y (2) , reading
d (1)
y = f (1) (t, y (1) , y (2) ), (4)
dt
d (2)
y = f (2) (t, y (1) , y (2) ). (5)
dt
In lecture 5, the rearrangement of the second order differential equation Eq. (1) in terms of two first order
equations of the form of Eqs. (4, 5) was illustrated on slide 24 for the harmonic oscillator potential (α = 0).
Here, for the case of the anharmonic
 oscillator potential, the right hand side of Eq. (5) reads f (2) (t, y (1) , y (2) ) =
(1) (1) (1)
F (y ) = −ky 1 − αy /m.

1
2

Figure 1: Potential of the anharmonic oscillator defined by Eq. (2) for α = 0.5 (blue solid line) and α = 0 (green
dashed line).

(a) Activate the function main a(). Consider the limiting case of the harmonic oscillator, i.e. α = 0 in
Eqs. (1,2). Complete and test the Euler forward stepping algorithm for the set of equations (4,5) and
compare your numerical approximation of the position coordinate x(t) to the exact solution xexact (t) for
α = 0.
Hints:

– To read up on Euler’s method, see slide 4 of lecture 5.


N
– For your numerical experiments feel free to consider k = 1 m and m = 1 kg, i.e. set k=1 and m=1.
p
– The exact solution in case of α = 0 is given by xexact (t) = x0 cos(ωt) + v0 sin(ωt)/ω, with ω = k/m,
see, e.g. here.
– Operations on entire arrays can be performed very efficiently by using numpy arrays. You can convert
any input to a numpy array using the function numpy.asarray. Several useful array-operations are
demonstrated here.

(b) Activate the function main b(), still considering the limiting case α = 0. Assess the scaling behavior
of the endpoint error |x(tmax ) − xexact (tmax )| upon decreasing time increment ∆t for Euler’s forward
stepping algorithm. Which scaling behavior do you expect to find? Is this consistent with your numerical
experiments? Include a brief answer as string variable ANSWER Q1 (If you discuss your solution with one
of tutors this is not required).

(c) Activate the function main c(), still considering the limiting case α = 0. Assess the scaling behavior of
the endpoint error |x(tmax ) − xexact (tmax )| upon decreasing time increment ∆t for the predictor-corrector
method. Which scaling behavior do you expect to find in this case? Is this consistent with your numerical
experiment? Include a brief answer as string variable ANSWER Q1 (If you discuss your solution with one of
the tutors this is not required).
Hints:

– To read up on the predictor-corrector method, see slides 26-27 of lecture 5.


– You can implement the single step update for each component of y indivdually, but be aware that
the abovementioned numpy array-operations allow you to write very concise code for entire arrays.
(d) Activate the function main d(), still considering the limiting case α = 0. Assess the scaling behavior of
the endpoint error |x(tmax ) − xexact (tmax )| upon decreasing time increment ∆t for the fourth-order Runge-
Kutta method. Which scaling behavior do you expect to find in this case? Is this consistent with your
numerical experiment? Include a brief answer as string variable ANSWER Q1 (If you discuss your solution
with one of the tutors this is not required).
Hints:
3

– To read up on the fourth-order Runge-Kutta method, see slides 28-33 of lecture 5.


– Since you have to pre-compute the values k1 . . . k4 for all the four stages of the Runge-Kutta algorithm
you will benefit from using the numpy array-operations mentioned above.

(e) Activate the function main e(). This time, you will consider the general case of nonzero α for which no
exact solution is given. In absence of an exact solution, conservation laws are a valuable tool to assess the
correctness of a numerical procedure. In this regard, monitor the trend of the total energy E of the particle
as function of integration time for both numerical schemes. What do you find? Include a brief answer as
string variable ANSWER Q1 (If you discuss your solution with one of the tutors this is not required).
Hints:
– For your numerical experiments feel free to set alpha=1.
m 2
– The total energy at a given instant t in time reads E(t) = 2 v(t) + V (x(t)).

You might also like