Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package calculator;
  2. import java.util.*;
  3.  
  4.  
  5.  
  6. public class Calc1 {
  7.    
  8.      public static final String ERR_NUMIN = "Non numerical input!";
  9.      
  10.      public static double function(double[] a, double x) {
  11.          
  12.          return a[3]*Math.pow(x, 5) + a[4]*Math.pow(x, 4) + a[5]*Math.pow(x, 3) + a[6]*Math.pow(x, 2) + a[7]*x + a[8];
  13.          
  14.      }
  15.      
  16.      public static double exact(double[] a) {
  17.          return (((a[3]*Math.pow(a[0], 6))/6) + ((a[4]*Math.pow(a[0], 5))/5) + ((a[5]*Math.pow(a[0], 4))/4) + ((a[6]*Math.pow(a[0], 3))/3) + ((a[7]*Math.pow(a[0], 2))/2) + (a[8]*a[0])) - (((a[3]*Math.pow(a[1], 6))/6) + ((a[4]*Math.pow(a[1], 5))/5) + ((a[5]*Math.pow(a[1], 4))/4) + ((a[6]*Math.pow(a[1], 3))/3) + ((a[7]*Math.pow(a[1], 2))/2) + (a[8]*a[1]));
  18.      }
  19.      
  20.      public static  double simpsons(double midpoint, double trapezoid) {
  21.         double total = ((2.0*midpoint)+trapezoid)/3;
  22.         return total;
  23.      }
  24.      
  25.      public static double trapezoid(double[] a) {
  26.           double interval = 0;        
  27.           double looped = 0;
  28.           double x = 0;
  29.          
  30.           interval = (a[0] - a[1]) / a[2];    
  31.          
  32.           looped = 0.5 * (function(a, a[1]) + function(a, a[0]));    
  33.          
  34.           for (int i = 1; i < a[2]; i++) {
  35.              x = a[1] + i * interval;
  36.              looped += function(a, x);
  37.           }
  38.  
  39.           return looped * interval;
  40.      }
  41.      
  42.      public static double midpoint(double[] a) {
  43.          
  44.          double dx = 0;
  45.          double looped = 0;
  46.          double total = 0;
  47.          
  48.          dx = (a[0]-a[1])/a[2];
  49.          
  50.          for(int i = (int) a[1]+1; i<a[0]; i += dx) {
  51.              looped += function(a, i);
  52.          }
  53.          
  54.          total = dx*looped;
  55.          
  56.          
  57.         return total;
  58.      }
  59.      
  60.      public static double percentError(double Exact, double Theoretical) {
  61.          return Math.abs(((Theoretical-Exact)/Exact)*100);
  62.      }
  63.    
  64.     public static void main(String[] args) {
  65.        
  66.         double[] userData = new double[9];
  67.         Scanner input = new Scanner(System.in);
  68.        
  69.         System.out.printf("%s", "Input the limits of integration(maximum first):");
  70.         try {
  71.             userData[0] = input.nextDouble();
  72.             userData[1] = input.nextDouble();
  73.         }
  74.         catch(Exception e) {
  75.             System.out.printf("%s%n", "ERR_NUMIN");
  76.         }
  77.        
  78.         System.out.printf("%s%n", "Enter the number of sub intervals");
  79.         try {
  80.             userData[2] = input.nextDouble();
  81.         }
  82.         catch(Exception e) {
  83.             System.out.printf("%s%n", "ERR_NUMIN");
  84.         }
  85.        
  86.         System.out.printf("%s%n", "Enter the coefficients for a 5th degree polynomial");
  87.         try {
  88.             userData[3] = input.nextDouble();
  89.             userData[4] = input.nextDouble();
  90.             userData[5] = input.nextDouble();
  91.             userData[6] = input.nextDouble();
  92.             userData[7] = input.nextDouble();
  93.             userData[8] = input.nextDouble();
  94.         }
  95.         catch(Exception e) {
  96.             System.out.printf("%s%n", "ERR_NUMIN");
  97.         }
  98.        
  99.         System.out.printf("%n%s%.2f%n%n", "Exact Value: ", exact(userData));
  100.         System.out.printf("%s%.2f%n%s%.2f%s%n%n", "Trapezoid Rule: ", trapezoid(userData), "Percent Error: ", percentError(exact(userData), trapezoid(userData)), "%");
  101.         System.out.printf("%s%.2f%n%s%.2f%s%n%n", "Simpsons: ", simpsons(midpoint(userData), trapezoid(userData)), "Percent Error: ", percentError(exact(userData), simpsons(midpoint(userData), trapezoid(userData))), "%");
  102.         System.out.printf("%s%.2f%n%s%.2f%s%n", "Midpoint Rule: ", midpoint(userData), "Percent Error: ", percentError(exact(userData), midpoint(userData)), "%");
  103.  
  104.         input.close();
  105.        
  106.     }
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement