1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
// $Id: ode_evaluate.cpp 3788 2016-02-09 15:50:06Z bradbell $
/* --------------------------------------------------------------------------
CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell
CppAD is distributed under multiple licenses. This distribution is under
the terms of the
GNU General Public License Version 3.
A copy of this license is included in the COPYING file of this distribution.
Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
-------------------------------------------------------------------------- */
/*
$begin ode_evaluate.cpp$$
$spell
$$
$section ode_evaluate: Example and test$$
$mindex ode_evaluate$$
$code
$srcfile%speed/example/ode_evaluate.cpp%0%// BEGIN C++%// END C++%1%$$
$$
$end
*/
// BEGIN C++
# include <cppad/speed/ode_evaluate.hpp>
# include <cppad/speed/uniform_01.hpp>
# include <cppad/cppad.hpp>
bool ode_evaluate(void)
{ using CppAD::NearEqual;
using CppAD::AD;
bool ok = true;
size_t n = 3;
CppAD::vector<double> x(n);
CppAD::vector<double> ym(n * n);
CppAD::vector< AD<double> > X(n);
CppAD::vector< AD<double> > Ym(n);
// choose x
size_t j;
for(j = 0; j < n; j++)
{ x[j] = double(j + 1);
X[j] = x[j];
}
// declare independent variables
Independent(X);
// evaluate function
size_t m = 0;
CppAD::ode_evaluate(X, m, Ym);
// evaluate derivative
m = 1;
CppAD::ode_evaluate(x, m, ym);
// use AD to evaluate derivative
CppAD::ADFun<double> F(X, Ym);
CppAD::vector<double> dy(n * n);
dy = F.Jacobian(x);
size_t k;
for(k = 0; k < n * n; k++)
ok &= NearEqual(ym[k], dy[k] , 1e-7, 1e-7);
return ok;
}
// END C++
|