NM5(0.00001)

 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
package labs;
import utils.Point;
public class Lab5 {
//variant 8
//x^4 + y^4 + sin(x/10 + y/5)
//x0 = (0,0)
private static double e;
private static double f(Point p) { return 4*Math.pow(p.x, 4) + 4*Math.pow(p.y, 4) + Math.sin(p.x/10 + p.y/5); }
private static double fx(Point p) { return 16*Math.pow(p.x, 3) + Math.cos(p.x/10 + p.y/5)/10; }
private static double fy(Point p) { return 16*Math.pow(p.y, 3) + Math.cos(p.x/10 + p.y/5)/5; }
private static double grad(Point p, double lambda) { return f(new Point(p.x - lambda*fx(p), p.y - lambda*fy(p))); }
private static double half(Point p) {
double min = 0,
max = 10,
center = (max + min)/2;
for (;max - min > e; center = (max + min)/2) {
if(grad(p, center - e/2) <= grad(p, center + e/2)) {
max = center;
} else {
min = center;
}
}
return center;
}
private static Point descent(Point p) {
System.out.print("\n" + p.toString() + "\t(Result = " + f(p) + ")");
double lambda = half(p);
Point pNew = new Point(p.x - lambda*fx(p), p.y - lambda*fy(p));
if (Math.sqrt(
Math.pow(p.x-pNew.x, 2) +
Math.pow(p.y-pNew.y, 2)
) < e) {
return pNew;
} else {
return descent(pNew);
}
}
public static void start(double epsilon) {
e = epsilon;
System.out.print("Method of steepest descent:");
descent(new Point(0, 0));
System.out.println("\t<---Min");
}
}