pendulum_C.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_integrators.hpp>
00036 
00037 
00038 USING_NAMESPACE_ACADO
00039 
00040 
00041 void ffcn_model( double *x, double *f, void *user_data ){
00042 
00043 //  double       time =  x[ 0];    // the time
00044     double        phi =  x[ 1];    // the angle phi
00045     double       dphi =  x[ 2];    // the first derivative of phi w.r.t time
00046     double          F =  x[ 3];    // a force acting on the pendulum
00047     double          l =  x[ 4];    // the length of the pendulum
00048 
00049     const double m     = 1.0  ;    // the mass of the pendulum
00050     const double g     = 9.81 ;    // the gravitational constant
00051     const double alpha = 2.0  ;    // frictional constant
00052 
00053     f[0] = dphi;
00054     f[1] = -(m*g/l)*sin(phi) - alpha*dphi + F/(m*l);
00055 }
00056 
00057 
00058 int main( ){
00059 
00060     DifferentialState phi, dphi;
00061 
00062     Control u;
00063     Parameter p;
00064     TIME t;
00065 
00066     IntermediateState x(5);
00067 
00068     x(0) = t   ;
00069     x(1) = phi ;
00070     x(2) = dphi;
00071     x(3) = u   ;
00072     x(4) = p   ;
00073 
00074     CFunction pendulumModel( 2, ffcn_model );
00075 
00076     // Define a Right-Hand-Side:
00077     // -------------------------
00078 
00079     DifferentialEquation f;
00080 
00081     f << pendulumModel(x);
00082 
00083 
00084     // DEFINE AN INTEGRATOR:
00085     // ---------------------
00086 
00087         IntegratorRK45 integrator( f );
00088 
00089         integrator.set(INTEGRATOR_PRINTLEVEL, HIGH );
00090         
00091 
00092     // DEFINE INITIAL VALUES:
00093     // ----------------------
00094 
00095     double x_start[2] = { 0.0, 0.0 };
00096     double u_     [1] = { 1.0      };
00097     double p_     [1] = { 1.0      };
00098 
00099     double t_start    =  0.0;
00100     double t_end      =  1.0;
00101 
00102 
00103     // START THE INTEGRATION:
00104     // ----------------------
00105 
00106     integrator.freezeAll();
00107     integrator.integrate( t_start, t_end, x_start, 0, p_, u_ );
00108 
00109 
00110     // DEFINE A SEED MATRIX:
00111     // ---------------------
00112     DVector seed1(2);
00113     DVector seed2(2);
00114 
00115     seed1(0) = 1.0;
00116     seed1(1) = 0.0;
00117 
00118     seed2(0) = 1.0;
00119     seed2(1) = 0.0;
00120 
00121     // COMPUTE FIRST ORDER DERIVATIVES:
00122     // --------------------------------
00123     integrator.setForwardSeed(1,seed1);
00124     integrator.integrateSensitivities();
00125 
00126     // COMPUTE SECOND ORDER DERIVATIVES:
00127     // ---------------------------------
00128     integrator.setForwardSeed(2,seed2);
00129     integrator.integrateSensitivities();
00130 
00131 
00132     // GET THE RESULTS
00133     // ---------------
00134 
00135         VariablesGrid differentialStates;
00136         integrator.getX( differentialStates );
00137         
00138         DVector Dx( 2 ), DDx( 2 );
00139         integrator.getForwardSensitivities( Dx,1 );
00140         integrator.getForwardSensitivities( DDx,2 );
00141         
00142         differentialStates.print( "x" );
00143         Dx.print( "Dx" );
00144         DDx.print( "DDx" );
00145 
00146 
00147     return 0;
00148 }
00149 
00150 
00151 


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