where is the independent variable, are the dependent variable, and underlines denote vectors.
This looks like a first-order ODE, but it can represent a higher order ODE with a simple "chain" of variables. E.g., can be implemented by defining , and
We can also eliminate explicit dependence by making it one of the elements of .
Note: I'm going to drop the underlines now, but and are still vectors.
Euler 1st order:
*** If we require that only the function be specified in analytic form, not its derivatives, then has to be estimated from previous or future values of in some clever way.
where is the Euler's method approximation for .
This is equivalent to
where
General principles:
The API can be very simple: just one function to advance one step.
The ODE step function takes three arguments:
There are two options for how to return the updated value:
The function takes a little more thought.
The straight-forward way: pass-by-value, return a value:
vector <double> f( vector<double> y );
Pass-by-reference (in and out):
void f(const vector<double> &y, vector<double> &f_out);
The second way is up to a factor of 2 faster because it requires less copying of data between memory locations.
typedef void f_vect_vect_t(const vector<double> &y, vector<double> &f_out); void rk4step(vector<double> &y, f_vect_vect_t * fptr, double h);
Good "namespace" citizenship:
We know the solutions to and to . Let's test the Euler ODE solver with that case.
Code: see course web page.
Position vs time:
It's spiralling outward. Energy is not being conserved!
Momentarily using the underline notation again, the vector is a vector in phase space. It is supposed to follow a closed path, but the finite steps in the 1-st order Euler ODE solver don't allow that.
The harmonic oscillator and orbit problems, and many others in science in engineering, are examples of solutions of Hamiltonian equations:
The solutions should conserve . The Euler and Runge-Kutta schemes don't.
The simplest (and very common) cases arise from , so that and .
A 1st-order symplectic version of Euler's equation:
A beautiful 2nd-order symplectic method:
Note these symplectic methods need to distinguish between and .