3 * Implements a simple linear least squares best fit routine.
6 // Written by Curtis Olson, started September 1997.
8 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Library General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 # error This library requires C++
37 Classical least squares fit:
44 b_1 = \frac{n * \sum_0^{i-1} (x_i*y_i) - \sum_0^{i-1} x_i* \sum_0^{i-1} y_i}
45 {n*\sum_0^{i-1} x_i^2 - (\sum_0^{i-1} x_i)^2}
49 b_0 = \frac{\sum_0^{i-1} y_i}{n} - b_1 * \frac{\sum_0^{i-1} x_i}{n}
52 void least_squares(double *x, double *y, int n, double *m, double *b);
56 * Incrimentally update existing values with a new data point.
58 void least_squares_update(double x, double y, double *m, double *b);
62 @return the least squares error:.
65 \frac{(y_i - \hat{y}_i)^2}{n}
68 double least_squares_error(double *x, double *y, int n, double m, double b);
72 @return the maximum least squares error.
78 double least_squares_max_error(double *x, double *y, int n, double m, double b);