Ordinary differential equaltions: Solutions by finite difference schemes
Ordinary differential equations 1st order ode Taylor expansion of x(t) about time t we thus have an approximation for x(t+dt) in terms of x(t) this is the (explicit) Euler discretization scheme
we can get a better approximation if we take two steps to get from t to t+dt first get an approximation for x(t+dt/2) using the Euler discretization then make the full step using the function evaluated at this midpoint this is the 2nd order Runge-Kutta discretization
Example: Analytic solution: Euler method: So this misses the term as expected Runge-Kutta method: So this misses the term, as expected
#include <stdio.h> #include <stdlib.h> #include <math.h> /* set up the finite-difference parameters */ const int runLength=1000; const double delta_t=0.01; /* set up the function prototypes */ double Euler(double x,double t); double RungeKutta(double x,double t); double function(double x,double t); void printData(double *xEuler,double *xRungeKutta); main() { /* define the arrays to store the data */ double xEuler[runLength]={0.0},xRungeKutta[runLength]={0.0},t=0.0; /* set the initial conditions */ xEuler[0]=1.0; xRungeKutta[0]=1.0; /* evolve the solutions using the two integration methods */ for(int n=0;n<runLength-1;++n){ xEuler[n+1]=Euler(xEuler[n],t); xRungeKutta[n+1]=RungeKutta(xRungeKutta[n],t); t=t+delta_t; } printData(xEuler,xRungeKutta);
general 2nd order ode: write as two first order equations solve using 2nd order Runge-Kutta half step full step
Example: Simple harmonic oscillator Initial conditions: Analytic solution: i.e. Runge-Kutta method:
Choosing an algorithm: accuracy how accurate is the algorithm? how accurate does the answer need to be? efficiency can I use a different, more efficient algorithm? can I implement the current algorithm more efficiently? stability is the algorithm stable? Can I avoid instabilities?
Numerical stability: numerical methods approximate continuous differential equations by discrete difference equations the difference equations may exhibit instabilities even if the continuous differential equations don’t two possible reasons are: time-step problems competing solutions
Time-step problems: e.g. has the analytic solution which is a decaying exponential The Euler approximation has Which we can analyze by changing dt good solution ------------------------- vanishing solution-------------------- decaying oscillations--------------- constant amplitude oscillations--- increasing amplitude oscillations-
=1.0)
Competing solutions: e.g. with boundary conditions: has the analytic solution which is a decaying exponential: However, the general solution is there are small errors in any finite difference scheme these errors have the effect of introducing some of the growing mode solution. This leads to a numerical solution which grows, rather than decays.