-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
76 lines (66 loc) · 1.91 KB
/
Copy pathMain.java
File metadata and controls
76 lines (66 loc) · 1.91 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
import java.util.function.Function;
public class Main
{
public static double [] SvenMethod(Function<Double, Double> f, double x, double step){
double x1= x-step;
double x2=x+step;
double f0= f.apply(x);
double f1= f.apply(x1);
double f2= f.apply(x2);
if ((f1>f0)&&(f2>f1)){
System.out.println(x1);
System.out.println(x2);
return new double[]{x1, x2};
}
else if(f1<f0 && f2<f0) {
System.out.println("функція не є унімодальною");
return null;
}
if (f1<=f2){
step=-step;
}
else {
x1=x2;
f1=f2;
}
for (int i = 1; true ; i++) {
System.out.print("iteration:"+i+" ");
step=step*2;
x2=x1+step;
f2=f.apply(x2);
if (f2<=f1)
{
x=x1;
x1=x2;
f1=f2;
System.out.print(x+" ");
System.out.println(x1);
}
else
{
double x3=x2-step/2;
double f3=f.apply(x3);
if(f1>f3){
// System.out.println("step"+step);
System.out.print(x1+" ");
System.out.println(x2);
return new double[]{x1, x2};
}
else{
// System.out.println("step"+step);
System.out.print(x1+" ");
System.out.println(x2);
return new double[]{x, x3};
}
}
}
}
public static void main(String[] args)
{
Function<Double, Double> f = x -> Math.pow((-5-x)/3-1.4,2);
double x= 5;
double step=0.1;
System.out.println("Початковий крок:"+step);
SvenMethod(f,x,step);
}
}