public class Main
{
public static void main
(String[] args
) { int numOfData = 5000;
int[] x = new int[numOfData];
int[] y = new int[numOfData];
for (int i=0; i<numOfData; i++) {
x[i] = 2*(i+1) + 1;
y[i] = (x[i]*x[i] - 5) / 4;
}
double[] ln_x = new double[numOfData];
double[] ln_y = new double[numOfData];
for (int i=0; i<numOfData; i++) {
ln_x
[i
] = Math.
log(x
[i
]); ln_y
[i
] = Math.
log(y
[i
]); }
double sum_ln_x = 0.0, sum_ln_y = 0.0;
for (int i=0; i<numOfData; i++) {
sum_ln_x += ln_x[i];
sum_ln_y += ln_y[i];
}
double mean_ln_x = sum_ln_x / numOfData, mean_ln_y = sum_ln_y / numOfData;
double[] ln_x_minus_mean = new double[numOfData];
double[] ln_y_minus_mean = new double[numOfData];
for (int i=0; i<numOfData; i++) {
ln_x_minus_mean[i] = ln_x[i] - mean_ln_x;
ln_y_minus_mean[i] = ln_y[i] - mean_ln_y;
}
double sum_sqr_ln_x_minus_mean = 0.0, sum_sqr_ln_y_minus_mean = 0.0, sum_products_x_y = 0.0;
for (int i=0; i<numOfData; i++) {
sum_sqr_ln_x_minus_mean
+= Math.
pow(ln_x_minus_mean
[i
],
2.0); sum_sqr_ln_y_minus_mean
+= Math.
pow(ln_y_minus_mean
[i
],
2.0); sum_products_x_y += ln_x_minus_mean[i] * ln_y_minus_mean[i];
}
double a, b, r, r2;
b = sum_products_x_y / sum_sqr_ln_x_minus_mean;
a
= Math.
exp(mean_ln_y
- b
*mean_ln_x
); r
= sum_products_x_y
/ (Math.
sqrt(sum_sqr_ln_x_minus_mean
)*Math.
sqrt(sum_sqr_ln_y_minus_mean
)); System.
out.
format("a=%f b=%f r=%f r2=%f", a, b, r, r2
); }
}
cHVibGljIGNsYXNzIE1haW4KewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewoJCWludCBudW1PZkRhdGEgPSA1MDAwOwoJCWludFtdIHggPSBuZXcgaW50W251bU9mRGF0YV07CgkJaW50W10geSA9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