Monday, May 01, 2006

Physics 20 Reading List #6

1. Runge-Kutta and vectors

In this assignment the use of the word "vector" is sometimes confusing. The Runge-Kutta method you will write is supposed to apply to an arbitrary number of variables, and we write these variables as a vector simply as a convenient notation that saves ink and paper.

For example, if you have variables y0, y1, ... yn, that obey equations

d/dt y0 = f0(t,y0,y1,...yn)
d/dt y1 = f1(t,y0,y1,...yn)
.
.
.
d/dt yn = fn(t,y0,y1,...yn)

where the f0,f1, etc are n+1 different functions, then it is much easier to write


d/dt y = f(t,y)

where y is a "vector" of variables and f is a "vector" of functions.
When you do this, then you can write formulas in a manner that does not depend on how many variables you have. For example, the midpoint method written in vector language is

k1= y + h/2 f(t,y)
y(t+h)= y + h f(t+h/2,k1)


2. C++ issues

In this assignment you will use even more pieces of the C++ standard template library or STL. You have used some STL classes before, like the C++ vector, and now you will use another class, valarray. The nice thing about valarrays is that you can perform operations on them like multiplication by a number, adding two valarrays, etc. By using valarrays your Runge-Kutta routine will look almost exactly like the Runge-Kutta formula you see written out in a book (or in the assignment). Having formulas in your code look like the natural way you write down the same formulas on paper is a huge benefit because it makes debugging much easier.

Here is an example using valarray so you can see the syntax and some of the possible operations.

0 Comments:

Post a Comment

<< Home