MRPT  2.0.3
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /**
11  * Execute a least squares approximation of the given x,y data.
12  */
13 
14 #include <mrpt/gui.h>
15 #include <mrpt/math/utils.h> // normalize()
16 #include <iostream>
17 #include <mrpt/math/interp_fit.hpp>
18 
19 using namespace mrpt::math;
20 using namespace mrpt::gui;
21 using namespace std;
22 
23 // ------------------------------------------------------
24 // TestLeastSquares
25 // ------------------------------------------------------
26 void TestLeastSquares()
27 {
28  CVectorDouble x, y;
29  normalize(x, y);
30 
31  /**
32  * for convenience, first initialize the data as C arrays and then define
33  * std;:vectors from
34  * that data
35  *
36  * loadVector
37  * Assignment operator for initializing a std::vector from a C array
38  * refer to
39  * https://reference.mrpt.org/stable/group__container__ops__grp.html#ga40e8e47dea9f504a28d2a70ea8ddb158
40  */
41  const double X[] = {1, 2, 3, 4};
42  const double Y[] = {6, 5, 7, 10};
43  loadVector(x, X);
44  loadVector(y, Y);
45 
46  // x points for plotting the least squres line against.
47  CVectorDouble Ts;
48  linspace(-3.0, 8.0, 100, Ts);
49  CVectorDouble Is;
50 
51  /**
52  * Least squares line approximation, based on the provided x,y vectors.
53  */
55 
56  CDisplayWindowPlots win("Result of linear least squares");
57 
58  win.plot(Ts, Is);
59  win.axis_fit();
60  win.axis_equal();
61 
62  win.plot(x, y, ".3r", "training_points");
63 
64  win.waitForKey();
65 }
66 
67 int main(int argc, char** argv)
68 {
69  try
70  {
72 
73  return 0;
74  }
75  catch (const std::exception& e)
76  {
77  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
78  return -1;
79  }
80  catch (...)
81  {
82  printf("Another exception!!");
83  return -1;
84  }
85 }
interp_fit.hpp
mrpt::math::loadVector
bool loadVector(std::istream &f, std::vector< int > &d)
Loads one row of a text file as a numerical std::vector.
TestLeastSquares
void TestLeastSquares()
Definition: vision_stereo_rectify/test.cpp:26
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::math::leastSquareLinearFit
NUMTYPE leastSquareLinearFit(const NUMTYPE t, const VECTORLIKE &x, const VECTORLIKE &y, bool wrap2pi=false)
Interpolates or extrapolates using a least-square linear fit of the set of values "x" and "y",...
Definition: interp_fit.hpp:119
mrpt::math::linspace
void linspace(T first, T last, size_t count, VECTOR &out_vector)
Generates an equidistant sequence of numbers given the first one, the last one and the desired number...
Definition: math/include/mrpt/math/utils.h:84
argv
const char * argv[]
Definition: RawlogGrabberApp_unittest.cpp:40
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
utils.h
mrpt::math::CVectorDynamic< double >
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:33
argc
const int argc
Definition: RawlogGrabberApp_unittest.cpp:41
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:11
gui.h
mrpt::exception_to_str
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
mrpt::math::normalize
void normalize(CONTAINER &c, Scalar valMin, Scalar valMax)
Scales all elements such as the minimum & maximum values are shifted to the given values.
Definition: ops_containers.h:279



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Thu May 21 21:53:32 UTC 2020