File: ode_problem.hpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (88 lines) | stat: -rw-r--r-- 2,908 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# ifndef CPPAD_CPPAD_IPOPT_EXAMPLE_ODE_PROBLEM_HPP
# define CPPAD_CPPAD_IPOPT_EXAMPLE_ODE_PROBLEM_HPP
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin ipopt_nlp_ode_problem.hpp dev}

ODE Inverse Problem Definitions: Source Code
############################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end ipopt_nlp_ode_problem.hpp}
------------------------------------------------------------------------------
*/
// BEGIN C++
# include "../src/cppad_ipopt_nlp.hpp"

namespace {
   //------------------------------------------------------------------
   typedef Ipopt::Number Number;
   Number a0 = 1.;  // simulation value for a[0]
   Number a1 = 2.;  // simulation value for a[1]
   Number a2 = 1.;  // simulatioln value for a[2]

   // function used to simulate data
   Number y_one(Number t)
   {  Number y_1 =  a0*a1 * (exp(-a2*t) - exp(-a1*t)) / (a1 - a2);
      return y_1;
   }

   // time points were we have data (no data at first point)
   double s[] = { 0.0,        0.5,        1.0,        1.5,        2.0 };
   // Simulated data for case with no noise (first point is not used)
   double z[] = { 0.0,  y_one(0.5), y_one(1.0), y_one(1.5), y_one(2.0) };
   // Number of measurement values
   size_t Nz  = sizeof(z) / sizeof(z[0]) - 1;
   // Number of components in the function y(t, a)
   size_t Ny  = 2;
   // Number of components in the vectro a
   size_t Na  = 3;

   // Initial Condition function, F(a) = y(t, a) at t = 0
   // (for this particular example)
   template <class Vector>
   Vector eval_F(const Vector &a)
   {  Vector F(Ny);
      // y_0 (t) = a[0]*exp(-a[1] * t)
      F[0] = a[0];
      // y_1 (t) =
      // a[0]*a[1]*(exp(-a[2] * t) - exp(-a[1] * t))/(a[1] - a[2])
      F[1] = 0.;
      return F;
   }
   // G(y, a) =  \partial_t y(t, a); i.e. the differential equation
   // (for this particular example)
   template <class Vector>
   Vector eval_G(const Vector &y , const Vector &a)
   {  Vector G(Ny);
      // y_0 (t) = a[0]*exp(-a[1] * t)
      G[0] = -a[1] * y[0];
      // y_1 (t) =
      // a[0]*a[1]*(exp(-a[2] * t) - exp(-a[1] * t))/(a[1] - a[2])
      G[1] = +a[1] * y[0] - a[2] * y[1];
      return G;
   }
   // H(i, y, a) = contribution to objective at i-th data point
   // (for this particular example)
   template <class Scalar, class Vector>
   Scalar eval_H(size_t i, const Vector &y, const Vector &a)
   {  // This particular H is for a case where y_1 (t) is measured
      Scalar diff = z[i] - y[1];
      return diff * diff;
   }
   // function used to count the number of calls to eval_r
   size_t count_eval_r(void)
   {  static size_t count = 0;
      ++count;
      return count;
   }
}
// END C++
# endif