<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Runge45: Example and Test</title>
<meta name="description" id="description" content="Runge45: Example and Test"/>
<meta name="keywords" id="keywords" content=" Runge45 example 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='_runge45_2.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="runge45_1.cpp.xml" target="_top">Prev</a>
</td><td><a href="rosen34.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>Runge45</option>
<option>runge45_2.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>Runge45-&gt;</option>
<option>runge45_1.cpp</option>
<option>runge45_2.cpp</option>
</select>
</td>
<td>runge45_2.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Runge45: 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">&#x000D7;</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'>j</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mi mathvariant='italic'>b</mi>
<mrow><mo stretchy="true">(</mo><mrow><munderover><mo displaystyle='true' largeop='true'>&#x02211;</mo>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">=</mo>
<mn>0</mn>
</mrow>
<mi mathvariant='italic'>j</mi>
</munderover>
<msup><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>k</mi>
</msup>
<mo stretchy="false">/</mo>
<mi mathvariant='italic'>k</mi>
<mo stretchy="false">!</mo>
</mrow><mo stretchy="true">)</mo></mrow>
</mrow></math>

for 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>j</mi>
<mo stretchy="false">=</mo>
<mn>0</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'>j</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<mo stretchy="false">,</mo>
<mn>0</mn>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<mi mathvariant='italic'>b</mi>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mo stretchy="false">&#x02202;</mo>
<mi mathvariant='italic'>t</mi>
</msub>
<msub><mi mathvariant='italic'>X</mi>
<mi mathvariant='italic'>j</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<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" >
<mi mathvariant='italic'>b</mi>
<mrow><mo stretchy="true">(</mo><mrow><munderover><mo displaystyle='true' largeop='true'>&#x02211;</mo>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">=</mo>
<mn>0</mn>
</mrow>
<mrow><mi mathvariant='italic'>j</mi>
<mn>-1</mn>
</mrow>
</munderover>
<msup><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>k</mi>
</msup>
<mo stretchy="false">/</mo>
<mi mathvariant='italic'>k</mi>
<mo stretchy="false">!</mo>
</mrow><mo stretchy="true">)</mo></mrow>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mo stretchy="false">&#x02202;</mo>
<mi mathvariant='italic'>t</mi>
</msub>
<msub><mi mathvariant='italic'>X</mi>
<mi mathvariant='italic'>j</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<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" >
<mrow><mo stretchy="true">{</mo><mrow><mtable rowalign="center" ><mtr><mtd columnalign="left" >
<mn>0</mn>
</mtd><mtd columnalign="left" >
<mrow><mstyle mathvariant='normal'><mi mathvariant='normal'>if</mi>
</mstyle></mrow>
<mspace width='.3em'/>
<mi mathvariant='italic'>j</mi>
<mo stretchy="false">=</mo>
<mn>0</mn>
</mtd></mtr><mtr><mtd columnalign="left" >
<msub><mi mathvariant='italic'>X</mi>
<mrow><mi mathvariant='italic'>j</mi>
<mn>-1</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="left" >
<mrow><mstyle mathvariant='normal'><mi mathvariant='normal'>otherwise</mi>
</mstyle></mrow>
</mtd></mtr></mtable>
</mrow><mo stretchy="true"> </mo></mrow>
</mtd></mtr></mtable>
</mrow></math>

For a fixed 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msub><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>f</mi>
</msub>
</mrow></math>

,
we can use <a href="runge45.xml" target="_top"><span style='white-space: nowrap'>Runge45</span></a>
 to define 

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</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>

 as an approximation for

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mi mathvariant='italic'>X</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<mo stretchy="false">,</mo>
<msub><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>f</mi>
</msub>
<mo stretchy="false">)</mo>
</mrow></math>

.
We can then compute 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msup><mi mathvariant='italic'>f</mi>
<mrow><mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<mo stretchy="false">)</mo>
</mrow></math>

 which is an approximation for

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mo stretchy="false">&#x02202;</mo>
<mi mathvariant='italic'>b</mi>
</msub>
<mi mathvariant='italic'>X</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>b</mi>
<mo stretchy="false">,</mo>
<msub><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>f</mi>
</msub>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<munderover><mo displaystyle='true' largeop='true'>&#x02211;</mo>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">=</mo>
<mn>0</mn>
</mrow>
<mi mathvariant='italic'>j</mi>
</munderover>
<msubsup><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>f</mi>
<mi mathvariant='italic'>k</mi>
</msubsup>
<mo stretchy="false">/</mo>
<mi mathvariant='italic'>k</mi>
<mo stretchy="false">!</mo>
</mrow></math>

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

# include &lt;cstddef&gt;              // for size_t
# include &lt;limits&gt;               // for machine epsilon
# include &lt;cppad/cppad.hpp&gt;      // for all of CppAD

namespace {

	template &lt;class Scalar&gt;
	class Fun {
	public:
		// constructor
		Fun(void) 
		{ }

		// set return value to X'(t)
		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();	
			f[0]      = 0.;
			for(size_t k = 1; k &lt; n; k++)
				f[k] = x[k-1];
		}
	};
}

bool runge_45_2(void)
{	typedef CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; Scalar;
	using CppAD::NearEqual;

	bool ok = true;     // initial return value
	size_t j;           // temporary indices

	size_t     n = 5;   // number components in X(t) and order of method
	size_t     M = 2;   // number of Runge45 steps in [ti, tf]
	Scalar ad_ti = 0.;  // initial time
	Scalar ad_tf = 2.;  // final time 

	// value of independent variable at which to record operations
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(Scalar) ad_b(1);
	ad_b[0] = 1.;

	// declare b to be the independent variable
	<a href="independent.xml" target="_top">Independent</a>(ad_b);
	
	// object to evaluate ODE
	Fun&lt;Scalar&gt; ad_F; 

	// xi = X(0)
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(Scalar) ad_xi(n); 
	for(j = 0; j &lt; n; j++)
		ad_xi[j] = ad_b[0];

	// compute Runge45 approximation for X(tf)
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(Scalar) ad_xf(n), ad_e(n); 
	ad_xf = CppAD::Runge45(ad_F, M, ad_ti, ad_tf, ad_xi, ad_e);

	// stop recording and use it to create f : b -&gt; xf
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(ad_b, ad_xf);

	// evaluate f(b)
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)  b(1);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) xf(n);
	b[0] = 1.;
	xf   = f.<a href="forward.xml" target="_top">Forward</a>(0, b);

	// check that f(b) = X(b, tf)
	double tf    = Value(ad_tf);
	double term  = 1;
	double sum   = 0;
	double eps   = 10. * CppAD::numeric_limits&lt;double&gt;::epsilon();
	for(j = 0; j &lt; n; j++)
	{	sum += term;
		ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(xf[j], b[0] * sum, eps, eps);
		term *= tf;
		term /= double(j+1);
	}

	// evalute f'(b)
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) d_xf(n);
	d_xf = f.<a href="jacobian.xml" target="_top">Jacobian</a>(b);

	// check that f'(b) = partial of X(b, tf) w.r.t b
	term  = 1;
	sum   = 0;
	for(j = 0; j &lt; n; j++)
	{	sum += term;
		ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(d_xf[j], sum, eps, eps);
		term *= tf;
		term /= double(j+1);
	}
	
	return ok;
}

</pre>

</font></code>


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

</body>
</html>
