Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Integral {
  6.  
  7.     private static Scanner scnr = new Scanner(System.in);
  8.      static int a, b, c, d, e, f;
  9.      static double upperLimit, lowerLimit, numSubInterval;
  10.  
  11.  
  12.     public static void getVal(Scanner scnr) {
  13.         a = scnr.nextInt();
  14.         b = scnr.nextInt();
  15.         c = scnr.nextInt();
  16.         d = scnr.nextInt();
  17.         e = scnr.nextInt();
  18.         f = scnr.nextInt();
  19.     }
  20.     public static void main(String args[]) {
  21.  
  22.         System.out.println("Enter a value for your lower limits: ");
  23.         lowerLimit =scnr.nextDouble();
  24.         System.out.println("Enter a value for your upper limits: ");
  25.         upperLimit =scnr.nextDouble();
  26.  
  27.         System.out.println("Enter number of subintervals n");
  28.         numSubInterval = scnr.nextDouble();
  29.  
  30.         System.out.println("Enter coefficients of an arbitrary 5th degree polynomial (i.e. A,B,C,D,E):  ");
  31.         getVal(scnr);
  32.  
  33.  
  34.         System.out.println(leftPoint());
  35.         System.out.println(rightPoint());
  36.         System.out.println(midPoint());
  37.         System.out.println(trapoezoid());
  38.         System.out.println(simpsons());
  39.         System.out.println(fExact(upperLimit)-fExact(lowerLimit));
  40.         errorRate();
  41.  
  42.     }
  43.  
  44.     /**
  45.      * My two methods that calculate f(x) and the exact value of f(x)
  46.      * @param x
  47.      * @return
  48.      */
  49.     public static double fVal(double x) {
  50.         return (Math.pow(x, 5) * a) + (Math.pow(x, 4) * b) + (Math.pow(x, 3) * c) + (Math.pow(x, 2) * d) + (e * x) + f;
  51.     }
  52.     public static double fExact(double x) {
  53.         return (Math.pow(x, 6) * a) / 6 + (Math.pow(x, 5) * b)/5  + (Math.pow(x, 4) * c)/4 + (Math.pow(x, 3) * d)/3 + (e *Math.pow(x, 2) )/2 + f*x ;
  54.     }
  55.  
  56.     /**
  57.      * All my methods below that calculate the numerical value using Left,Right, Mid point
  58.      * Trapezoid rule and Simpsons.
  59.      * @return f(deltaX)*deltaX
  60.      */
  61.     public static double leftPoint() {
  62.         double deltaX = (upperLimit - lowerLimit) / numSubInterval;
  63.         double sum = 0;
  64.         double counter = lowerLimit;
  65.  
  66.         for (int i = 0; i < numSubInterval - 1; i++) {
  67.             sum += fVal(counter);
  68.             counter += deltaX;
  69.         }
  70.  
  71.         return sum * deltaX;
  72.  
  73.     }
  74.  
  75.     public static double rightPoint() {
  76.         double deltaX = (upperLimit - lowerLimit) / numSubInterval;
  77.         double sum = 0;
  78.         double counter = lowerLimit + deltaX;
  79.  
  80.         for (int i = 0; i < numSubInterval - 1; ++i) {
  81.             sum += fVal(counter);
  82.             counter += deltaX;
  83.         }
  84.         return sum * deltaX;
  85.     }
  86.  
  87.     public static double trapoezoid() {
  88.         return ( rightPoint() + leftPoint() ) / 2;
  89.     }
  90.  
  91.     public static double midPoint(){
  92.         double deltaX = (upperLimit - lowerLimit) / numSubInterval;
  93.         double sum = 0;
  94.         double counter = lowerLimit + (deltaX/2);
  95.  
  96.         for (int i = 0; i < numSubInterval - 1; ++i) {
  97.             sum += fVal(counter);
  98.             counter += deltaX;
  99.         }
  100.         return sum * deltaX;
  101.     }
  102.  
  103.     public static double simpsons(){
  104.  
  105.         return ( (2*midPoint() ) +trapoezoid() ) /3;
  106.     }
  107.  
  108.     /**
  109.      * My method for calculating the error rate and printing each percentage respectively
  110.      */
  111.     public static void errorRate(){
  112.  
  113.         //I declare the exact numerical value of the integral as a variable and use it to make the calculation of the error rate.
  114.         double correctVal= fExact(upperLimit)-fExact(lowerLimit);
  115.         String percent = "%";
  116.         // I use printf to format the percentage up to 5 decimals of precision.
  117.         System.out.printf("The error rate for Left Point Rule is: %.5f%s%n", (leftPoint()/correctVal)*100 ,percent);
  118.         System.out.printf("The error rate for Right Point Rule is: %.5f%s%n", (rightPoint()/correctVal)*100 ,percent);
  119.         System.out.printf("The error rate for Mid Point Rule is: %.5f%s%n", (midPoint()/correctVal)*100,percent );
  120.         System.out.printf("The error rate for Trapezoid Rule is: %.5f%s%n", (trapoezoid()/correctVal)*100,percent );
  121.         System.out.printf("The error rate for Simpsons Rule is: %.5f%s", (simpsons()/correctVal)*100 ,percent);
  122.  
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement