summaryrefslogtreecommitdiff
path: root/Integration/src/Integrate.java
blob: 5eac9f16be7f73cf56a0ddf3839d5c3d49051ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * @author Simon O'Shea
 */
import java.text.DecimalFormat;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Integrate {
	
public static double functionValue(double A, double B, double C, double D, double E, double F, double x) {
		
		double value = 0;
		
		value = (A * Math.pow(x, 5)) + (B * Math.pow(x, 4)) + (C * Math.pow(x, 3)) + 
					(D * (Math.pow(x, 2)) + (E * x) + F); 
		
		return value;
		
}
	

public static double antiDerivative(double A, double B, double C, double D, double E, double F, double a, double b) {
	
	A = A / 6.0; B = B / 5.0; C = C / 4.0; D = D / 3.0; E = E / 2.0;
	
	double first = (A * Math.pow(a, 6)) + (B * Math.pow(a, 5)) + (C * Math.pow(a, 4)) + (D * Math.pow(a, 3)) +
					(E * Math.pow(a, 2)) + (F * a); 
	
	double last = (A * Math.pow(b, 6)) + (B * Math.pow(b, 5)) + (C * Math.pow(b, 4)) + (D * Math.pow(b, 3)) +
			(E * Math.pow(b, 2)) + (F * b);  
	

	
	return last - first;
	
}


public static double midpointRule(double A, double B, double C, double D, double E, double F, double startInterval, double endInterval, double numInterval) {
		
		double sum = 0, temp;
		double width = (endInterval - startInterval) / numInterval;
		double start = startInterval + (width / 2.0);
		
		for(double i = start; i <= endInterval; i = i + width) {
				
				temp = functionValue(A, B, C, D, E, F, i);
				sum += temp;
				
		}
		
	return sum * width;
		
}
	

public static double trapezoidRule(double A, double B, double C, double D, double E, double F, double startInterval, double endInterval, double numInterval) {
	
	double sum = 0, b1, b2;
	double width = (endInterval - startInterval) / numInterval;	

	for(double i = startInterval; i < endInterval; i = i + width) {
		
		
		
		b1 = functionValue(A, B, C, D, E, F, i);
		b2 = functionValue(A, B, C, D, E, F, (i + width));
		
		
		sum += (width * ((b1 + b2) / 2.0));
		
	}
	
	return sum;
	
}


public static double simpsonsRule(double A, double B, double C, double D, double E, double F, double startInterval, double endInterval, double numInterval) {
	
	double mid = midpointRule(A, B, C, D, E, F, startInterval, endInterval, numInterval);
	double trap = trapezoidRule(A, B, C, D, E, F, startInterval, endInterval, numInterval); 
	
	return ((2.0 * mid) + trap) / 3.0;
	
}

		
public static String percentError(double actual, double estimate) {
	
	double error = (estimate - actual) / actual;
	
	if(error == 0)
		return "none";
	
	return String.format("%.9f %%", error);

}


public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	DecimalFormat formatter = new DecimalFormat("#,###.00");
	
	try {
		System.out.println("Please enter the \"a\" bound of integration: ");
			double a = input.nextDouble();
			
		
		System.out.println("Please enter the \"b\" bound of integration: ");
			double b = input.nextDouble();
		
			if(b < a) {
				System.out.println("Error: \"a\" must be less than \"b\"");	
				System.exit(0);
			}
		
		System.out.println("Please enter the number of sub-intervals: ");
			int n = input.nextInt();
		
			if(n <= 0) {
				System.out.println("The amount of sub-intervals must be greater than or equal to 0.");
				System.exit(0);
			}
		
		System.out.println("Please enter 6 values: ");
			double A = input.nextDouble();
			double B = input.nextDouble();
			double C = input.nextDouble();
			double D = input.nextDouble();
			double E = input.nextDouble();
			double F = input.nextDouble();
			input.close();
		
		System.out.printf("%n%n%nFunction being integrated: %.2fx^5 + %.2fx^4 + %.2fx^3 + %.2fx^2 + %.2fx + %.2f%n%n", A, B, C, D, E, F);
			
			Double actual = antiDerivative(A, B, C, D, E, F, a, b);
			double midpoint = midpointRule(A, B, C, D, E, F, a, b, n);
			double trapezoid = trapezoidRule(A, B, C, D, E, F, a, b, n);
			double simpsons = simpsonsRule(A, B, C, D, E, F, a, b, n);
			
		
		System.out.printf("-> The exact value (found by using the Fundamental Theorem of Calculus): %s   // error: %s%n%n", 
																							formatter.format(actual), percentError(actual, actual));
		
		System.out.printf("-> The estimated value (found by using the Midpoint Reimann Sum method): %s   // error: %s%n%n", 
																							formatter.format(midpoint), percentError(actual, midpoint));
		
		System.out.printf("-> The estimated value (found by using the Trapezoidal Reimann Sum method): %s   // error: %s%n%n", 
																							formatter.format(trapezoid), percentError(actual, trapezoid));
		
		System.out.printf("-> The estimated value (found by using the Simpsons Reimann Sum method): %s   // error: %s%n%n", 
																							formatter.format(simpsons), percentError(actual, simpsons));
	}

	catch(InputMismatchException x) {
		
		System.out.println("Error: Invalid input type");
		System.exit(0);
			
	}
	
	catch(NoSuchElementException y) {
		
		System.out.println("Error: No input");
		System.exit(0);
		
	}
	
}
}