Codebase list cppad / upstream/2015.00.00.7 doc / ode_gear_control.cpp.xml
upstream/2015.00.00.7

Tree @upstream/2015.00.00.7 (Download .tar.gz)

ode_gear_control.cpp.xml @upstream/2015.00.00.7raw · history · blame

<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>OdeGearControl: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="OdeGearControl: Example and Test"/>
<meta name="keywords" id="keywords" content=" odegearcontrol: example and test Odegearcontrol "/>
<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_gear_control.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="odegearcontrol.xml" target="_top">Prev</a>
</td><td><a href="cppad_vector.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>OdeGearControl</option>
<option>ode_gear_control.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>OdeGearControl-&gt;</option>
<option>ode_gear_control.cpp</option>
</select>
</td>
<td>ode_gear_control.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>OdeGearControl: Example and Test</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>

The example tests OdeGearControl using the relations above:

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

# include &lt;cppad/cppad.hpp&gt;
# include &lt;cppad/ode_gear_control.hpp&gt;   // CppAD::OdeGearControl

namespace {
	// --------------------------------------------------------------
	class Fun {
	private:
		 <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) w;
	public:
		// constructor
		Fun(const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) &amp;w_) : w(w_)
		{ } 

		// set f = x'(t)
		template &lt;typename Scalar&gt;
		void Ode(
			const Scalar                    &amp;t, 
			const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(Scalar) &amp;x, 
			<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(Scalar)       &amp;f)
		{	f[0] = - w[0] * x[0];
			f[1] = + w[0] * x[0] - w[1] * x[1];	
		}

		void Ode_dep(
			const double                    &amp;t, 
			const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) &amp;x, 
			<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)       &amp;f_x)
		{	using namespace CppAD;

			size_t n  = x.size();	
			<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) T(1);
			<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) X(n);
			<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) F(n);

			// set argument values
			T[0] = t;
			size_t i, j;
			for(i = 0; i &lt; n; i++)
				X[i] = x[i];

			// declare independent variables
			<a href="independent.xml" target="_top">Independent</a>(X);

			// compute f(t, x)
			this-&gt;Ode(T[0], X, F);

			// define AD function object
			<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; fun(X, F);

			// compute partial of f w.r.t x
			<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n);
			<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) df(n);
			for(j = 0; j &lt; n; j++)
				dx[j] = 0.;
			for(j = 0; j &lt; n; j++)
			{	dx[j] = 1.;
				df = fun.<a href="forward.xml" target="_top">Forward</a>(1, dx);
				for(i = 0; i &lt; n; i++)
					f_x [i * n + j] = df[i];
				dx[j] = 0.;
			}
		}
	};
}

bool OdeGearControl(void)
{	bool ok = true;     // initial return value

	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) w(2);
	w[0] = 10.;
	w[1] = 1.;
	Fun F(w);

	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) xi(2);
	xi[0] = 1.;
	xi[1] = 0.;

	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) eabs(2);
	eabs[0] = 1e-4;
	eabs[1] = 1e-4;

	// return values
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) ef(2);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) maxabs(2);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) xf(2);
	size_t                nstep;

	// input values
	size_t  M   = 5;
	double ti   = 0.;
	double tf   = 1.;
	double smin = 1e-8;
	double smax = 1.;
	double sini = 1e-10;
	double erel = 0.;
	
	xf = CppAD::OdeGearControl(F, M,
		ti, tf, xi, smin, smax, sini, eabs, erel, ef, maxabs, nstep);

	double x0 = exp(-w[0]*tf);
	ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(x0, xf[0], 1e-4, 1e-4);
	ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(0., ef[0], 1e-4, 1e-4);

	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], 1e-4, 1e-4);
	ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(0., ef[1], 1e-4, 1e-4);

	return ok;
}

</pre>

</font></code>


<hr/>Input File: example/ode_gear_control.cpp

</body>
</html>