метод стрельбы

 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
public class Method {
Double c1;
Double a;
Double b;
Double A;
Double B;
Integer n;
Double[] pointsX;
Double[] pointsY0;
Double[] pointsY1;
Double[] pointsY;
Coefficients coeff;
public Method(double a, double b, int n, double A, double B) {
coeff = new Coefficients();
this.a = a;
this.b = b;
this.A = A;
this.B = B;
this.n = n;
pointsX = new Double[n + 1];
pointsY = new Double[n + 1];
pointsY0 = new Double[n + 1];
pointsY1 = new Double[n + 1];
}
public void shoot() {
double h = (b - a) / n;
for (int i = 0; i < n + 1; i++) {
pointsX[i] = i * h;
}
pointsY0[0] = A;
pointsY0[1] = A + h; // ?
pointsY1[0] = 0.0;
pointsY1[1] = h; // ?
for (int i = 1; i < n; i++) {
pointsY0[i + 1] = (h * h * coeff.F(pointsX[i]) -
(1 - (h / 2) * coeff.P(pointsX[i])) * pointsY0[i - 1] -
(h * h * coeff.Q(pointsX[i]) - 2) * pointsY0[i]) /
(1 + (h / 2) * coeff.P(pointsX[i]));
pointsY1[i + 1] = (-(1 - (h / 2) * coeff.P(pointsX[i])) * pointsY1[i - 1] -
(Math.pow(h, 2) * coeff.Q(pointsX[i]) - 2) * pointsY1[i]) /
(1 + (h / 2) * coeff.P(pointsX[i]));
}
// если pointsY1[n] == 0 => try catch
c1 = (B - pointsY0[n]) / pointsY1[n];
for (int i = 0; i < n + 1; i++) {
pointsY[i] = pointsY0[i] + c1 * pointsY1[i];
}
}
public void print() {
System.out.println("[" + a + ", " + b + "]");
System.out.println("y(" + a + ") = " + A);
System.out.println("y(" + b + ") = " + B);
System.out.println("Function y(x):");
System.out.println("x\ty");
for (int i = 0; i < n + 1; i++) {
System.out.println(pointsX[i] + "\t" + pointsY[i]);
}
}
}
class Coefficients implements Coeff {
@Override
public Double P(Double x) {
return x;
}
@Override
public Double Q(Double x) {
return 1.0;
}
@Override
public Double F(Double x) {
return (x + 2) * Math.exp(x);
}
}