public class Runge2{
public static double f(double x, double y){
double f = Math.pow(x,5) * Math.pow(Math.E,Math.pow(x,3));
return f;
}
/* This is the Runge Function given in class */
public static double rk(double x, double y, double step){
double h = step;
double k1 = h * f(x, y);
double k2 = h * f(x + h/2, y + k1/2);
double k3 = h * f(x + h/2, y + k2/2);
double k4 = h * f(x + h, y + k3);
return y += k1/6 + k2/3+ k3/3 + k4/6;
}
public static void main(String[] args){
try{
double stepSize = Double.parseDouble(args[0]);
/* this sets the range of the integral */
double initial = 0;
double end = 1;
/* steps is total steps and finalY is the value of the integral */
double steps = (end - initial) / stepSize;
double i = 0;
double finalY = 0;
if (stepSize <= 0)
throw new Exception("Error: Stepsize is negative or 0. \n");
for (i = 0; i < steps ; i++){
finalY += rk(initial + i * stepSize,
0, stepSize);
}
System.out.printf("The Runge Kutta method gives the value \n" );
System.out.printf("%1.4f as the integral of x^5 * e^x^3 \n",
finalY);
System.out.printf("on the interval of %1.4f to %1.4f \n",
initial, end );
System.out.printf("using %1.0f steps and a step size of %1.6f \n",
steps,stepSize);
if (stepSize > 1.0)
throw new Exception("Warning: Stepsize is large. Values may be off.\n");
}
catch(Exception e){
System.out.printf(e.getMessage());
System.out.printf("\nYou need to input a float point value \n");
System.out.printf("For the value of the step size. To get a \n");
System.out.printf("reasonable answer, this step size should \n");
System.out.printf("be fairly small, non-negative and entered \n");
System.out.printf("as a command line argument, thank you. \n");
}
}
}