//simple demo of runga-kutta method to solve diff-eq //this example solves the equation y'= x^2 + y where f(0)=-1 public class rungakutta { public static void main ( String args[] ) { //for this example we are going to get the answer //at 0.10 since f(0) is our starting point the //difference between 0 and .1 is .1 double h = .1; //now we do 4 iterations to get our answer double x0 = 0; double y0 = -1; double k1 = (solve( x0, y0 ) )*h; double k2 = (solve( x0+(h/2), y0+(k1/2)) )*h; double k3 = (solve( x0+(h/2), y0+(k2/2)) )*h; double k4 = (solve( x0+h, y0+k3) )*h; double y1 = y0 + k4; System.out.println ( "k1 " + k1 + " k2 " + k2 + " k3 " + k3 + " k4 " + k4 ); System.out.println ( "result " + y1 ); } static double solve ( double x, double y ) { System.out.println ( "x " + x + " y " + y ); // x^2 + y this is the diff-eq we are solving return ( x*x + y ); } }