Advertisement
ptrawt

CPE208-Modified Euler's Method

Sep 8th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. public class EulerModified {
  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.             h = (b-a)/n[i];
  12.             for(int j = 0 ; j < n[i] ; j++){
  13.                 y *=(1+h+(0.5*Math.pow(h, 2)));
  14.             }
  15.             if(i < 3)
  16.                 System.out.println("n = "+ n[i] +";\t\t"+y+"\terror = "+(Math.E-y)+"\tlog|error| = "+ Math.log10(Math.E-y));
  17.             else
  18.                 System.out.println("n = "+ n[i] +";\t"+y+"\terror = "+(Math.E-y)+"\tlog|error| = "+ Math.log10(Math.E-y));
  19.             y = x;
  20.         }
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement