methods - lab6 - all

 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
public class Marina_lab6 {
static int size = 9;
public static double f(double x, double a, double b) {
return 1 / (a*x + b);
}
public static double SKU(double a, double b, double[] x, double[] y) {
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += (a * x[i] + b - y[i]) * (a * x[i] + b - y[i]);
}
return Math.sqrt(sum);
}
public static double[] reverseY(double[] y) {
for (int i = 0; i < size; i++) {
y[i] = 1 / y[i];
}
return y;
}
public static void main(String args[]) {
double x[] = {1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0};
double y[] = {2.07, 2.41, 1.77, 1.98, 2.24, 2.73, 2.91, 2.56, 3.07};
double a = 0, b = 0, c = 0, d = size, e = 0, f = 0;
double A, B;
y = reverseY(y);
for(int i = 0; i < size; i++) {
a += x[i] * x[i];
b += x[i];
f += x[i] * y[i];
e += y[i];
}
c = b;
B = (a*e - c*f) / (a*d - b*c);
A = (f - B*b) / a;
System.out.println("f(x) = 1 / (" + A + "*x + " + B + ")");
System.out.println("SKU = " + SKU(A, B, x, y));
System.out.println("SKU for appr function f(x) = 0.5x + 1: SKU = " + SKU(0.5, 1, x, y));
}
}