<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>OdeErrControl: Example and Test Using Maxabs Argument</title> <meta name="description" id="description" content="OdeErrControl: Example and Test Using Maxabs Argument"/> <meta name="keywords" id="keywords" content=" Odeerrcontrol example maxabs test "/> <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='_odeerrmaxabs.cpp_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="odeerrcontrol.cpp.xml" target="_top">Prev</a> </td><td><a href="odegear.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>library</option> <option>OdeErrControl</option> <option>OdeErrMaxabs.cpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>CppAD-></option> <option>Install</option> <option>Introduction</option> <option>AD</option> <option>ADFun</option> <option>library</option> <option>Example</option> <option>configure</option> <option>Appendix</option> </select> </td> <td> <select onchange='choose_down2(this)'> <option>library-></option> <option>ErrorHandler</option> <option>NearEqual</option> <option>speed_test</option> <option>SpeedTest</option> <option>NumericType</option> <option>CheckNumericType</option> <option>SimpleVector</option> <option>CheckSimpleVector</option> <option>nan</option> <option>pow_int</option> <option>Poly</option> <option>LuDetAndSolve</option> <option>RombergOne</option> <option>RombergMul</option> <option>Runge45</option> <option>Rosen34</option> <option>OdeErrControl</option> <option>OdeGear</option> <option>OdeGearControl</option> <option>BenderQuad</option> <option>LuRatio</option> <option>std_math_unary</option> <option>CppAD_vector</option> <option>TrackNewDel</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>OdeErrControl-></option> <option>OdeErrControl.cpp</option> <option>OdeErrMaxabs.cpp</option> </select> </td> <td>OdeErrMaxabs.cpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>OdeErrControl: Example and Test Using Maxabs Argument</big></big></b></center> Define <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow> <mi mathvariant='italic'>X</mi> <mo stretchy="false">:</mo> <mrow><mstyle mathvariant='bold'><mi mathvariant='bold'>R</mi> </mstyle></mrow> <mo stretchy="false">→</mo> <msup><mrow><mstyle mathvariant='bold'><mi mathvariant='bold'>R</mi> </mstyle></mrow> <mn>2</mn> </msup> </mrow></math> by <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow> <mtable rowalign="center" ><mtr><mtd columnalign="right" > <msub><mi mathvariant='italic'>X</mi> <mn>0</mn> </msub> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mtd><mtd columnalign="center" > <mo stretchy="false">=</mo> </mtd><mtd columnalign="left" > <mo stretchy="false">-</mo> <mi>exp</mi> <mo stretchy="false">(</mo> <mo stretchy="false">-</mo> <msub><mi mathvariant='italic'>w</mi> <mn>0</mn> </msub> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mtd></mtr><mtr><mtd columnalign="right" > <msub><mi mathvariant='italic'>X</mi> <mn>1</mn> </msub> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mtd><mtd columnalign="center" > <mo stretchy="false">=</mo> </mtd><mtd columnalign="left" > <mfrac><mrow><msub><mi mathvariant='italic'>w</mi> <mn>0</mn> </msub> </mrow> <mrow><msub><mi mathvariant='italic'>w</mi> <mn>1</mn> </msub> <mo stretchy="false">-</mo> <msub><mi mathvariant='italic'>w</mi> <mn>0</mn> </msub> </mrow> </mfrac> <mo stretchy="false">[</mo> <mi>exp</mi> <mo stretchy="false">(</mo> <mo stretchy="false">-</mo> <msub><mi mathvariant='italic'>w</mi> <mn>0</mn> </msub> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> <mo stretchy="false">-</mo> <mi>exp</mi> <mo stretchy="false">(</mo> <mo stretchy="false">-</mo> <msub><mi mathvariant='italic'>w</mi> <mn>1</mn> </msub> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> <mo stretchy="false">]</mo> </mtd></mtr></mtable> </mrow></math> It follows that <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow> <msub><mi mathvariant='italic'>X</mi> <mn>0</mn> </msub> <mo stretchy="false">(</mo> <mn>0</mn> <mo stretchy="false">)</mo> <mo stretchy="false">=</mo> <mn>1</mn> </mrow></math> , <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow> <msub><mi mathvariant='italic'>X</mi> <mn>1</mn> </msub> <mo stretchy="false">(</mo> <mn>0</mn> <mo stretchy="false">)</mo> <mo stretchy="false">=</mo> <mn>0</mn> </mrow></math> and <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow> <mtable rowalign="center" ><mtr><mtd columnalign="right" > <msubsup><mi mathvariant='italic'>X</mi> <mn>0</mn> <mrow><mo stretchy="false">(</mo> <mn>1</mn> <mo stretchy="false">)</mo> </mrow> </msubsup> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mtd><mtd columnalign="center" > <mo stretchy="false">=</mo> </mtd><mtd columnalign="left" > <mo stretchy="false">-</mo> <msub><mi mathvariant='italic'>w</mi> <mn>0</mn> </msub> <msub><mi mathvariant='italic'>X</mi> <mn>0</mn> </msub> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mtd></mtr><mtr><mtd columnalign="right" > <msubsup><mi mathvariant='italic'>X</mi> <mn>1</mn> <mrow><mo stretchy="false">(</mo> <mn>1</mn> <mo stretchy="false">)</mo> </mrow> </msubsup> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mtd><mtd columnalign="center" > <mo stretchy="false">=</mo> </mtd><mtd columnalign="left" > <mo stretchy="false">+</mo> <msub><mi mathvariant='italic'>w</mi> <mn>0</mn> </msub> <msub><mi mathvariant='italic'>X</mi> <mn>0</mn> </msub> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> <mo stretchy="false">-</mo> <msub><mi mathvariant='italic'>w</mi> <mn>1</mn> </msub> <msub><mi mathvariant='italic'>X</mi> <mn>1</mn> </msub> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mtd></mtr></mtable> </mrow></math> Note that <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow> <msub><mi mathvariant='italic'>X</mi> <mn>1</mn> </msub> <mo stretchy="false">(</mo> <mn>0</mn> <mo stretchy="false">)</mo> </mrow></math> is zero an if <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow> <msub><mi mathvariant='italic'>w</mi> <mn>0</mn> </msub> <mi mathvariant='italic'>t</mi> </mrow></math> is large, <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow> <msub><mi mathvariant='italic'>X</mi> <mn>0</mn> </msub> <mo stretchy="false">(</mo> <mi mathvariant='italic'>t</mi> <mo stretchy="false">)</mo> </mrow></math> is near zero. This example tests OdeErrControl using the <i>maxabs</i> argument. <code><font color="blue"> <pre style='display:inline'> # include <cstddef> // for size_t # include <cmath> // for exp # include <cppad/ode_err_control.hpp> // CppAD::OdeErrControl # include <cppad/near_equal.hpp> // CppAD::NearEqual # include <cppad/vector.hpp> // CppAD::vector # include <cppad/runge_45.hpp> // CppAD::Runge45 namespace { // -------------------------------------------------------------- class Fun { private: CppAD::vector<double> w; public: // constructor Fun(const CppAD::vector<double> &w_) : w(w_) { } // set f = x'(t) void Ode( const double &t, const CppAD::vector<double> &x, CppAD::vector<double> &f) { f[0] = - w[0] * x[0]; f[1] = + w[0] * x[0] - w[1] * x[1]; } }; // -------------------------------------------------------------- class Method { private: Fun F; public: // constructor Method(const CppAD::vector<double> &w_) : F(w_) { } void step( double ta, double tb, CppAD::vector<double> &xa , CppAD::vector<double> &xb , CppAD::vector<double> &eb ) { xb = CppAD::Runge45(F, 1, ta, tb, xa, eb); } size_t order(void) { return 4; } }; } bool OdeErrMaxabs(void) { bool ok = true; // initial return value CppAD::vector<double> w(2); w[0] = 10.; w[1] = 1.; Method method(w); CppAD::vector<double> xi(2); xi[0] = 1.; xi[1] = 0.; CppAD::vector<double> eabs(2); eabs[0] = 0.; eabs[1] = 0.; CppAD::vector<double> ef(2); CppAD::vector<double> xf(2); CppAD::vector<double> maxabs(2); double ti = 0.; double tf = 1.; double smin = .5; double smax = 1.; double scur = .5; double erel = 1e-4; bool accurate = false; while( ! accurate ) { xf = OdeErrControl(method, ti, tf, xi, smin, smax, scur, eabs, erel, ef, maxabs); accurate = true; size_t i; for(i = 0; i < 2; i++) accurate &= ef[i] <= erel * maxabs[i]; if( ! accurate ) smin = smin / 2; } double x0 = exp(-w[0]*tf); ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(x0, xf[0], erel, 0.); ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(0., ef[0], erel, erel); double x1 = w[0] * (exp(-w[0]*tf) - exp(-w[1]*tf))/(w[1] - w[0]); ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(x1, xf[1], erel, 0.); ok &= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(0., ef[1], erel, erel); return ok; } </pre> </font></code> <hr/>Input File: example/ode_err_maxabs.cpp </body> </html>