An Integral Calculator for Polynomials

About This Calculator

It's pretty straight forward in methodology. It takes in arguements for the upper bound, lower bound, and polynomial function from the user and then makes an estimate (which is pretty accurate) the value of the definite interval of that polynomial function using the upper and lower bounds.

The Algorithm

Simply explained, the polynomial function entered is in a string format and therefore needs to be parsed to be able convert the string into mathematical semantics. The algorithm then creates two ArrayLists that records the power and coefficient of each term respectively. The lists are then passed through a function which has a loop which calculates the output of the polynomial function for a given term of x. Being able to calculate the value of the polynomial function now, the algorithm uses the trapezoid method of estimation for definite integrals for a 1,000,000 intervals in a loop. The answer is then outputted.

Limitations

The user input unfortunately has to be very strict in formatting where each term must specify +/- ax^n where +/- denotes a positive or negative term, a denotes the coefficient, and n denotes the power of the term. It can also only calculate polynomials. Can I code it to include more functions and less strict formatting? Theoretically, yes. In actuality, I'm lazy and parsing is way too much work to code code code so we're stuck with strict polynomial inputs only. Ok have fun. Or use SymboLab 💀

import java.util.ArrayList;

public class Polynomial {
  ArrayList<Integer> powersList = new ArrayList<>();
  ArrayList<Integer> coefficientsList = new ArrayList<>();

  public static Polynomial parse (String input) throws Exception {
    Polynomial polynomial = new Polynomial();

    for (int i = 0; i < input.length();){
      int xIndex = input.indexOf("x", i);
      if (xIndex == -1){
        throw new Exception("Invalid input, x variable expected");
      }
      String coeffString = input.substring(i, xIndex);
      int coefficient = Integer.valueOf(coeffString);
      polynomial.coefficientsList.add(coefficient);

      i = xIndex + 2;

      int nextCoeffSign = 1;

      int pIndexPos = input.indexOf("+", i);
      int pIndexNeg = input.indexOf("-", i);
      int pIndex = pIndexPos;

      if (pIndexNeg != -1 && (pIndexNeg < pIndexPos || pIndex == -1)) {
        pIndex = pIndexNeg;
        nextCoeffSign = -1;
      }
      
      if (pIndex == -1){
        pIndex = input.length();
      }

      String powString = input.substring(i, pIndex);
      int power = Integer.valueOf(powString);
      polynomial.powersList.add(power);

      if (nextCoeffSign == -1){
        i = pIndex;
      }
      else {
        i = pIndex + 1;
      }
    }

    return polynomial;
  } 

  public double polynomialOutput(double x){
    double output = 0;

    for (int i = 0; i < powersList.size(); i++){
      output += coefficientsList.get(i) * Math.pow(x, powersList.get(i));
    }
    return output;
  }

  public double polynomialIntegralCalc(double lowerBound, double upperBound){
    int iterations = 1000000;
    double interval = (upperBound - lowerBound)/iterations;
    double output = 0;
    for (int i = 0; i < iterations; i++) {
      double x1 = lowerBound + interval*i;
      double x2 = x1 + interval; 
      
      double lowerOutput = polynomialOutput(x1);
      double upperOutput = polynomialOutput(x2);

      double trapezoidArea = (upperOutput+lowerOutput)*interval/2;
      output += trapezoidArea;
    }

    return output;
  }
}
import java.util.Scanner;

public class PolynomialIntegralCalculator {
  public static void happyCase() throws Exception {
    Scanner userInput = new Scanner(System.in);

    System.out.println("Type your polynomial: must be single variable (x) polynomial with no spaces and all terms with powers specified {Ex: -5x^4+10x^1-2x^0}");
    String input = userInput.nextLine();
    System.out.println("Polynomial Function: " + input);

    System.out.println("Type the lower bound of the integral");
    double lowerBound = Double.valueOf(userInput.nextLine().trim());
    System.out.println("Lower Bound: " + String.valueOf(lowerBound));

    System.out.println("Type the upper bound of the integral");
    double upperBound = Double.valueOf(userInput.nextLine().trim());
    System.out.println("Higher Bound: " + String.valueOf(upperBound));

    userInput.close();

    Polynomial polynomial = Polynomial.parse(input.trim());

    System.out.println(polynomial.polynomialIntegralCalc(lowerBound, upperBound));
  } 

  public static void main(String[] args) throws Exception{
    PolynomialIntegralCalculator.happyCase();
  }
}

PolynomialIntegralCalculator.main(null);
Type your polynomial: must be single variable (x) polynomial with no spaces and all terms with powers specified {Ex: -5x^4+10x^1-2x^0}
Polynomial Function: -5x^4+10x^1
Type the lower bound of the integral
Lower Bound: 0.0
Type the upper bound of the integral
Higher Bound: 2.0
-12.000000000053387