Advertisement
Guest User

2

a guest
Mar 16th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1.  
  2. public class Runge5 extends Runge2 {
  3.  
  4. public static double f(double x, double y) {
  5. double f = 1000*x;
  6. return f;
  7. }
  8.  
  9.  
  10. public static void main(String[] args) {
  11.  
  12.  
  13.  
  14.  
  15. try{
  16.  
  17. double stepSize = Double.parseDouble(args[0]);
  18.  
  19.  
  20. /* this sets the range of the integral */
  21.  
  22. double initial = 0;
  23. double end = 1;
  24.  
  25. /* steps is total steps and finalY is the value of the integral */
  26.  
  27. double steps = (end - initial) / stepSize;
  28. double i = 0;
  29. double finalY = 0;
  30.  
  31. if (stepSize <= 0)
  32. throw new Exception("Error: Stepsize is negative or 0. \n");
  33.  
  34. for (i = 0; i < steps ; i++){
  35. finalY += rk(initial + i * stepSize,
  36. 0, stepSize);
  37. }
  38.  
  39. System.out.printf("The Runge Kutta method gives the value \n" );
  40. System.out.printf("%1.4f as the integral of x^5 * e^x^3 \n",
  41. finalY);
  42. System.out.printf("on the interval of %1.4f to %1.4f \n",
  43. initial, end );
  44. System.out.printf("using %1.0f steps and a step size of %1.6f \n",
  45. steps,stepSize);
  46.  
  47. if (stepSize > 1.0)
  48. throw new Exception("Warning: Stepsize is large. Values may be off.\n");
  49. }
  50.  
  51. catch(Exception e){
  52. System.out.printf(e.getMessage());
  53. System.out.printf("\nYou need to input a float point value \n");
  54. System.out.printf("For the value of the step size. To get a \n");
  55. System.out.printf("reasonable answer, this step size should \n");
  56. System.out.printf("be fairly small, non-negative and entered \n");
  57. System.out.printf("FUCKas a command line argument, thank you. \n");
  58.  
  59. }
  60.  
  61.  
  62. }
  63.  
  64.  
  65.  
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement