Thanks, i will look into this.
@edit:
Im missing something: My output is as follows ( using y = mx + b) : m: 0 b: 2.8
i expect something like this: m : 0.5842 b : 1.6842
my code is : #include <iostream> #include <vector>
#include "lsfit.h" #include "linreg.h"
#include <stdio.h> #include <stdlib.h> #include <time.h>
int main(int argc, char **argv) { std::vector<double> xrange; std::vector<double> yrange; /* y = mx + b:
Data Set x y 1.0 2.6 2.3 2.8 3.1 3.1 4.8 4.7 5.6 5.1 6.3 5.3 m (slope) : 0.5842 b (y - int) : 1.6842 r (corr.coeff) : 0.9741 */ xrange.push_back(1.0); xrange.push_back(2.3); xrange.push_back(3.1); xrange.push_back(4.8); xrange.push_back(5.6); xrange.push_back(6.3);
yrange.push_back(2.6); yrange.push_back(2.8); yrange.push_back(3.1); yrange.push_back(4.7); yrange.push_back(5.1); yrange.push_back(5.3);
if( !xrange.empty() && !yrange.empty()){ int npoints = std::min(xrange.size(), yrange.size());//points ap::real_2d_array xy; xy.setlength(npoints, npoints); // Fill up the matrix. First column is x, second column is y for(int x = 0; x < npoints; x++){ xy(x, 0) = xrange[x];
for(int y = 0; y < npoints; y++) { xy(x, y) = yrange[y]; } }
int nvars = 1; int info = 0; linearmodel lm; lrreport ar; lrbuild(xy, npoints, nvars, info, lm, ar);
ap::real_1d_array v; lrunpack(lm, v, nvars);
if( nvars > 0 ){ std::cout << "m: " << v(0) << " b: " << v(1); } } return 0; }
|