<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<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='_ode_err_maxabs.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="ode_err_control.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-&gt;</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-&gt;</option>
<option>CppAD</option>
<option>library</option>
<option>OdeErrControl</option>
<option>ode_err_maxabs.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>library-&gt;</option>
<option>ErrorHandler</option>
<option>NearEqual</option>
<option>speed_test</option>
<option>SpeedTest</option>
<option>time_test</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>CppAD_vector</option>
<option>thread_alloc</option>
<option>index_sort</option>
<option>BenderQuad</option>
<option>opt_val_hes</option>
<option>LuRatio</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>OdeErrControl-&gt;</option>
<option>ode_err_control.cpp</option>
<option>ode_err_maxabs.cpp</option>
</select>
</td>
<td>ode_err_maxabs.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">&#x02192;</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 
<code><i><font color="black"><span style='white-space: nowrap'>maxabs</span></font></i></code>
 argument.

<code><font color="blue">
<pre style='display:inline'> 

# include &lt;cstddef&gt;              // for size_t
# include &lt;cmath&gt;                // for exp
# include &lt;cppad/ode_err_control.hpp&gt;   // CppAD::OdeErrControl
# include &lt;cppad/near_equal.hpp&gt;    // CppAD::NearEqual
# include &lt;cppad/vector.hpp&gt; // CppAD::vector
# include &lt;cppad/runge_45.hpp&gt;      // CppAD::Runge45

namespace {
	// --------------------------------------------------------------
	class Fun {
	private:
		 CppAD::vector&lt;double&gt; w;
	public:
		// constructor
		Fun(const CppAD::vector&lt;double&gt; &amp;w_) : w(w_)
		{ } 

		// set f = x'(t)
		void Ode(
			const double                &amp;t, 
			const CppAD::vector&lt;double&gt; &amp;x, 
			CppAD::vector&lt;double&gt;       &amp;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&lt;double&gt; &amp;w_) : F(w_)
		{ }
		void step(
			double ta, 
			double tb, 
			CppAD::vector&lt;double&gt; &amp;xa ,
			CppAD::vector&lt;double&gt; &amp;xb ,
			CppAD::vector&lt;double&gt; &amp;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&lt;double&gt; w(2);
	w[0] = 10.;
	w[1] = 1.;
	Method method(w);

	CppAD::vector&lt;double&gt; xi(2);
	xi[0] = 1.;
	xi[1] = 0.;

	CppAD::vector&lt;double&gt; eabs(2);
	eabs[0] = 0.;
	eabs[1] = 0.;

	CppAD::vector&lt;double&gt; ef(2);
	CppAD::vector&lt;double&gt; xf(2);
	CppAD::vector&lt;double&gt; 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 &lt; 2; i++)
			accurate &amp;= ef[i] &lt;= erel * maxabs[i];
		if( ! accurate )
			smin = smin / 2;
	} 

	double x0 = exp(-w[0]*tf);
	ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(x0, xf[0], erel, 0.);
	ok &amp;= 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 &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(x1, xf[1], erel, 0.);
	ok &amp;= 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>
