-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |