parameter_estimation_tutorial2.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 dphi; // 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  Control F; // force acting on the pendulum
55  // (control input)
56 
57 
58  double const m = 1.0;
59 
60 
61  // DEFINE A DIFFERENTIAL EQUATION:
62  // -------------------------------
63 
65 
66  f << dot(phi ) == dphi;
67  f << dot(dphi) == -(g/l)*sin(phi) - alpha*dphi + F/m;
68 
69 
70  // REMARK: Note that the parameters g, and l are not independent.
71  // Only one of these parameters can be estimated from
72  // the measurements of the dynamic motion.
73  // -----------------------------------------------------------------
74 
75 
76  // DEFINE A MEASUREMENT FUNCTION:
77  // ------------------------------
78 
79  Function h;
80  h << phi; // The state phi is being measured.
81 
82 
83  // DEFINE THE INVERSE OF THE VARIANCE-COVARIANCE MATRIX OF THE MEASUREMENTS:
84  // -------------------------------------------------------------------------
85  DMatrix S(1,1);
86  S(0,0) = 1.0/pow(0.1,2); // (1 over the variance of the measurement)
87  // HERE: the standard deviation of the measurement is
88  // assumed to be 0.1, thus S = 1/(0.1)^2.
89 
90 
91  // READ THE MEASUREMENT FROM A DATA FILE:
92  // --------------------------------------
93 
94  VariablesGrid measurements;
95  measurements.read( "parameter_estimation_data2.txt" );
96 
97  if( measurements.isEmpty() == BT_TRUE )
98  printf("The file \"parameter_estimation_data2.txt\" can't be opened.");
99 
100 
101 
102  // READ THE CONTROL INPUT FROM A DATA FILE:
103  // ----------------------------------------
104 
105  VariablesGrid F_reference;
106  F_reference.read( "parameter_estimation_controls.txt" );
107 
108  if( F_reference.isEmpty() == BT_TRUE )
109  printf("The file \"parameter_estimation_controls.txt\" can't be opened.");
110 
111 
112 
113  // DEFINE A PARAMETER ESTIMATION PROBLEM:
114  // --------------------------------------
115  OCP ocp( measurements.getTimePoints() );
116 
117  ocp.minimizeLSQ( S, h, measurements );
118  ocp.subjectTo( f );
119 
120  ocp.subjectTo( 0.0 <= alpha <= 4.0 );
121  ocp.subjectTo( 0.0 <= l <= 2.0 );
122 
123  ocp.subjectTo( F == F_reference(0) );
124  ocp.subjectTo( g == 9.81 );
125 
126 
127  // SETUP AN PLOT WINDOW:
128  // ---------------------------------------------------
129  GnuplotWindow window( PLOT_NEVER );
130 
131  window.addSubplot( phi, "The angle phi" );
132  window.addSubplot( dphi, "The angular velocity dphi " );
133  window.addSubplot( l, "The length of the pendulum l" );
134  window.addSubplot( alpha,"Frictional constant alpha " );
135  window.addSubplot( F, "Control input (force) F" );
136 
137 
138  // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
139  // ---------------------------------------------------
140  ParameterEstimationAlgorithm algorithm(ocp);
141 
142  algorithm << window;
143  algorithm.initializeDifferentialStates( "parameter_estimation_data2.txt" );
144  algorithm.set(LEVENBERG_MARQUARDT, 1e-5);
145 
146  algorithm.solve();
147 
148 
149  // GET THE OPTIMAL PARAMETERS:
150  // -----------------------------------
151  VariablesGrid parameters;
152  algorithm.getParameters( parameters );
153 
154 
155  printf("\n\nResults for the parameters: \n");
156  printf("-----------------------------------------------\n");
157  printf(" l = %.3e \n", parameters(0,0) );
158  printf(" alpha = %.3e \n", parameters(0,1) );
159  printf(" g = %.3e \n", parameters(0,2) );
160  printf("-----------------------------------------------\n\n\n");
161 
162 
163  // PLOT THE RESULT:
164  // ---------------------------------------------------
165  algorithm.getPlotWindow( window );
166 
167  window.addData( 0, measurements(0) );
168 
169  window.plot( );
170 
171  return 0;
172 }
173 
174 
175 
USING_NAMESPACE_ACADO IntermediateState sin(const Expression &arg)
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
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 addSubplot(PlotWindowSubplot &_subplot)
returnValue set(OptionsName name, int value)
Definition: options.cpp:126
User-interface to formulate and solve parameter estimation problems.
returnValue minimizeLSQ(const DMatrix &S, const Function &h, const DVector &r)
Definition: ocp.cpp:244
returnValue initializeDifferentialStates(const char *fileName, BooleanType autoinit=BT_FALSE)
Data class for defining optimal control problems.
Definition: ocp.hpp:89
Expression dot(const Expression &arg)
returnValue read(std::istream &stream)
#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:55