Codebase list cppad / upstream/2015.00.00.7 doc / ode_evaluate.hpp.xml
upstream/2015.00.00.7

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

ode_evaluate.hpp.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>Source: ode_evaluate</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Source: ode_evaluate"/>
<meta name="keywords" id="keywords" content=" source: ode_evaluate source "/>
<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_evaluate.hpp_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="ode_evaluate.cpp.xml" target="_top">Prev</a>
</td><td><a href="sparse_jac_fun.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>speed</option>
<option>speed_utility</option>
<option>ode_evaluate</option>
<option>ode_evaluate.hpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>speed-&gt;</option>
<option>speed_main</option>
<option>speed_utility</option>
<option>speed_double</option>
<option>speed_adolc</option>
<option>speed_cppad</option>
<option>speed_fadbad</option>
<option>speed_sacado</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>speed_utility-&gt;</option>
<option>det_by_lu</option>
<option>det_of_minor</option>
<option>det_by_minor</option>
<option>det_33</option>
<option>det_grad_33</option>
<option>mat_sum_sq</option>
<option>ode_evaluate</option>
<option>sparse_jac_fun</option>
<option>sparse_hes_fun</option>
<option>uniform_01</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>ode_evaluate-&gt;</option>
<option>ode_evaluate.cpp</option>
<option>ode_evaluate.hpp</option>
</select>
</td>
<td>ode_evaluate.hpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Source: ode_evaluate</big></big></b></center>
<code><font color="blue"># ifndef CPPAD_ODE_EVALUATE_INCLUDED 
<code><span style='white-space: nowrap'><br/>
</span></code># define CPPAD_ODE_EVALUATE_INCLUDED 

<pre style='display:inline'> 
# include &lt;cppad/vector.hpp&gt;
# include &lt;cppad/ode_err_control.hpp&gt;
# include &lt;cppad/runge_45.hpp&gt;

namespace CppAD { 

	template &lt;class Float&gt;
	class ode_evaluate_fun {
	public:
		// Given that y_i (0) = x_i, 
		// the following y_i (t) satisfy the ODE below:
		// y_0 (t) = x[0]
		// y_1 (t) = x[1] + x[0] * t 
		// y_2 (t) = x[2] + x[1] * t + x[0] * t^2/2
		// y_3 (t) = x[3] + x[2] * t + x[1] * t^2/2 + x[0] * t^3 / 3!
		// ...
		void Ode(
			const Float&amp;                    t, 
			const CppAD::vector&lt;Float&gt;&amp;     y, 
			CppAD::vector&lt;Float&gt;&amp;           f)
		{	size_t n  = y.size();	
			f[0]      = 0.;
			for(size_t k = 1; k &lt; n; k++)
				f[k] = y[k-1];
		}
	};
	//
	template &lt;class Float&gt;
	void ode_evaluate(
		const CppAD::vector&lt;Float&gt;&amp; x  , 
		size_t                      p  , 
		CppAD::vector&lt;Float&gt;&amp;       fp )
	{	using CppAD::vector;
		typedef vector&lt;Float&gt; VectorFloat;

		size_t n = x.size();
		CPPAD_ASSERT_KNOWN( p == 0 || p == 1,
			&quot;ode_evaluate: p is not zero or one&quot;
		);
		CPPAD_ASSERT_KNOWN( 
			((p==0) &amp; (fp.size()==n)) || ((p==1) &amp; (fp.size()==n*n)),
			&quot;ode_evaluate: the size of fp is not correct&quot;
		);
		if( p == 0 )
		{	// function that defines the ode
			ode_evaluate_fun&lt;Float&gt; F;

			// number of Runge45 steps to use
			size_t M = 10;

			// initial and final time
			Float ti = 0.0;
			Float tf = 1.0;

			// initial value for y(x, t); i.e. y(x, 0)
			// (is a reference to x)
			const VectorFloat&amp; yi = x;

			// final value for y(x, t); i.e., y(x, 1)
			// (is a reference to fp)
			VectorFloat&amp; yf = fp;
			
			// Use fourth order Runge-Kutta to solve ODE
			yf = CppAD::Runge45(F, M, ti, tf, yi);

			return;
		}
		/* Compute derivaitve of y(x, 1) w.r.t x
		y_0 (x, t) = x[0]
		y_1 (x, t) = x[1] + x[0] * t 
		y_2 (x, t) = x[2] + x[1] * t + x[0] * t^2/2
		y_3 (x, t) = x[3] + x[2] * t + x[1] * t^2/2 + x[0] * t^3 / 3!
		...
		*/
		size_t i, j, k;
		for(i = 0; i &lt; n; i++)
		{	for(j = 0; j &lt; n; j++)
				fp[ i * n + j ] = 0.0;
		}
		size_t factorial = 1;
		for(k = 0; k &lt; n; k++)
		{	if( k &gt; 1 )
				factorial *= k; 
			for(i = k; i &lt; n; i++)
			{	// partial w.r.t x[i-k] of x[i-k] * t^k / k!
				j = i - k;
				fp[ i * n + j ] += 1.0 / Float(factorial);
			}
		}
	}
}
</pre>


# endif
</font></code>


<hr/>Input File: omh/ode_evaluate.omh

</body>
</html>