dev_parameter_estimation_tutorial.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit 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  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 
36 #include <acado_gnuplot.hpp>
37 
38 
39 int main( ){
40 
42 
43 
44  // INTRODUCE THE VARIABLES:
45  // -------------------------
46 
47  DifferentialState phi; // the angle phi
48  DifferentialState omega; // the first derivative of phi w.r.t. time
49 
50  Parameter l; // the length of the pendulum
51  Parameter alpha; // frictional constant
52  Parameter g; // the gravitational constant
53 
54 
55  // DEFINE A DIFFERENTIAL EQUATION:
56  // -------------------------------
57 
59 
60  f << dot(phi ) == omega;
61  f << dot(omega) == -(g/l)*sin(phi) - alpha*omega;
62 
63  // REMARK: Note that the parameters g, and l are not independent.
64  // Only one of these parameters can be estimated from
65  // the measurements of the dynamic motion.
66  // -----------------------------------------------------------------
67 
68 
69  // DEFINE A MEASUREMENT FUNCTION:
70  // ------------------------------
71 
72  Function h;
73  h << phi; // The state phi is being measured.
74 
75 
76  // DEFINE THE INVERSE OF THE VARIANCE-COVARIANCE MATRIX OF THE MEASUREMENTS:
77  // -------------------------------------------------------------------------
78  DMatrix S(1,1);
79  S(0,0) = 1.0/pow(0.1,2); // (1 over the variance of the measurement) HERE: the standard deviation of the measurement is assumed to be 0.1, thus S = 1/(0.1)^2.
80 
81 
82  // READ THE MEASUREMENT FROM A DATA FILE:
83  // --------------------------------------
84 
85  VariablesGrid measurements;
86  measurements = readFromFile( "parameter_estimation_data.txt" );
87 
88  if( measurements.isEmpty() == BT_TRUE )
89  printf("The file \"parameter_estimation_data.txt\" can't be opened.");
90 
91 
92  // DEFINE A PARAMETER ESTIMATION PROBLEM:
93  // --------------------------------------
94  OCP ocp( measurements.getTimePoints() );
95 
96  ocp.minimizeLSQ( h, measurements );
97 
98  ocp.subjectTo( f );
99 
100  ocp.subjectTo( 0.0 <= alpha <= 4.0 );
101  ocp.subjectTo( 0.0 <= l <= 2.0 );
102 
103  ocp.subjectTo( g == 9.81 );
104 
105 
106  // SETUP AN PLOT WINDOW:
107  // ---------------------------------------------------
108  GnuplotWindow window( PLOT_NEVER );
109 
110  window.addSubplot( phi, "The angle phi", "time [s]", "angle [rad]" );
111  window.addSubplot( omega, "The angular velocity omega" );
112  window.addSubplot( l, "The length of the pendulum l" );
113  window.addSubplot( alpha, "Frictional constant alpha" );
114 
115 
116  // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
117  // ---------------------------------------------------
118  ParameterEstimationAlgorithm algorithm(ocp);
119 
120  algorithm << window;
121 // algorithm.initializeDifferentialStates( "parameter_estimation_data2.txt" );
122 
123  algorithm.solve();
124 
125 
126  // GET THE OPTIMAL PARAMETERS:
127  // -----------------------------------
128  VariablesGrid parameters;
129  algorithm.getParameters( parameters );
130 
131 
132 // return 0;
133 
134  // GET THE VARIANCE COVARIANCE IN THE SOLUTION:
135  // ---------------------------------------------
136  DMatrix var;
137  algorithm.getParameterVarianceCovariance( var );
138 
139 // return 0;
140 
141  // PRINT THE RESULT ON THE TERMINAL:
142  // -----------------------------------------------------------------------
143 
144  printf("\n\nResults for the parameters: \n");
145  printf("-----------------------------------------------\n");
146  printf(" l = %.3e +/- %.3e \n", parameters(0,0), sqrt( var(0,0) ) );
147  printf(" alpha = %.3e +/- %.3e \n", parameters(0,1), sqrt( var(1,1) ) );
148  printf(" g = %.3e +/- %.3e \n", parameters(0,2), sqrt( var(2,2) ) );
149  printf("-----------------------------------------------\n\n\n");
150 
151 
152  // PLOT THE RESULT:
153  // ---------------------------------------------------
154  algorithm.getPlotWindow( window );
155 
156  window.addData( 0, measurements(0) );
157  window.addLine( 2, parameters(0,0) + sqrt( var(0,0) ) );
158  window.addLine( 2, parameters(0,0) - sqrt( var(0,0) ) );
159  window.addLine( 3, parameters(0,1) + sqrt( var(1,1) ) );
160  window.addLine( 3, parameters(0,1) - sqrt( var(1,1) ) );
161 
162  window.plot( );
163 
164  return 0;
165 }
USING_NAMESPACE_ACADO IntermediateState sin(const Expression &arg)
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
IntermediateState sqrt(const Expression &arg)
returnValue getPlotWindow(uint idx, PlotWindow &_window) const
#define USING_NAMESPACE_ACADO
Provides a time grid consisting of vector-valued optimization variables at each grid point...
IntermediateState pow(const Expression &arg1, const Expression &arg2)
BooleanType isEmpty() const
returnValue getParameters(VariablesGrid &u_) const
returnValue getParameterVarianceCovariance(DMatrix &pVar)
returnValue addSubplot(PlotWindowSubplot &_subplot)
returnValue readFromFile(real_t *data, int nrow, int ncol, const char *datafilename)
User-interface to formulate and solve parameter estimation problems.
returnValue minimizeLSQ(const DMatrix &S, const Function &h, const DVector &r)
Definition: ocp.cpp:244
Data class for defining optimal control problems.
Definition: ocp.hpp:89
Expression dot(const Expression &arg)
#define BT_TRUE
Definition: acado_types.hpp:47
Provides an interface to Gnuplot for plotting algorithmic outputs.
virtual returnValue solve()
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:32