runge_sys
Computes an approximate solution to a system of first-order ODE's using the 4th order Runge-Kutta method.
You're viewing an older version of this page (#391). View the current version.
Interface
#include <codecogs/maths/calculus/ode/runge_sys.h>
using namespace Maths::Calculus::Ode;
FUNCTION
runge_sys
Consider a continuous function, where I is a real interval and p is a positive integer. This module solves the following system of ordinary differential equations:
where ,
and the unknown is the function
,
being composed of
separate functions
,
.
If F is written as being composed of p separate functions
,
, then the above system can be rewritten equivalently as follows:
This module approximates the solution of the above system at equally spaced abscissas using the recurrence relation:
where
and . Hence the abscissas
divide a given interval
in equal segments. The previous recurrence is known as the 4th order Runge-Kutta method for solving a system of first-order ODE's.
References
C-XSC, A C++ Class Library for Extended Scientific Computing, http://www.rz.uni-karlsruhe.de/~iam/html/language/cxsc/node14.html
Example 1
Next we give a fully documented example in which the solution of the following system is approximated on the interval using a step of
:
which can be rewritten equivalently as:
Notice that we have defined a new data type D_VECTOR to make it easier to declare variables or specify functions which are needed by this module.
#include <codecogs/maths/calculus/ode/runge_sys.h>
#include <stdio.h>
#include <math.h>
// precision constant
#define H 0.1
// limits of the approximation interval
#define A 0.0
#define B 1.0
// the given function
D_VECTOR f(double x, D_VECTOR Y)
{
// in this case f is independent of x
D_VECTOR Z;
// the system of differential equations
Z.push_back( Y[1]*Y[2] );
Z.push_back( -Y[0]*Y[2] );
Z.push_back( -0.522*Y[0]*Y[1] );
return Z;
}
int main()
{
// initial values vector
D_VECTOR Y0;
Y0.push_back( 0.0 );
Y0.push_back( 1.0 );
Y0.push_back( 1.0 );
// compute the approximate solution
std::vector<D_VECTOR> sol =
Maths::Calculus::ODE::runge_sys(f, Y0, A, B, H);
// display the results
for (int i = 0; i < sol.size(); i++)
printf("x = %.2lf Y[0] = %.10lf Y[1] = %.10lf Y[2] = %.10lf\n",
H*i + A, sol[i][0], sol[i][1], sol[i][2]);
return 0;
}Output
x = 0.00 Y[0] = 0.0000000000 Y[1] = 1.0000000000 Y[2] = 1.0000000000
x = 0.10 Y[0] = 0.0997468763 Y[1] = 0.9950128339 Y[2] = 0.9973998103
x = 0.20 Y[0] = 0.1979929460 Y[1] = 0.9802034182 Y[2] = 0.9897155790
x = 0.30 Y[0] = 0.2933197040 Y[1] = 0.9560143620 Y[2] = 0.9772864890
x = 0.40 Y[0] = 0.3844633096 Y[1] = 0.9231402010 Y[2] = 0.9606466378
x = 0.50 Y[0] = 0.4703690538 Y[1] = 0.8824697046 Y[2] = 0.9404832898
x = 0.60 Y[0] = 0.5502233243 Y[1] = 0.8350174154 Y[2] = 0.9175872973
x = 0.70 Y[0] = 0.6234622825 Y[1] = 0.7818532761 Y[2] = 0.8928020595
x = 0.80 Y[0] = 0.6897598251 Y[1] = 0.7240380715 Y[2] = 0.8669765655
x = 0.90 Y[0] = 0.7489996600 Y[1] = 0.6625701827 Y[2] = 0.8409264404
x = 1.00 Y[0] = 0.8012372763 Y[1] = 0.5983465271 Y[2] = 0.8154050061