comparison armadillo-3.900.4/examples/example_lsq.cpp @ 49:1ec0e2823891

Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author Chris Cannam
date Thu, 13 Jun 2013 10:25:24 +0100
parents
children
comparison
equal deleted inserted replaced
48:69251e11a913 49:1ec0e2823891
1 // Tutorial for linear least square fitting
2 // Author: Pierre Moulon
3 // Date: 8 December 2009
4 // Objective:
5 // Fit a 2D line with to a set of points
6 // Specifically, find a and b in the model y = ax + b
7 //
8 // Direct application of the example in:
9 // http://en.wikipedia.org/wiki/Linear_least_squares#Motivational_example
10
11
12 #include <iostream>
13
14 #include "armadillo"
15
16 using namespace arma;
17 using namespace std;
18
19
20
21 int main(int argc, char** argv)
22 {
23 // points to which we will fit the line
24 mat data = "1 6; 2 5; 3 7; 4 10";
25
26 cout << "Points used for the estimation:" << endl;
27 cout << data << endl;
28
29 // Build matrices to solve Ax = b problem:
30 vec b(data.n_rows);
31 mat C(data.n_rows, 2);
32
33 for(u32 i=0; i<data.n_rows; ++i)
34 {
35 b(i) = data(i,1);
36
37 C(i,0) = 1;
38 C(i,1) = data(i,0);
39 }
40
41 cout << "b:" << endl;
42 cout << b << endl;
43
44 cout << "Constraint matrix:" << endl;
45 cout << C << endl;
46
47 // Compute least-squares solution:
48 vec solution = solve(C,b);
49
50 // solution should be "3.5; 1.4"
51 cout << "solution:" << endl;
52 cout << solution << endl;
53
54
55 cout << "Reprojection error:" << endl;
56
57 for(u32 i=0; i<data.n_rows; ++i)
58 {
59 cout << " residual: " << ( data(i,1) - (solution(0) + solution(1) * data(i,0)) ) << endl;
60 }
61
62 return 0;
63 }
64