4/14/11

Numerical Analysis : Runge Kutta Method

Runge Kutta method is a part of numerical analysis. Its an iterative method. To know more read :- CLICK

Java Code for Runga Kutta :



import java.util.Scanner;


  

public class runge {

      

        public static double f(double p,double q){

        return (q/2+3*p);

        }

      

      

        public static void main(String[] args){

            Scanner sc=new Scanner(System.in);

          

            int n,choice,i;

            double x0,y0,h,y1,x1,z0,sum,temp,k1,k2,k3,k4;

          

            int ans;

            System.out.println(" Enter  starting value of x and corr. value of y"+"\n");

            x0=sc.nextDouble();

            y0=sc.nextDouble();

            System.out.println("Enter the value of x for which y is found"+"\n");

            x1=sc.nextDouble();

            System.out.println("Enter the step size"+"\n");

            h=sc.nextDouble();

              

                do{

                    k1=h*f(x0,y0);

                    k2=h*f((x0+h)/2,(y0+k1)/2);

                    k3=h*f((x0+h)/2,(y0+k2)/2);

                    k4=h*f((x0+h),(y0+k3));

                    System.out.println("The solution at " + x1 + "and" + y0 +"\n");

                    System.out.println("k1 "+ k1 +"\n");

                    System.out.println("k2 "+ k2 +"\n");

                    System.out.println("k3 "+ k3 +"\n");

                    System.out.println("k4 "+ k4 +"\n");

                  

                    y1=(k1+2*k2+2*k3+k4)/6;

                    y1=y1+y0;

                  

                    System.out.println(" Solution when h " + h + " is " + y1 +"\n");

                    System.out.println(" Goto next step???(Y=1/N=0)" +"\n");

                    ans=sc.nextInt();

                    if(ans==1){

                        x0=x0+h;

                        y0=y1;

                        x1=x0;

                    }

                }while(ans==1);

        }

      


}


SHARE THIS POST: