runge
Computes an approximate solution to the Cauchy problem using the 4th order Runge-Kutta method.
Controller: CodeCogs
Contents
Interface
C++
Runge
std::vector<double>runge( | double | (*f)(double, double)[function pointer] | |
double | y0 | ||
double | a | ||
double | b | ||
double | h | ) |
Example 1
- Next we give an example of how to use this function and display the absolute error (e) from the exact solution (Y). An approximate solution is found to the following Cauchy problem on the interval using a step of :
which has the exact solution
, with the constant of gravitational acceleration.
This is a mathematical model of the following situation:
A person jumps out of an airplane at a speed of meters per second. Obviously he will feel the effects of gravity and will be accelerated downwards. At the same time he will feel the wind resistance which decreases his acceleration by a factor of up to the point where it becomes zero, when the velocity will become constant.
The solution of the above differential equation then gives the velocity of the person at a precise moment of time in his free fall.
#include <codecogs/maths/calculus/ode/runge.h> #include <stdio.h> #include <math.h> // precision constant #define H 0.1 // initial value of the problem #define Y0 3.0 // limits of the approximation interval #define A 0.0 #define B 1.0 // the given function double f(double x, double y) { return 9.806650 - 1.3*y; } // the exact solution double exact(double x) { return 9.80665/1.3 + (Y0 - 9.80665/1.3)*exp(-1.3*x); } int main() { // compute the approximate solution std::vector<double> sol = Maths::Calculus::ODE::runge(f, Y0, A, B, H); // display the problem data printf("\n"); printf("f(x, y) = g - 1.3*y\n"); printf(" y0 = %.10lf\n\n", Y0); printf(" a = %.3lf\n", A); printf(" b = %.3lf\n", B); printf(" h = %.3lf\n\n", H); // display the results, including error estimation printf("Point Approximation Actual value Error\n\n"); // display the result for (int i = 0; i < sol.size(); i++) printf("x = %.1lf %.11lf %.11lf %.11lf\n", H*i + A, sol[i], exact(H*i + A), fabs(sol[i] - exact(H*i+A))); return 0; }
Outputf(x, y) = g - 1.3*y y0 = 3.0000000000 a = 0.000 b = 1.000 h = 0.100 Point Approximation Actual value Error x = 0.0 3.00000000000 3.00000000000 0.00000000000 x = 0.1 3.55388141096 3.55388278689 0.00000137593 x = 0.2 4.04024231492 4.04024473132 0.00000241639 x = 0.3 4.46731374976 4.46731693250 0.00000318274 x = 0.4 4.84232335469 4.84232708102 0.00000372633 x = 0.5 5.17161768890 5.17162177900 0.00000409009 x = 0.6 5.46076963892 5.46077394871 0.00000430979 x = 0.7 5.71467273264 5.71467714778 0.00000441514 x = 0.8 5.93762395601 5.93762838678 0.00000443076 x = 0.9 6.13339647409 6.13340085106 0.00000437696 x = 1.0 6.30530348701 6.30530775744 0.00000427043
References
- Mihai Postolache - "Metode Numerice", Editura Sirius
Parameters
f the function which describes the Cauchy problem y0 the initial value a the inferior limit of the interval b the superior limit of the interval h the precision constant (the step)
Returns
- a vector containing approximate values of the solution at equally spaced abscissas
Authors
- Lucian Bentea (September 2006)
Source Code
Source code is available when you buy a Commercial licence.
Not a member, then Register with CodeCogs. Already a Member, then Login.