private static String fibonacci double double double ArrayList Integer

 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
private static String fibonacci(double a, double b, double e) {
ArrayList<Integer> F = new ArrayList<>();
F.add(1);
F.add(1);
for (int i = 2; F.get(i - 1) < (b - a) / e; i++) {
F.add(F.get(i - 1) + F.get(i - 2));
}
int N = F.size() - 1;
double x1 = a + ((b - a) * F.get(N - 2) / F.get(N));
double x2 = a + ((b - a) * F.get(N - 1) / F.get(N));
for (int i = 1; i < N - 3; i++) {
if (func(x1) > func(x2)) {
a = x1;
x1 = x2;
x2 = a + (F.get(N - i - 2) * (b - a) / F.get(N - i - 1));
} else {
b = x2;
x2 = x1;
x1 = a + (F.get(N - i - 3) * (b - a) / F.get(N - i - 1));
}
}
x1 = x2;
x2 = x1 + e;
if (func(x1) > func(x2)) {
a = x1;
} else {
b = x2;
}
return "FIBONACCI:\n" + "n = " + (N - 3) + ", min point = " +
((a + b) / 2) + ", f(x) = " + func((a + b) / 2);
}