Advertisement
ptrawt

CPE208-Runge Kutta's Method

Sep 8th, 2013
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. public class Runge {
  2.     public static void main(String[] args){
  3.         double a,b,x,y;
  4.         double h;
  5.         int[] n = new int[]{10, 20, 50, 100, 200, 500, 1000};
  6.         a = 0;
  7.         x = b = 1;
  8.         y = x;
  9.        
  10.         for(int i = 0 ; i < n.length ; i++){
  11.             double k1,k2,k3,k4;
  12.             h = (b-a)/n[i];
  13.             for(int j = 0 ; j < n[i] ; j++){
  14.                 k1=y;
  15.                 k2=y+((h*k1)/2);
  16.                 k3=y+((h*k2)/2);
  17.                 k4=y+(h*k3);
  18.                 y=y+((h/6)*(k1+(2*k2)+(2*k3)+k4)); 
  19.             }
  20.             if(i < 3)
  21.                 System.out.println("n = "+ n[i] +";\t\t"+y+"\terror = "+(Math.E-y)+"\tlog|error| = "+ Math.log10(Math.E-y));
  22.             else
  23.                 System.out.println("n = "+ n[i] +";\t"+y+"\terror = "+(Math.E-y)+"\tlog|error| = "+ Math.log10(Math.E-y));
  24.             y = x;
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement