Advertisement
yloplopy

Midpoint - Trapezoid - Simpson's Rule Problem Java

Apr 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3.  
  4. public class Start {
  5.     public static void main(String[] args) {
  6.         doMidpoint();
  7.         doTrapezoid();
  8.         doSimpsons();
  9.     }
  10.    
  11.     private static void doMidpoint() {
  12.         List<Integer> subDivisions = Arrays.asList(10);
  13.         double a = 0;
  14.         double b = 2;
  15.         System.out.println("Midpoint Rule:");
  16.         for(int n : subDivisions) {
  17.             double h = (b-a)/n;
  18.             double answer = 0;
  19.             for(double i = a; i <= (b-h); i+=h) {
  20.                 answer = answer + MidF((i+(i+h))/2);
  21.             }
  22.             answer = answer * h;
  23.             double second = (1/Math.sqrt(2 * Math.PI));
  24.             answer = answer * second;
  25.             answer = answer + 0.5;
  26.             System.out.println("SubDivision " + n + " = " + answer);
  27.         }
  28.         System.out.println("");
  29.     }
  30.    
  31.     private static double MidF(double x) {
  32.         double power = (-1 * Math.pow(x, 2))/2;
  33.         return Math.exp(power);
  34.     }
  35.    
  36.     private static void doTrapezoid() {
  37.         List<Integer> subDivisions = Arrays.asList(10);
  38.         double a = 0;
  39.         double b = 2;
  40.         System.out.println("Trapezoid Rule:");
  41.         for(int n : subDivisions) {
  42.             double h = (b-a)/n;
  43.             double answer = (MidF(a) + MidF(b))/2;
  44.             for(int k = 1; k < n; k++) {
  45.                 answer += MidF((a + (k*h)));
  46.             }
  47.             answer *= h;
  48.             double second = (1/Math.sqrt(2 * Math.PI));
  49.             answer = answer * second;
  50.             answer = answer + 0.5;
  51.             System.out.println("SubDivision " + n + " = " + answer);
  52.         }
  53.         System.out.println("");
  54.     }
  55.    
  56.     private static void doSimpsons() {
  57.         List<Integer> subDivisions = Arrays.asList(10);
  58.         double a = 0;
  59.         double b = 2;
  60.         System.out.println("Simpsons Rule:");
  61.         for(int n : subDivisions) {
  62.             double h = (b-a)/n;
  63.             double answer = MidF(a) + MidF(b);
  64.             for(int k = 1; k < n; k+=2) {
  65.                 answer += (4 * MidF((a + (k*h))));
  66.             }
  67.             for(int k = 2; k < (n-1); k+=2) {
  68.                 answer += (2 * MidF((a + (k*h))));
  69.             }
  70.             answer = (answer * h)/3;
  71.             double second = (1/Math.sqrt(2 * Math.PI));
  72.             answer = answer * second;
  73.             answer = answer + 0.5;
  74.             System.out.println("SubDivision " + n + " = " + answer);
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement