12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

differential equation applications 205✞// rk4 . java : 4th order Runge−Kutta ODE Solver for arbitrary y( t )import java . io .∗ ;☎public class rk4 {✝}public static void main ( String [] argv ) throws IOException , FileNotFoundException {PrintWriter w = new PrintWriter (new FileOutputStream ("rk4 . dat") , true );double h, t , a = 0. , b = 10.; / / Step size , time , endpointsdouble ydumb [ ] = new double [2] , y[] = new double [2] , fReturn [] = new double [2];double k1 [ ] = new double [2] , k2[] = new double [2] , k3[] = new double [2];double k4 [ ] = new double [2];int i , n = 100;y[0] = 3. ; y[1] = −5. ; // Initializeh = (b−a)/n;t = a;w. println ( t + " " + y[0] + " " + y[1]) ; // File outputwhile (t < b) { / / Time loopif ( ( t + h) > b ) h = b − t; // Last stepf (t , y, fReturn ) ; // Evaluate RHS’s , return in fReturnk1[0] = h∗fReturn [0]; k1[1] = h∗fReturn [1]; / / Compute function valuesfor ( i = 0 ; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!