COSC 1002/1902 Computational Science in C
Practical 5
This practical session provides an introduction to the numerical solution of ordinary differential equa
tions (ODEs) in C, using Euler’s method. In this practical you will implement your own version of
Euler’s method, and then apply it.
Before you begin these exercises, you should review the material in lecture 5. Download the code
simple_euler.c
via WebCT or from the website
http://www.physics.usyd.edu.au/
∼
wheat/cosc1002
pages and compile and run the code, and conﬁrm that you obtain the results described in the lecture
notes. After you do this you will be ready to begin the exercises. If necessary consult a tutor, who can
provide assistance.
The exercises are for students in both units except where indicated (‘1002’ means that the question
is just for students in COSC 1002, and ‘1902’ means that the question is just for students in COSC
1902). Remember to have a tutor mark off checkpoints as you reach them.
Exercises
1. In lecture 5 the nondimensional version of the ODE for Malthusian population growth
dy
dx
=
y
(1)
was introduced. The initial value problem presented by this ODE together with
y
(0) = 1
has
the exponential solution
y
=
e
x
.
The code
simple_euler.c
solves this ODE via Euler’s method, to determine
y
(1)
. Accord
ing to the exponential solution, the exact answer is
e
1
=
e
≈
2
.
71828
. As shown in the lecture,
the code produces a rough approximation to the correct value, for 10 integration steps.
The code
simple_euler.c
was kept simple to illustrate the implementation of the Euler
algorithm in code. In this exercise you will write an improved, modular program, which is: 1.
easier to modify to solve other ODEs of the form
dy
dx
=
f
(
x, y
);
and 2. easy to modify to implement algorithms other than Euler’s method. Following the ap
proach that functions represent modules, this program requires:
•
a function to represent the right hand side of the ODE being solved, i.e.
f
(
x, y
)
;
•
a function implementing one step of Euler’s method.
To make the problem easier, I have written a
main
program for you (listed below), and you will
not need to alter this. You will need to write the two functions, which are called
derivs()
and
euler()
respectively. The
main
code is available on the unit pages.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentYou need to ﬁll in the dots. Please study the code ﬁrst, to understand the design. This code is
somewhat more complicated than it needs to be for the problem at hand, but it has been written
to make it easy to modify and apply to other problems.
/
*
ode_exp_euler.c
*
Example code to solve dy/dx=y using Euler’s method.
*
/
#include <stdio.h>
#define N 1 /
*
number of ODEs
*
/
#define NSTEP 10 /
*
number of integration steps
*
/
#define XMIN 0.0 /
*
starting point for integration
*
/
#define XMAX 1.0 /
*
stopping point for integration
*
/
#define Y0 1.0 /
*
initial value
*
/
void derivs(float xin, float yin[], float dydx[]);
This is the end of the preview.
Sign up
to
access the rest of the document.
 Three '09
 wheatland
 Numerical Analysis, Boundary value problem, Euler, Euler’s Method, Numerical ordinary differential equations

Click to edit the document details