Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calculator;
- import java.util.*;
- public class Calc1 {
- public static final String ERR_NUMIN = "Non numerical input!";
- public static double function(double[] a, double x) {
- 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];
- }
- public static double exact(double[] a) {
- 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]));
- }
- public static double simpsons(double midpoint, double trapezoid) {
- double total = ((2.0*midpoint)+trapezoid)/3;
- return total;
- }
- public static double trapezoid(double[] a) {
- double interval = 0;
- double looped = 0;
- double x = 0;
- interval = (a[0] - a[1]) / a[2];
- looped = 0.5 * (function(a, a[1]) + function(a, a[0]));
- for (int i = 1; i < a[2]; i++) {
- x = a[1] + i * interval;
- looped += function(a, x);
- }
- return looped * interval;
- }
- public static double midpoint(double[] a) {
- double dx = 0;
- double looped = 0;
- double total = 0;
- dx = (a[0]-a[1])/a[2];
- for(int i = (int) a[1]+1; i<a[0]; i += dx) {
- looped += function(a, i);
- }
- total = dx*looped;
- return total;
- }
- public static double percentError(double Exact, double Theoretical) {
- return Math.abs(((Theoretical-Exact)/Exact)*100);
- }
- public static void main(String[] args) {
- double[] userData = new double[9];
- Scanner input = new Scanner(System.in);
- System.out.printf("%s", "Input the limits of integration(maximum first):");
- try {
- userData[0] = input.nextDouble();
- userData[1] = input.nextDouble();
- }
- catch(Exception e) {
- System.out.printf("%s%n", "ERR_NUMIN");
- }
- System.out.printf("%s%n", "Enter the number of sub intervals");
- try {
- userData[2] = input.nextDouble();
- }
- catch(Exception e) {
- System.out.printf("%s%n", "ERR_NUMIN");
- }
- System.out.printf("%s%n", "Enter the coefficients for a 5th degree polynomial");
- try {
- userData[3] = input.nextDouble();
- userData[4] = input.nextDouble();
- userData[5] = input.nextDouble();
- userData[6] = input.nextDouble();
- userData[7] = input.nextDouble();
- userData[8] = input.nextDouble();
- }
- catch(Exception e) {
- System.out.printf("%s%n", "ERR_NUMIN");
- }
- System.out.printf("%n%s%.2f%n%n", "Exact Value: ", exact(userData));
- System.out.printf("%s%.2f%n%s%.2f%s%n%n", "Trapezoid Rule: ", trapezoid(userData), "Percent Error: ", percentError(exact(userData), trapezoid(userData)), "%");
- 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))), "%");
- System.out.printf("%s%.2f%n%s%.2f%s%n", "Midpoint Rule: ", midpoint(userData), "Percent Error: ", percentError(exact(userData), midpoint(userData)), "%");
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement