<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>ODE Fitting Using Simple Representation</title> <meta name="description" id="description" content="ODE Fitting Using Simple Representation"/> <meta name="keywords" id="keywords" content=" ipopt_cppad_nlp ode example source "/> <style type='text/css'> body { color : black } body { background-color : white } A:link { color : blue } A:visited { color : purple } A:active { color : purple } </style> <script type='text/javascript' language='JavaScript' src='_ipopt_ode_simple.hpp_xml.js'> </script> </head> <body> <table><tr> <td> <a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a> </td> <td><a href="ipopt_ode_simple.xml" target="_top">Prev</a> </td><td><a href="ipopt_ode_fast.xml" target="_top">Next</a> </td><td> <select onchange='choose_across0(this)'> <option>Index-></option> <option>contents</option> <option>reference</option> <option>index</option> <option>search</option> <option>external</option> </select> </td> <td> <select onchange='choose_up0(this)'> <option>Up-></option> <option>CppAD</option> <option>Example</option> <option>General</option> <option>ipopt_cppad_nlp</option> <option>ipopt_cppad_ode</option> <option>ipopt_ode_simple</option> <option>ipopt_ode_simple.hpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>ipopt_cppad_nlp-></option> <option>ipopt_cppad_windows</option> <option>ipopt_get_started.cpp</option> <option>ipopt_cppad_ode</option> </select> </td> <td> <select onchange='choose_down2(this)'> <option>ipopt_cppad_ode-></option> <option>ipopt_ode_problem</option> <option>ipopt_ode_simple</option> <option>ipopt_ode_fast</option> <option>ipopt_ode_run.hpp</option> <option>ipopt_ode_check.cpp</option> <option>ipopt_ode_speed.cpp</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>ipopt_ode_simple-></option> <option>ipopt_ode_simple.hpp</option> </select> </td> <td>ipopt_ode_simple.hpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>ODE Fitting Using Simple Representation</big></big></b></center> <code><font color="blue"><pre style='display:inline'> # include "ipopt_ode_problem.hpp" // define in the empty namespace namespace { class FG_simple : public ipopt_cppad_fg_info { private: bool retape_; SizeVector N_; SizeVector S_; public: // derived class part of constructor FG_simple(bool retape, const SizeVector& N) : retape_ (retape), N_(N) { assert( N_[0] == 0 ); S_.resize( N.size() ); S_[0] = 0; for(size_t i = 1; i < N_.size(); i++) S_[i] = S_[i-1] + N_[i]; } // Evaluation of the objective f(x), and constraints g(x) // using an Algorithmic Differentiation (AD) class. ADVector eval_r(size_t not_used, const ADVector& x) { // temporary indices size_t i, j, k; // # of components of x corresponding to values for y size_t ny_inx = (S_[Nz] + 1) * Ny; // # of constraints (range dimension of g) size_t m = ny_inx; // # of components in x (domain dimension for f and g) assert ( x.size() == ny_inx + Na ); // vector for return value ADVector fg(m + 1); // vector of parameters ADVector a(Na); for(j = 0; j < Na; j++) a[j] = x[ny_inx + j]; // vector for value of y(t) ADVector y(Ny); // objective function ------------------------------- fg[0] = 0.; for(k = 0; k < Nz; k++) { for(j = 0; j < Ny; j++) y[j] = x[Ny*S_[k+1] + j]; fg[0] += eval_H<ADNumber>(k+1, y, a); } // initial condition --------------------------------- ADVector F = eval_F(a); for(j = 0; j < Ny; j++) { y[j] = x[j]; fg[1+j] = y[j] - F[j]; } // trapezoidal approximation -------------------------- ADVector ym(Ny), G(Ny), Gm(Ny); G = eval_G(y, a); ADNumber dy; for(k = 0; k < Nz; k++) { // interval between data points Number T = s[k+1] - s[k]; // integration step size Number dt = T / Number( N_[k+1] ); for(j = 0; j < N_[k+1]; j++) { size_t index = (j + S_[k]) * Ny; // y(t) at end of last step ym = y; // G(y, a) at end of last step Gm = G; // value of y(t) at end of this step for(i = 0; i < Ny; i++) y[i] = x[Ny + index + i]; // G(y, a) at end of this step G = eval_G(y, a); // trapezoidal approximation residual for(i = 0; i < Ny; i++) { dy = (G[i] + Gm[i]) * dt / 2; fg[1+Ny+index+i] = y[i] - ym[i] - dy; } } } return fg; } // The operations sequence for r_eval does not depend on u, // hence retape = false should work and be faster. bool retape(size_t k) { return retape_; } }; }</pre> </font></code> <hr/>Input File: ipopt_cppad/ipopt_ode_simple.hpp </body> </html>