-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.c
More file actions
49 lines (39 loc) · 1.05 KB
/
LinearRegression.c
File metadata and controls
49 lines (39 loc) · 1.05 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *fp;
fp = fopen("data_linear.txt", "r");
double matrix[100][2];
for (int i = 0; i < 2631; i++)
{
fscanf(fp, "%lf", &matrix[i][0]);
fscanf(fp, "%lf", &matrix[i][1]);
}
double x[] = {1.2, 2.3, 3.8, 4.9, 5.9};
double y[] = {1.245, 4.90, 9.67, 16.67, 25.89};
int n = sizeof(x) / sizeof(x[0]);
int n = 100;
double sumx = 0.0;
double sumy = 0.0;
double sumxy = 0.0;
double sumx2 = 0.0;
for (int i = 0; i < n; i++)
{
sumx += matrix[i][0];
sumy += matrix[i][1];
sumxy += matrix[i][0] * matrix[i][1];
sumx2 += matrix[i][0] * matrix[i][0];
}
double m = ((n * sumxy) - (sumx * sumy)) / ((n * sumx2) - (sumx * sumx));
double c = (sumy - m * sumx) / n;
FILE *fp1 = fopen("linearreg.txt", "w");
for (double i = 1; i <= 100; i += 0.0001)
{
fprintf(fp1, "%0.18lf %0.18lf\n", i, (m * i + c));
}
printf("%0.50lf %0.50lf", m, c);
fclose(fp);
return 0;
}