numeric_differentiation.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 
00034 #include <acado/utils/acado_utils.hpp>
00035 #include <acado/user_interaction/user_interaction.hpp>
00036 #include <acado/symbolic_expression/symbolic_expression.hpp>
00037 #include <acado/function/function.hpp>
00038 
00039 using namespace std;
00040 
00041 USING_NAMESPACE_ACADO
00042 
00043 /* >>> start tutorial code >>> */
00044 void my_function( double *x_, double *f, void *user_data ){
00045 
00046 //  double          t =  x_[ 0];    // the time
00047     double          x =  x_[ 1];    // the first  differential state
00048     double          y =  x_[ 2];    // the second differential state
00049 
00050     f[0] = x*x + pow(y,3);
00051 }
00052 
00053 int main( )
00054 {
00055         DifferentialState a, b;
00056         TIME t;
00057 
00058         CFunction myFunction(1, my_function);
00059 
00060         IntermediateState x(3);
00061 
00062         return 0;
00063 
00064         x(0) = t;
00065         x(1) = a;
00066         x(2) = b;
00067 
00068         Function f;
00069         f << myFunction(x);
00070 
00071         // TEST THE FUNCTION f:
00072         // --------------------
00073         int x_index, y_index;
00074 
00075         x_index = f.index(VT_DIFFERENTIAL_STATE, 0);
00076         y_index = f.index(VT_DIFFERENTIAL_STATE, 1);
00077 
00078         double *xx = new double[f.getNumberOfVariables() + 1];
00079         double *seed = new double[f.getNumberOfVariables() + 1];
00080         double *ff = new double[f.getDim()];
00081         double *df = new double[f.getDim()];
00082 
00083         xx[x_index] = 1.0;
00084         xx[y_index] = 1.0;
00085 
00086         seed[x_index] = 0.5;
00087         seed[y_index] = 0.5;
00088 
00089         // FORWARD DIFFERENTIATION:
00090         // ------------------------
00091         f.evaluate(0, xx, ff);
00092         f.AD_forward(0, seed, df);
00093 
00094         // PRINT THE RESULTS:
00095         // ------------------
00096         cout << scientific
00097                  << "     x = " << xx[x_index] << endl
00098                  << "     y = " << xx[y_index] << endl
00099                  << "seed_x = " << seed[x_index] << endl
00100                  << "seed_y = " << seed[y_index] << endl
00101                  << "     f = " << ff[0] << endl
00102                  << "    df = " << df[0] << endl;
00103 
00104         delete[] xx;
00105         delete[] seed;
00106         delete[] ff;
00107         delete[] df;
00108 
00109         return 0;
00110 }
00111 /* <<< end tutorial code <<< */
00112 
00113 


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