Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /**
11  * Execute a least squares approximation of the given x,y data.
12  */
13 
14 #include <mrpt/math/types_math.h> // CVectorDouble
15 #include <mrpt/gui.h>
16 #include <mrpt/math/interp_fit.hpp>
17 #include <mrpt/math/utils.h> // normalize()
18 #include <iostream>
19 
20 using namespace mrpt::math;
21 using namespace mrpt::gui;
22 using namespace std;
23 
24 // ------------------------------------------------------
25 // TestLeastSquares
26 // ------------------------------------------------------
27 void TestLeastSquares()
28 {
29  CVectorDouble x, y;
30  normalize(x, y);
31 
32  /**
33  * for convenience, first initialize the data as C arrays and then define
34  * std;:vectors from
35  * that data
36  *
37  * loadVector
38  * Assignment operator for initializing a std::vector from a C array
39  * refer to
40  * http://reference.mrpt.org/stable/group__container__ops__grp.html#ga40e8e47dea9f504a28d2a70ea8ddb158
41  */
42  const double X[] = {1, 2, 3, 4};
43  const double Y[] = {6, 5, 7, 10};
44  loadVector(x, X);
45  loadVector(y, Y);
46 
47  // x points for plotting the least squres line against.
48  CVectorDouble Ts;
49  linspace(-3.0, 8.0, 100, Ts);
50  CVectorDouble Is;
51 
52  /**
53  * Least squares line approximation, based on the provided x,y vectors.
54  */
56 
57  CDisplayWindowPlots win("Result of linear least squares");
58 
59  win.plot(Ts, Is);
60  win.axis_fit();
61  win.axis_equal();
62 
63  win.plot(x, y, ".3r", "training_points");
64 
65  win.waitForKey();
66 }
67 
68 int main(int argc, char** argv)
69 {
70  try
71  {
73 
74  return 0;
75  }
76  catch (std::exception& e)
77  {
78  std::cout << "MRPT exception caught: " << e.what() << std::endl;
79  return -1;
80  }
81  catch (...)
82  {
83  printf("Another exception!!");
84  return -1;
85  }
86 }
mrpt::math::dynamic_vector
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:44
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:27
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:121
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:82
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
utils.h
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:31
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
gui.h
mrpt::math::normalize
void normalize(const VEC1 &v, VEC2 &out_v)
Normalize a vector, such as its norm is the unity.
Definition: math/include/mrpt/math/utils.h:118
y
GLenum GLint GLint y
Definition: glext.h:3538
types_math.h
x
GLenum GLint x
Definition: glext.h:3538



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST