forked from yourtion/LearningMasteringAlgorithms-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsqe.c
44 lines (30 loc) · 741 Bytes
/
lsqe.c
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
//
// lsqe.c
// Algorithms
//
// Created by YourtionGuo on 16/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <math.h>
#include "nummeths.h"
#pragma mark - Public
void lsqe(const double *x, const double *y, int n, double *b1, double *b0)
{
double sumx, sumy, sumx2, sumxy;
int i;
/// 计算合计结果
sumx = 0.0;
sumy = 0.0;
sumx2 = 0.0;
sumxy = 0.0;
for (i = 0; i < n; i++) {
sumx = sumx + x[i];
sumy = sumy + y[i];
sumx2 = sumx2 + pow(x[i], 2.0);
sumxy = sumxy + (x[i] * y[i]);
}
/// 计算最小二乘估计
*b1 = (sumxy - ((sumx * sumy)/(double)n)) / (sumx2-(pow(sumx,2.0)/(double)n));
*b0 = (sumy - ((*b1) * sumx)) / (double)n;
return;
}