dev_parameter_estimation_tutorial.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00027 
00035 #include <acado_optimal_control.hpp>
00036 #include <acado_gnuplot.hpp>
00037 
00038 
00039 int main( ){
00040 
00041     USING_NAMESPACE_ACADO
00042 
00043 
00044     // INTRODUCE THE VARIABLES:
00045     // -------------------------
00046 
00047     DifferentialState      phi;    // the angle phi
00048     DifferentialState    omega;    // the first derivative of phi w.r.t. time
00049 
00050     Parameter                l;    // the length of the pendulum
00051     Parameter            alpha;    // frictional constant
00052     Parameter                g;    // the gravitational constant
00053 
00054 
00055     // DEFINE A DIFFERENTIAL EQUATION:
00056     // -------------------------------
00057 
00058     DifferentialEquation f;
00059 
00060     f << dot(phi ) == omega;
00061     f << dot(omega) == -(g/l)*sin(phi) - alpha*omega;
00062 
00063     // REMARK: Note that the parameters g, and l are not independent.
00064     //         Only one of these parameters can be estimated from
00065     //         the measurements of the dynamic motion.
00066     // -----------------------------------------------------------------
00067 
00068 
00069     // DEFINE A MEASUREMENT FUNCTION:
00070     // ------------------------------
00071 
00072     Function h;
00073     h <<   phi;  // The state phi is being measured.
00074 
00075 
00076    // DEFINE THE INVERSE OF THE VARIANCE-COVARIANCE MATRIX OF THE MEASUREMENTS:
00077    // -------------------------------------------------------------------------
00078     DMatrix S(1,1);
00079     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.
00080 
00081 
00082     // READ THE MEASUREMENT FROM A DATA FILE:
00083     // --------------------------------------
00084 
00085     VariablesGrid measurements;
00086     measurements = readFromFile( "parameter_estimation_data.txt" );
00087 
00088     if( measurements.isEmpty() == BT_TRUE )
00089         printf("The file \"parameter_estimation_data.txt\" can't be opened.");
00090 
00091 
00092     // DEFINE A PARAMETER ESTIMATION PROBLEM:
00093     // --------------------------------------
00094     OCP ocp( measurements.getTimePoints() );
00095 
00096     ocp.minimizeLSQ( h, measurements );
00097 
00098     ocp.subjectTo( f );
00099 
00100     ocp.subjectTo( 0.0 <= alpha <= 4.0  );
00101     ocp.subjectTo( 0.0 <=   l   <= 2.0  );
00102 
00103     ocp.subjectTo( g == 9.81 );
00104 
00105 
00106     // SETUP AN PLOT WINDOW:
00107     // ---------------------------------------------------
00108         GnuplotWindow window( PLOT_NEVER );
00109 
00110         window.addSubplot( phi,   "The angle phi", "time [s]", "angle [rad]" );
00111         window.addSubplot( omega, "The angular velocity omega" );
00112         window.addSubplot( l,     "The length of the pendulum l" );
00113         window.addSubplot( alpha, "Frictional constant alpha" );
00114 
00115 
00116     // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
00117     // ---------------------------------------------------
00118     ParameterEstimationAlgorithm algorithm(ocp);
00119 
00120         algorithm << window;
00121 //     algorithm.initializeDifferentialStates( "parameter_estimation_data2.txt" );
00122 
00123     algorithm.solve();
00124 
00125 
00126     // GET THE OPTIMAL PARAMETERS:
00127     // -----------------------------------
00128     VariablesGrid parameters;
00129     algorithm.getParameters( parameters );
00130 
00131 
00132 //      return 0;
00133 
00134         // GET THE VARIANCE COVARIANCE IN THE SOLUTION:
00135         // ---------------------------------------------
00136         DMatrix var;
00137         algorithm.getParameterVarianceCovariance( var );
00138 
00139 //      return 0;
00140 
00141         // PRINT THE RESULT ON THE TERMINAL:
00142         // -----------------------------------------------------------------------
00143 
00144         printf("\n\nResults for the parameters: \n");
00145         printf("-----------------------------------------------\n");
00146         printf("   l      =  %.3e  +/-  %.3e \n", parameters(0,0), sqrt( var(0,0) )  );
00147         printf("   alpha  =  %.3e  +/-  %.3e \n", parameters(0,1), sqrt( var(1,1) )  );
00148         printf("   g      =  %.3e  +/-  %.3e \n", parameters(0,2), sqrt( var(2,2) )  );
00149         printf("-----------------------------------------------\n\n\n");
00150 
00151 
00152     // PLOT THE RESULT:
00153     // ---------------------------------------------------
00154     algorithm.getPlotWindow( window );
00155 
00156         window.addData( 0, measurements(0) );
00157         window.addLine( 2, parameters(0,0) + sqrt( var(0,0) ) );
00158         window.addLine( 2, parameters(0,0) - sqrt( var(0,0) ) );
00159         window.addLine( 3, parameters(0,1) + sqrt( var(1,1) ) );
00160         window.addLine( 3, parameters(0,1) - sqrt( var(1,1) ) );
00161 
00162         window.plot( );
00163 
00164     return 0;
00165 }


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:58:05