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

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

ode_gear.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>OdeGear: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="OdeGear: Example and Test"/>
<meta name="keywords" id="keywords" content=" odegear: example and test Odegear "/>
<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.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="odegear.xml" target="_top">Prev</a>
</td><td><a href="odegearcontrol.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>OdeGear</option>
<option>ode_gear.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>OdeGear-&gt;</option>
<option>ode_gear.cpp</option>
</select>
</td>
<td>ode_gear.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>OdeGear: 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>
<mi mathvariant='italic'>n</mi>
</msup>
</mrow></math>

 by

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi mathvariant='italic'>x</mi>
<mi mathvariant='italic'>i</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<msup><mi mathvariant='italic'>t</mi>
<mrow><mi mathvariant='italic'>i</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
</mrow>
</msup>
</mrow></math>

for 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>i</mi>
<mo stretchy="false">=</mo>
<mn>1</mn>
<mo stretchy="false">,</mo>
<mo stretchy="false">&#x02026;</mo>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>n</mi>
<mn>-1</mn>
</mrow></math>

.
It follows that

<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>
<mi mathvariant='italic'>i</mi>
</msub>
<mo stretchy="false">(</mo>
<mn>0</mn>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<mn>0</mn>
</mtd><mtd columnalign="right" >
<mrow><mstyle mathvariant='normal'><mi mathvariant='normal'>for</mi>
<mspace width='.3em'/>
<mi mathvariant='normal'>all</mi>
<mspace width='.3em'/>
</mstyle></mrow>
<mi mathvariant='italic'>i</mi>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mi mathvariant='italic'>x</mi>
<mi mathvariant='italic'>i</mi>
</msub>
<mo stretchy="false">'</mo>
<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" >
<mn>1</mn>
</mtd><mtd columnalign="right" >
<mrow><mstyle mathvariant='normal'><mi mathvariant='normal'>if</mi>
<mspace width='.3em'/>
</mstyle></mrow>
<mi mathvariant='italic'>i</mi>
<mo stretchy="false">=</mo>
<mn>0</mn>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mi mathvariant='italic'>x</mi>
<mi mathvariant='italic'>i</mi>
</msub>
<mo stretchy="false">'</mo>
<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 mathvariant='italic'>i</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<msup><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>i</mi>
</msup>
<mo stretchy="false">=</mo>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>i</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<msub><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>i</mi>
<mn>-1</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="right" >
<mrow><mstyle mathvariant='normal'><mi mathvariant='normal'>if</mi>
<mspace width='.3em'/>
</mstyle></mrow>
<mi mathvariant='italic'>i</mi>
<mo stretchy="false">&gt;</mo>
<mn>0</mn>
</mtd></mtr></mtable>
</mrow></math>

The example tests OdeGear using the relations above:

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

# include &lt;cppad/ode_gear.hpp&gt;
# include &lt;cppad/cppad.hpp&gt;        // For automatic differentiation

namespace {
	class Fun {
	public:
		// constructor
		Fun(bool use_x_) : use_x(use_x_) 
		{ }

		// compute f(t, x) both for double and <a href="ad.xml" target="_top">AD</a>&lt;double&gt;
		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)
		{	size_t n  = x.size();	
			Scalar ti(1);
			f[0]   = Scalar(1);
			size_t i;
			for(i = 1; i &lt; n; i++)
			{	ti *= t;
				// convert int(size_t) to avoid warning
				// on _MSC_VER systems
				if( use_x )
					f[i] = int(i+1) * x[i-1];
				else	f[i] = int(i+1) * ti;
			}
		}

		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.;
			}
		}

	private:
		const bool use_x;

	};
}

bool OdeGear(void)
{	bool ok = true; // initial return value
	size_t i, j;    // temporary indices

	size_t  m = 4;  // index of next value in X
	size_t  n = m;  // number of components in x(t)

	// vector of times
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) T(m+1); 
	double step = .1;
	T[0]        = 0.;
	for(j = 1; j &lt;= m; j++)
	{	T[j] = T[j-1] + step;
		step = 2. * step;
	}

	// initial values for x( T[m-j] ) 
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) X((m+1) * n);
	for(j = 0; j &lt; m; j++)
	{	double ti = T[j];
		for(i = 0; i &lt; n; i++)
		{	X[ j * n + i ] = ti;
			ti *= T[j];
		}
	}

	// error bound
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) e(n);

	size_t use_x;
	for( use_x = 0; use_x &lt; 2; use_x++)
	{	// function object depends on value of use_x
		Fun F(use_x &gt; 0); 

		// compute OdeGear approximation for x( T[m] )
		CppAD::OdeGear(F, m, n, T, X, e);

		double check = T[m];
		for(i = 0; i &lt; n; i++)
		{	// method is exact up to order m and x[i] = t^{i+1}
			if( i + 1 &lt;= m ) ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(
				X[m * n + i], check, 1e-10, 1e-10
			);
			// error bound should be zero up to order m-1
			if( i + 1 &lt; m ) ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(
				e[i], 0., 1e-10, 1e-10
			);
			// check value for next i
			check *= T[m];
		}
	}
	return ok;
}

</pre>

</font></code>


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

</body>
</html>