//  -----------------------------------------------------------------
//  -------  Numerische Mathematik                   ----------------
//  -------  Ib 03                                   ----------------
//  -------  Runge-Kutta 4. Ordnung                  ----------------
//  -------                                          ----------------
//  -------  Prüfung 4.2                             ----------------
//  -------                                          ----------------
//  -------  2005 March 17      Manfred Vogel        ----------------
//  -----------------------------------------------------------------


public class Runge_Kutta
{
	public static double y0  = 1;	  // Anfangsbedingung  
	public static double t0  = 0;
	public static double tEnd= 5;
	public static double h   = 0.01;
	public static int    n   = (int) ((tEnd-t0)/h);

	// Arrays
	public static double [] t  = new double[n+1];
	public static double [] w  = new double[n+1];	//
		
	// DG
	public static double f(double t, double y) { return Math.cos(2*t)+Math.sin(3*t); }
	
	public static void main (String [] args)
	{
		//getInfo();
		System.out.println("\n\n");
		rk(y0, t0, tEnd, h , n);
		print();
	}
	
	public static void rk(double y0, double t0, double tEnd, double h, int n)
	{
		t[0]     = t0;
		w[0]     = y0;
		
		for(int j=0; j<n; j++)
		{
			double k1 = h * f( t[j]         , w[j]         );
			double k2 = h * f( t[j] + h/2.0 , w[j]+k1/2.0  );
			double k3 = h * f( t[j] + h/2.0 , w[j]+k2/2.0  );
			double k4 = h * f( t[j] + h     , w[j]+k3      ); 
			t[j+1] = t[j] + h; 
			w[j+1] = w[j] + 1.0/6.0*(k1 + 2*k2 +2*k3 + k4);
		}
	}
	
	public static void print(){
		System.out.println("Ausgabe: Approximation der gegebenen DG mit Runge-Kutta \n");
		for(int i = 0; i<w.length; i=i+10)
		{
			System.out.print("        t: ");
			InOut.print(t[i], 2,2);
			System.out.println("\t\tw["+i+"]: " + w[i]);
		}
	}
}

