practical05 - COSC 1002/1902 Computational Science in C...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 confirm 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 non-dimensional 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
You need to fill in the dots. Please study the code first, 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[]);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2009 for the course COSC 1002 taught by Professor Wheatland during the Three '09 term at University of Sydney.

Page1 / 9

practical05 - COSC 1002/1902 Computational Science in C...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online