<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>One Dimensional Romberg Integration: Example and Test</title>
<meta name="description" id="description" content="One Dimensional Romberg Integration: Example and Test"/>
<meta name="keywords" id="keywords" content=" Romberg 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='_rombergmul.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="rombergmul.xml" target="_top">Prev</a>
</td><td><a href="runge45.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>RombergMul</option>
<option>Rombergmul.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>RombergMul-&gt;</option>
<option>Rombergmul.cpp</option>
</select>
</td>
<td>Rombergmul.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>One Dimensional Romberg Integration: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

# include &lt;cppad/romberg_mul.hpp&gt;
# include &lt;cppad/vector.hpp&gt;
# include &lt;cppad/near_equal.hpp&gt;


namespace {

	class TestFun {
	private:
		const CppAD::vector&lt;size_t&gt; deg;
	public:
		// constructor
		TestFun(const CppAD::vector&lt;size_t&gt; deg_) 
		: deg(deg_)
		{ }

		// function F(x) = x[0]^deg[0] * x[1]^deg[1]
		double operator () (const CppAD::vector&lt;double&gt; &amp;x)
		{	size_t i;
			double   f = 1;
			for(i = 0; i &lt; deg[0]; i++)
				f *= x[0];
			for(i = 0; i &lt; deg[1]; i++)
				f *= x[1];
			return f;
		}
	};

}

bool RombergMul(void)
{	bool ok = true;
	size_t i;
	size_t k;

	CppAD::vector&lt;size_t&gt; deg(2);
	deg[0] = 5;
	deg[1] = 3;
	TestFun F(deg);

	CppAD::RombergMul&lt;
		TestFun              , 
		CppAD::vector&lt;size_t&gt;, 
		CppAD::vector&lt;double&gt;, 
		2                    &gt; RombergMulTest;

	// arugments to RombergMul
	CppAD::vector&lt;double&gt; a(2);
	CppAD::vector&lt;double&gt; b(2);
	CppAD::vector&lt;size_t&gt; n(2);
	CppAD::vector&lt;size_t&gt; p(2);
	for(i = 0; i &lt; 2; i++)
	{	a[i] = 0.;
		b[i] = 1.;
	}
	n[0] = 4;
	n[1] = 3;
	double r, e;

	// int_a1^b1 dx1 int_a0^b0 F(x0,x1) dx0
	//	= [ b0^(deg[0]+1) - a0^(deg[0]+1) ] / (deg[0]+1) 
	//	* [ b1^(deg[1]+1) - a1^(deg[1]+1) ] / (deg[1]+1) 
	double bpow = 1.;
	double apow = 1.;
	for(i = 0; i &lt;= deg[0]; i++)
	{	bpow *= b[0];
		apow *= a[0];
	}  
	double check = (bpow - apow) / (deg[0]+1);
	bpow = 1.;
	apow = 1.;
	for(i = 0; i &lt;= deg[1]; i++)
	{	bpow *= b[1];
		apow *= a[1];
	}  
	check *= (bpow - apow) / (deg[1]+1);

	double step = (b[1] - a[1]) / exp(log(2.)*(n[1]-1));
	double spow = 1;
	for(k = 0; k &lt;= n[1]; k++)
	{	spow = spow * step * step;
		double bnd = 3 * (deg[1] + 1) * spow;

		for(i = 0; i &lt; 2; i++)
			p[i] = k;
		r    = RombergMulTest(F, a, b, n, p, e);

		ok  &amp;= e &lt; bnd;
		ok  &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(check, r, 0., e);	

	}

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
