-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStripProof.java
More file actions
83 lines (68 loc) · 2.45 KB
/
StripProof.java
File metadata and controls
83 lines (68 loc) · 2.45 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package statsmodel;
/**
*
* @author Milu
*/
public class StripProof {
static double n;
public static void main(String args[]) {
n = findN();
System.out.println(trapeziumRule(0, 4, 0, 1, n));//limit a,limit b, mean, sd
System.out.println(n);
}
public static double trapeziumRule(double a, double b, double mean, double sd, double n) {
//double n = n; //number of strips
double h = (b - a) / n; //height aka width of trapezium
double sum = 0; //sum of the middle strips
//first strip
double yfirst = p_of_x(sd, mean, a);
double[] xIntermediates = new double[(int) n];
xIntermediates[0] = a;
double[] yIntermediates = new double[(int) n];
yIntermediates[0] = yfirst;
//middle strips
double temp = a;
//last strip
for (int i = 1; i < n; i++) {
temp = temp + h;
xIntermediates[i] = temp;
yIntermediates[i] = p_of_x(sd, mean, xIntermediates[i]);
sum += yIntermediates[i];
}
double yLast = p_of_x(sd, mean, xIntermediates[(int) n - 1] + h);
double area = h / 2 * (yfirst + 2 * (sum) + yLast);
area = Math.round((area) * 100000d) / 100000d;
return area;
}
public static double p_of_x(double sd, double mean, double x) {
//P(x), Probability fuction
double y;
double denominator, numerator, exponent;
numerator = Math.E;
denominator = sd * Math.sqrt(2 * Math.PI);
exponent = Math.pow(x - mean, 2) / 2 * (Math.pow(sd, 2));
y = Math.pow(numerator, -exponent) / denominator; //P(x)
return y; //return y subscript n
}
/*
TEST WHEN 5dp PRECISON is found to true integral values
true: 0.49996833 @ n = 200, 7dp matches
*/
public static double findN() {//
double trueValue = 0.49997;//0.49996833
//double trueValue = 0.38209;
double a;
n = 0;
do {
n++;
a= trapeziumRule(0, 4, 0, 1, n);
// a = trapeziumRule(1.5, 20, 0, 5, n);
} while (a != trueValue);
return n;
}
}