genRungeKutta
in genMath.lib

Solver Functions            Function List by Category            Alphabetical Function List

Sample Code

int genRungeKutta(
                                 double     x,                         // initial value of x
                                 double     *y,                       // pointer to array y
                                                                         // y[0] should be initial value
                             
    double     (*fx)(double, double),             // pointer to user's f(x,y)
                                 double     h,                         // step size for x
                                 int           n)                         // number of points to compute

This function is used to solve a first order differential equation of the form y' = f(x,y) for
the function y(x).  The function uses the Runge-Kutta numerical algorithm.

To use this function, the calling program should define the function f(x,y) and generate
a function pointer to the f(x,y) code. Click Here for Function Pointer Information.  In 
addition, the calling program should have an array of sufficient size (n+1 elements) to 
hold the result function values, y(x).  Before calling genRungeKutta, the y array 
zero-th element, y[0], should contain the initial value, y(0).  

The return code is errno, defined in the C standard library (math.h).  This is an integer value
for math errors.

Sample Application:
///////////// splRK.c for genRungeKutta Library function //////////
//                                      			 //
// As a sample, f(x,y) = x + y		                         //
// 								 //
// This sample program solves y' = f(x,y) = x+y for y(x)	 //
// x0 = 0, y(0) = 0, n = 5 and h = 0.2				 //
//								 //
///////////////////////////////////////////////////////////////////
////////////////////////// INCLUDES ///////////////////////////////
#include "stdio.h" 
#include "genMath.h" 
////////////////////////// FUNCTIONS //////////////////////////////
double fnTest(double x, double y); 		// f(x,y)
////////////////////////////// Main //////////////////////////////
void main()
{
	//local declarations
        //integers
	int     i;              // index for y(x)
        int    retcode;         // return code for call to genRungeKutta
        //doubles
	double y[6]; 		// array for y(x);
				// array size should be n+1
				// y[0] MUST hold y0 initial value 
	//character strings
	char  ch;		// input string to exit
	// initialize y array
	// y[0] would be initialized to whatever your initial
	// condition for y(0).  The others are just set to zero.
	y[0] = 0; 
	y[1] = 0; 
	y[2] = 0; 
	y[3] = 0; 
	y[4] = 0; 
	y[5] = 0;
	// call the solver, passing x0, y0, pointer to f(x,y), step and 
	// number of points 
	retcode = genRungeKutta(0,y,fnTest,0.2,5); 
	//display results 
	printf("Solved y' = (x + y) numerically, x = 0.2, 0.4, ..\n\n"); 
	for(i=0; i <= 5; ++i){ 
		printf("%s%f%s", "y(", i * 0.2, ") = "); 
		printf("%f%s", y[i], "\n"); } printf("\n"); 
	} 
	//display closing message and wait for <enter> before exiting
	printf("Press <Enter> to end");
	scanf("%c", &ch);
} // End Main 
// function f(x,y); the user must include f(x,y) and pass 
// pointer to this function in call to genRungeKutta. 
double fnTest(double x, double y)
{
	//Substitute your code for f(x,y) here:
	x += y; return(x);
}
// End fnTest 
///////////////////////// End splRK.c //////////////////////////// 
The exact solution for this equation is given by y = ex - x -1.
The reader may compare the exact, solution to the following numerical
results.
Ouput from the test program splRK.c: 
Solved y' = (x + y) numerically, x = 0.2, 0.4, ... 
y(0.000000) = 0.000000 
y(0.200000) = 0.021400 
y(0.400000) = 0.091818 
y(0.600000) = 0.222106 
y(0.800000) = 0.425521 
y(1.000000) = 0.718251