NM6

 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
package labs;
import utils.Point;
public class Lab6 {
private static Point[] points = {
new Point(1 , 1.32),
new Point(1.5, 1.81),
new Point(2 , 2.58),
new Point(2.5, 2.88),
new Point(3 , 3.88),
new Point(3.5, 4.29),
new Point(4 , 4.58),
new Point(4.5, 5.00),
new Point(5 , 4.14)
};
//y = ax^b+c
private static double SKU(double a, double b, double c) {
double sum = 0;
for(Point p:points) {
sum += Math.pow(a*Math.pow(p.x, b) + c - p.y, 2);
}
return Math.sqrt(sum);
}
public static void start() {
//y_1 = ln(y), x_1 = ln(x)
//A = sum(x_i), B = sum(y_i), C = sum(x_i*y_i)
double A=0, B=0, C=0, D=0;
for(Point p:points.clone()) {
double x = Math.log(p.x);
double y = Math.log(p.y);
A += x;
B += y;
C += x*y;
D += x*x;
}
double a_1 = (points.length*C - A*B)/(points.length*D - A*A);
double b_1 = (B-a_1*A)/points.length;
double b = a_1, a = Math.exp(b_1);
System.out.println("f(x) = " + a + "*x^" + b);
System.out.println("SKU z(x)= " + SKU(1, 1, 0.5));
System.out.println("SKU f(x)= " + SKU(a, b, 0));
}
}