-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpson1_3Integration.java
More file actions
56 lines (50 loc) · 1.84 KB
/
Copy pathSimpson1_3Integration.java
File metadata and controls
56 lines (50 loc) · 1.84 KB
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
package tscore;
/* File :Simpson1_3Integration.java
* Project: Math Tools in Java
* Author : Wachara R.
* First Released: Thu 8 Mar 2007.
* Last Updated : Thu 8 Mar 2007.
*/
//import common.Function;
/** Integration of given function from lower limit
to upper limit using the Simpson 1/3 method.
*/
public class Simpson1_3Integration extends Integrator{
/** Constructor.
* @param function the function to integration.
* @param lower the lower limit of integration.
* @param upper the upper limit of integration.
* @param intervals the number of equal-width intervals
*/
public Simpson1_3Integration (Function function, double lower, double upper, int intervals){
super(function, lower, upper,intervals);
try {
IntegrationResult = integrate();
} catch (IntegrationException msg) {
System.out.println( "Error : "+ msg);
}
}
double integrate() throws IntegrationException {
if(intervals <=0) throw new IntegrationException(IntegrationException.INVALID_INTERVALS);
double h = (upper - lower)/(2*intervals); // half of interval width
double area = 0;
for(int i = 0; i < intervals; i++){
double x1 = lower + 2*i*h;
area += findAreaUnderCurve(x1,h);
}
return area;
}
/** Evaluate the area under parabolic curve
* @param x1 the left bound of the region
* @param h the interval width
*/
double findAreaUnderCurve(double x1, double h){
// area of the parabolic region = h/3(f(-h) +4f(0) +f(h))
double x2 = x1 + h; // middle point
double x3 = x2 + h; // rightmost point
double y1 = function.Of(x1);
double y2 = function.Of(x2);
double y3 = function.Of(x3);
return (h*(y1 + 4*y2 +y3)/3); // area under parabolic curve element
}
}