methods - lab2

  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
package methods_lab2;
import java.util.HashMap;
public class Methods_lab2 {
public static Boolean Richardson(OutputValues vstep, OutputValues vstep1, int k, double e)
{
vstep.r = (vstep.intg - vstep1.intg) / k;
return Math.abs(vstep.r) <= e;
}
public static double f(double x)
{
return Math.exp(x);
}
public static OutputValues rectangles(int n, double a, double b) {
OutputValues values = new OutputValues(n);
double h = (b - a) / n;
double x1 = a;
double x2 = a + h;
for (int i = 0; i < n; i++)
{
values.intg += f((x2 + x1) / 2);
x1 = x2;
x2 += h;
}
values.intg *= h;
return values;
}
public static OutputValues trapeze(int n, double a, double b) {
OutputValues values = new OutputValues(n);
double h = (b - a) / n;
for (int i = 1; i < n; i++) {
values.intg += f(a + i * h);
}
values.intg += (f(a) + f(b)) / 2;
values.intg *= h;
return values;
}
public static OutputValues simpson(int n, double a, double b) {
OutputValues values = new OutputValues(n);
double h = (b - a) / n;
values.intg += f(a) + f(b);
for (int i = 1; i <= n - 1; i++) {
if (i % 2 == 0) {
values.intg += 2 * f(a + i * h);
} else {
values.intg += 4 * f(a + i * h);
}
}
values.intg *= h / 3;
return values;
}
public static void methods(int n, double a, double b, double e, int mnum) {
String method = "";
int step1 = n;
int step = 2 * n;
for (;;) {
int k;
OutputValues vstep1 = null;
OutputValues vstep = null;
switch (mnum) {
case 0:
method = "Average rectangles";
vstep1 = rectangles(step1, a, b);
vstep = rectangles(step, a, b);
break;
case 1:
method = "Trapeze";
vstep1 = trapeze(step1, a, b);
vstep = trapeze(step, a, b);
break;
case 2:
method = "Simpson's method";
vstep1 = simpson(step1, a, b);
vstep = simpson(step, a, b);
break;
}
if (mnum < 2) k = 3; else k = 15;
if (Richardson(vstep, vstep1, k, e))
{
System.out.println(method);
System.out.println(vstep.toString());
return;
}
step1 *= 2;
step *= 2;
}
}
public static void monteCarlo(int points, double a, double b) {
System.out.println("Monte Carlo");
//System.out.println("x y");
OutputValues values = new OutputValues(-1, points);
double miny = f(a);
double maxy = f(b);
double s = (b - a) * maxy;// - miny);
int goodpoints = 0;
for (int i = 0; i < points; i++) {
double x = Math.random() * (b - a);
double y = Math.random() * maxy;// - miny);
// System.out.print(x);
//System.out.print('\t');
//System.out.println(y);
double fy = f(x);
if (y <= fy) {
goodpoints++;
}
}
values.intg = s * goodpoints / points;
values.r = Math.E - 1;
//System.out.println("Monte Carlo");
System.out.println(values.toString());
}
public static void main(String[] args) {
for (int i = 0; i < 3; i++)
methods(1, 0, 1, 0.001, i);
monteCarlo(100, 0, 1);
}
}
package methods_lab2;
public class OutputValues {
public Integer n;
public Integer points;
public Double intg;
public Double r;
public OutputValues(int n) {
this.n = n;
intg = 0.0;
r = 0.0;
}
public OutputValues(int n, int points) {
this.n = n;
this.points = points;
intg = 0.0;
r = 0.0;
}
@Override
public String toString() {
if (n != -1) {
System.out.println("n" + "\t" + "intg" + "\t" + "intg + Richardson");
return n.toString() + "\t" + intg.toString() + "\t"
+ (new Double(intg + r)).toString();
} else {
System.out.println("points" + "\t" + "intg" + "\t" + "intg - realintg");
return points.toString() + "\t" + intg.toString() + "\t"
+ (new Double(intg - r)).toString();
}
}
}