<?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_1.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.xml" target="_top">Prev</a>
</td><td><a href="runge45_2.cpp.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_1.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_1.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">&#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 Runge45 using the relations above:

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

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

// Runge45 requires fabs to be defined (not std::fabs)
// &lt;cppad/cppad.hpp&gt; defines this for doubles, but runge_45.hpp does not. 
# include &lt;math.h&gt;      // for fabs without std in front

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

		// 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)
		{	size_t n  = x.size();	
			double ti = 1.;
			f[0]      = 1.;
			size_t i;
			for(i = 1; i &lt; n; i++)
			{	ti *= t;
				if( use_x )
					f[i] = (i+1) * x[i-1];
				else	f[i] = (i+1) * ti;
			}
		}
	private:
		const bool use_x;

	};
}

bool runge_45_1(void)
{	bool ok = true;     // initial return value
	size_t i;           // 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]
	double ti = 0.;     // initial time
	double tf = 2.;     // final time 

	// xi = X(0)
	CppAD::vector&lt;double&gt; xi(n); 
	for(i = 0; i &lt;n; i++)
		xi[i] = 0.;

	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 Runge45 approximation for X(tf)
		CppAD::vector&lt;double&gt; xf(n), e(n); 
		xf = CppAD::Runge45(F, M, ti, tf, xi, e);

		double check = tf;
		for(i = 0; i &lt; n; i++)
		{	// check that error is always positive
			ok    &amp;= (e[i] &gt;= 0.);
			// 5th order method is exact for i &lt; 5
			if( i &lt; 5 ) ok &amp;=
				CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(xf[i], check, 1e-10, 1e-10);
			// 4th order method is exact for i &lt; 4
			if( i &lt; 4 )
				ok &amp;= (e[i] &lt;= 1e-10);

			// check value for next i
			check *= tf;
		}
	}
	return ok;
}

</pre>

</font></code>


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

</body>
</html>
