numeric_differentiation.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 
38 
39 using namespace std;
40 
42 
43 /* >>> start tutorial code >>> */
44 void my_function( double *x_, double *f, void *user_data ){
45 
46 // double t = x_[ 0]; // the time
47  double x = x_[ 1]; // the first differential state
48  double y = x_[ 2]; // the second differential state
49 
50  f[0] = x*x + pow(y,3);
51 }
52 
53 int main( )
54 {
55  DifferentialState a, b;
56  TIME t;
57 
58  CFunction myFunction(1, my_function);
59 
60  IntermediateState x(3);
61 
62  return 0;
63 
64  x(0) = t;
65  x(1) = a;
66  x(2) = b;
67 
68  Function f;
69  f << myFunction(x);
70 
71  // TEST THE FUNCTION f:
72  // --------------------
73  int x_index, y_index;
74 
75  x_index = f.index(VT_DIFFERENTIAL_STATE, 0);
76  y_index = f.index(VT_DIFFERENTIAL_STATE, 1);
77 
78  double *xx = new double[f.getNumberOfVariables() + 1];
79  double *seed = new double[f.getNumberOfVariables() + 1];
80  double *ff = new double[f.getDim()];
81  double *df = new double[f.getDim()];
82 
83  xx[x_index] = 1.0;
84  xx[y_index] = 1.0;
85 
86  seed[x_index] = 0.5;
87  seed[y_index] = 0.5;
88 
89  // FORWARD DIFFERENTIATION:
90  // ------------------------
91  f.evaluate(0, xx, ff);
92  f.AD_forward(0, seed, df);
93 
94  // PRINT THE RESULTS:
95  // ------------------
96  cout << scientific
97  << " x = " << xx[x_index] << endl
98  << " y = " << xx[y_index] << endl
99  << "seed_x = " << seed[x_index] << endl
100  << "seed_y = " << seed[y_index] << endl
101  << " f = " << ff[0] << endl
102  << " df = " << df[0] << endl;
103 
104  delete[] xx;
105  delete[] seed;
106  delete[] ff;
107  delete[] df;
108 
109  return 0;
110 }
111 /* <<< end tutorial code <<< */
112 
113 
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
USING_NAMESPACE_ACADO void my_function(double *x_, double *f, void *user_data)
#define USING_NAMESPACE_ACADO
DVector evaluate(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:520
IntermediateState pow(const Expression &arg1, const Expression &arg2)
int getDim() const
int index(VariableType variableType_, int index_) const
Definition: function.cpp:176
DVector AD_forward(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:533
(no description yet)
Definition: c_function.hpp:54
int getNumberOfVariables() const
Definition: function.cpp:264


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