So I'm more or less a noob when it comes to coding. I'm learning java for a university course, and one of my exercises requires me to write a code that evaluates an integral using the left endpoint method, midpoint method and the trapezium method.
For this I need to use a for loop that increments by one step each time. I know how to do this just using double, but the problem is, when I'm running calculations using the double data type, it gives me rounding errors. After reading up (mostly looking at other stack overflow questions), I figured I should use BigDecimal to remove the rounding error.
However using BigDecimal gives me a whole bunch of errors. From what I can tell, I can't use normal operators such as <, >, == etc, because BigDecimal is not a primitive data type. But then I don't know how to do a basic for loop that increments whilst still using BigDecimal. Another thing I looked at here suggested using Commons-math? (second answer down)
https://stackoverflow.com/questions/16707397/whats-wrong-with-this-simple-double-calculation#=
I don't have the faintest idea how to use this as, as far as I can tell, it's not similar to just plain old import java.lang.Math for example.
So I guess my question is, how can I get my code to work using BigDecimal in a for loop, and in comparing numbers together generally, or how can I use Common-Math to simply avoid the rounding error altogether?
Here is my code:
import java.util.Scanner;
import java.lang.Math;
import java.math.BigDecimal;
public class IntegrationMethods{
//calculates the value of the function f(x)=x^2
public static BigDecimal f(BigDecimal x){
return x.multiply(x);
}
//uses a, b and N as arguments, calculates sum of integral using left
endpoint rule
public static BigDecimal leftEndpoint(BigDecimal a, BigDecimal b,
BigDecimal n){
BigDecimal h = (b.subtract(a)).divide(n);
sum = new BigDecimal("0");
i = new BigDecimal("0");
for(i.compareTo(n)<=0;){
sum = sum.add(h.multiply(f(a.add(h.multiply(i-1)))));
i = i.add(new BigDecimal(1));
}
return sum;
}
public static BigDecimal midpoint(BigDecimal a, BigDecimal b, BigDecimal
n){
BigDecimal h = (b.subtract(a)).divide(n);
BigDecimal sum = 0.0;
for(BigDecimal i=0.0; i<n; i++){
BigDecimal x1 = a.add(h.multiply(i));
BigDecimal x2 = a.add(h.multiply(i+1));
sum = sum.add(h.multiply((f(x1).add(f(x2))).divide(2)));
}
return sum;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the lower limit of integration a.");
BigDecimal a = scanner.nextBigDecimal();
System.out.println("Please enter the upper limit of integration b.");
BigDecimal b = scanner.nextBigDecimal();
//swaps a and b so that the bigger of the two is always b.
//this prevents a negative popping up later on.
if(b<a){
BigDecimal r = a;
BigDecimal m = b;
a = m;
b = r;
}
System.out.println("Please enter the number of sampling points N.");
BigDecimal n = scanner.nextBigDecimal();
//checks if n is greater than or equal to 1, and asks for a new value if it isn't.
while (n<1.0){
System.out.println("Please enter a new value for N.");
n = scanner.nextBigDecimal();
}
System.out.println("For " + n + " intervals, the integral of x^2 using the left endpoint rule is: " + leftEndpoint(a,b,n));
System.out.println("Using the midpoint rule, the integral of x^2 is: " + midpoint(a,b,n));
}
}