<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Getting Started Using CppAD to Compute Derivatives</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Getting Started Using CppAD to Compute Derivatives"/>
<meta name="keywords" id="keywords" content=" getting started using cppad to compute derivatives simple example start purpose function derivative value poly exercises program output running "/>
<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='_get_started.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="example.xml" target="_top">Prev</a>
</td><td><a href="general.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>Example</option>
<option>get_started.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down2(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>utility</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>Example-&gt;</option>
<option>get_started.cpp</option>
<option>General</option>
<option>ExampleUtility</option>
<option>ListAllExamples</option>
<option>testvector</option>
<option>wno_conversion</option>
</select>
</td>
<td>get_started.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
<option>Function</option>
<option>Derivative</option>
<option>Value</option>
<option>Poly</option>
<option>Exercises</option>
<option>Program</option>
<option>Output</option>
<option>Running</option>
</select>
</td>
</tr></table><br/>
<center><b><big><big>Getting Started Using CppAD to Compute Derivatives</big></big></b></center>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
Demonstrate the use of CppAD by computing the derivative
of a simple example function.

<br/>
<br/>
<b><big><a name="Function" id="Function">Function</a></big></b>
<br/>
The example function 
<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>
<mrow><mstyle mathvariant='bold'><mi mathvariant='bold'>R</mi>
</mstyle></mrow>
</mrow></math>

 is defined by

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<msub><mi mathvariant='italic'>a</mi>
<mn>0</mn>
</msub>
<mo stretchy="false">+</mo>
<msub><mi mathvariant='italic'>a</mi>
<mn>1</mn>
</msub>
<mo stretchy="false">*</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>1</mn>
</msup>
<mo stretchy="false">+</mo>
<mo stretchy="false">&#x022EF;</mo>
<mo stretchy="false">+</mo>
<msub><mi mathvariant='italic'>a</mi>
<mrow><mi mathvariant='italic'>k</mi>
<mn>-1</mn>
</mrow>
</msub>
<mo stretchy="false">*</mo>
<msup><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>k</mi>
<mn>-1</mn>
</mrow>
</msup>
</mrow></math>

where 
<code><i><font color="black"><span style='white-space: nowrap'>a</span></font></i></code>
 is a fixed vector of length 
<code><i><font color="black"><span style='white-space: nowrap'>k</span></font></i></code>
.

<br/>
<br/>
<b><big><a name="Derivative" id="Derivative">Derivative</a></big></b>
<br/>
The derivative of 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mrow></math>

 is given by

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">'</mo>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<msub><mi mathvariant='italic'>a</mi>
<mn>1</mn>
</msub>
<mo stretchy="false">+</mo>
<mn>2</mn>
<mo stretchy="false">*</mo>
<msub><mi mathvariant='italic'>a</mi>
<mn>2</mn>
</msub>
<mo stretchy="false">*</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">+</mo>
<mo stretchy="false">&#x022EF;</mo>
<mo stretchy="false">+</mo>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>k</mi>
<mn>-1</mn>
<mo stretchy="false">)</mo>
<mo stretchy="false">*</mo>
<msub><mi mathvariant='italic'>a</mi>
<mrow><mi mathvariant='italic'>k</mi>
<mn>-1</mn>
</mrow>
</msub>
<mo stretchy="false">*</mo>
<msup><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>k</mi>
<mn>-2</mn>
</mrow>
</msup>
</mrow></math>

<br/>
<b><big><a name="Value" id="Value">Value</a></big></b>
<br/>
For the particular case in this example,

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>k</mi>
</mrow></math>

 is equal to 5,

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>a</mi>
<mo stretchy="false">=</mo>
<mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">,</mo>
<mn>1</mn>
<mo stretchy="false">,</mo>
<mn>1</mn>
<mo stretchy="false">,</mo>
<mn>1</mn>
<mo stretchy="false">,</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow></math>

, and

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">=</mo>
<mn>3</mn>
</mrow></math>

.
If follows that

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">'</mo>
<mo stretchy="false">(</mo>
<mn>3</mn>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mn>1</mn>
<mo stretchy="false">+</mo>
<mn>2</mn>
<mo stretchy="false">*</mo>
<mn>3</mn>
<mo stretchy="false">+</mo>
<mn>3</mn>
<mo stretchy="false">*</mo>
<msup><mn>3</mn>
<mn>2</mn>
</msup>
<mo stretchy="false">+</mo>
<mn>4</mn>
<mo stretchy="false">*</mo>
<msup><mn>3</mn>
<mn>3</mn>
</msup>
<mo stretchy="false">=</mo>
<mn>142</mn>
</mrow></math>

<br/>
<b><big><a name="Poly" id="Poly">Poly</a></big></b>
<br/>
The routine <code><font color="blue">Poly</font></code> is defined below for this particular application.
A general purpose polynomial evaluation routine is documented and
distributed with CppAD (see <a href="poly.xml" target="_top"><span style='white-space: nowrap'>Poly</span></a>
).

<br/>
<br/>
<b><big><a name="Exercises" id="Exercises">Exercises</a></big></b>
<br/>
Modify the program below to accomplish the following tasks
using CppAD:
<ol type="1"><li>
Compute and print the derivative of 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mn>1</mn>
<mo stretchy="false">+</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>2</mn>
</msup>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>3</mn>
</msup>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>4</mn>
</msup>
</mrow></math>


at the point 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">=</mo>
<mn>2</mn>
</mrow></math>

.
</li><li>

Compute and print the derivative of 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mn>1</mn>
<mo stretchy="false">+</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>2</mn>
</msup>
<mo stretchy="false">/</mo>
<mn>2</mn>
</mrow></math>


at the point 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">=</mo>
<mn>.5</mn>
</mrow></math>

.
</li><li>

Compute and print the derivative of 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mi>exp</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">-</mo>
<mn>1</mn>
<mo stretchy="false">-</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">-</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>2</mn>
</msup>
<mo stretchy="false">/</mo>
<mn>2</mn>
</mrow></math>


at the point 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">=</mo>
<mn>.5</mn>
</mrow></math>

.
</li></ol>



<br/>
<br/>
<b><big><a name="Program" id="Program">Program</a></big></b>

<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;iostream&gt;</span>      <span style="font-style: italic"><span style="color: #9A1900">// standard input/output</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;vector&gt;</span>        <span style="font-style: italic"><span style="color: #9A1900">// standard vector</span></span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;cppad/cppad.hpp&gt;</span> <span style="font-style: italic"><span style="color: #9A1900">// the CppAD package http://www.coin-or.org/CppAD/</span></span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> <span style="color: #FF0000">{</span>
      <span style="font-style: italic"><span style="color: #9A1900">// define y(x) = Poly(a, x) in the empty namespace</span></span>
      <span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Type</span><span style="color: #990000">&gt;</span>
      <span style="color: #008080">Type</span> <span style="font-weight: bold"><span style="color: #000000">Poly</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;double&gt;</span> <span style="color: #990000">&amp;</span>a<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> <span style="color: #008080">Type</span> <span style="color: #990000">&amp;</span>x<span style="color: #990000">)</span>
      <span style="color: #FF0000">{</span>     <span style="color: #008080">size_t</span> k  <span style="color: #990000">=</span> a<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
            <span style="color: #008080">Type</span> y   <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">.;</span>  <span style="font-style: italic"><span style="color: #9A1900">// initialize summation</span></span>
            <span style="color: #008080">Type</span> x_i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>  <span style="font-style: italic"><span style="color: #9A1900">// initialize x^i</span></span>
            <span style="color: #008080">size_t</span> i<span style="color: #990000">;</span>
            <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> k<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
            <span style="color: #FF0000">{</span>     y   <span style="color: #990000">+=</span> a<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">*</span> x_i<span style="color: #990000">;</span>  <span style="font-style: italic"><span style="color: #9A1900">// y   = y + a_i * x^i</span></span>
                  x_i <span style="color: #990000">*=</span> x<span style="color: #990000">;</span>           <span style="font-style: italic"><span style="color: #9A1900">// x_i = x_i * x</span></span>
            <span style="color: #FF0000">}</span>
            <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> y<span style="color: #990000">;</span>
      <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>
<span style="font-style: italic"><span style="color: #9A1900">// main program</span></span>
<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #0000FF">using</span></span> CppAD<span style="color: #990000">::</span>AD<span style="color: #990000">;</span>           <span style="font-style: italic"><span style="color: #9A1900">// use AD as abbreviation for CppAD::AD</span></span>
      <span style="font-weight: bold"><span style="color: #0000FF">using</span></span> std<span style="color: #990000">::</span>vector<span style="color: #990000">;</span>         <span style="font-style: italic"><span style="color: #9A1900">// use vector as abbreviation for std::vector</span></span>
      <span style="color: #008080">size_t</span> i<span style="color: #990000">;</span>                  <span style="font-style: italic"><span style="color: #9A1900">// a temporary index</span></span>

      <span style="font-style: italic"><span style="color: #9A1900">// vector of polynomial coefficients</span></span>
      <span style="color: #008080">size_t</span> k <span style="color: #990000">=</span> <span style="color: #993399">5</span><span style="color: #990000">;</span>              <span style="font-style: italic"><span style="color: #9A1900">// number of polynomial coefficients</span></span>
      <span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">a</span></span><span style="color: #990000">(</span>k<span style="color: #990000">);</span>       <span style="font-style: italic"><span style="color: #9A1900">// vector of polynomial coefficients</span></span>
      <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span>i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> k<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
            a<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">.;</span>           <span style="font-style: italic"><span style="color: #9A1900">// value of polynomial coefficients</span></span>

      <span style="font-style: italic"><span style="color: #9A1900">// domain space vector</span></span>
      <span style="color: #008080">size_t</span> n <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>              <span style="font-style: italic"><span style="color: #9A1900">// number of domain space variables</span></span>
      <span style="color: #008080">vector&lt; AD&lt;double&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">X</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span> <span style="font-style: italic"><span style="color: #9A1900">// vector of domain space variables</span></span>
      X<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">3</span><span style="color: #990000">.;</span>                 <span style="font-style: italic"><span style="color: #9A1900">// value corresponding to operation sequence</span></span>

      <span style="font-style: italic"><span style="color: #9A1900">// declare independent variables and start recording operation sequence</span></span>
      CppAD<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">Independent</span></span><span style="color: #990000">(</span>X<span style="color: #990000">);</span>

      <span style="font-style: italic"><span style="color: #9A1900">// range space vector</span></span>
      <span style="color: #008080">size_t</span> m <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>              <span style="font-style: italic"><span style="color: #9A1900">// number of ranges space variables</span></span>
      <span style="color: #008080">vector&lt; AD&lt;double&gt; &gt;</span> <span style="font-weight: bold"><span style="color: #000000">Y</span></span><span style="color: #990000">(</span>m<span style="color: #990000">);</span> <span style="font-style: italic"><span style="color: #9A1900">// vector of ranges space variables</span></span>
      Y<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">Poly</span></span><span style="color: #990000">(</span>a<span style="color: #990000">,</span> X<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]);</span>      <span style="font-style: italic"><span style="color: #9A1900">// value during recording of operations</span></span>

      <span style="font-style: italic"><span style="color: #9A1900">// store operation sequence in f: X -&gt; Y and stop recording</span></span>
      CppAD<span style="color: #990000">::</span><span style="color: #008080">ADFun&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">f</span></span><span style="color: #990000">(</span>X<span style="color: #990000">,</span> Y<span style="color: #990000">);</span>

      <span style="font-style: italic"><span style="color: #9A1900">// compute derivative using operation sequence stored in f</span></span>
      <span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">jac</span></span><span style="color: #990000">(</span>m <span style="color: #990000">*</span> n<span style="color: #990000">);</span> <span style="font-style: italic"><span style="color: #9A1900">// Jacobian of f (m by n matrix)</span></span>
      <span style="color: #008080">vector&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">x</span></span><span style="color: #990000">(</span>n<span style="color: #990000">);</span>       <span style="font-style: italic"><span style="color: #9A1900">// domain space vector</span></span>
      x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">3</span><span style="color: #990000">.;</span>                 <span style="font-style: italic"><span style="color: #9A1900">// argument value for derivative</span></span>
      jac  <span style="color: #990000">=</span> f<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Jacobian</span></span><span style="color: #990000">(</span>x<span style="color: #990000">);</span>      <span style="font-style: italic"><span style="color: #9A1900">// Jacobian for operation sequence</span></span>

      <span style="font-style: italic"><span style="color: #9A1900">// print the results</span></span>
      std<span style="color: #990000">::</span>cout <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"f'(3) computed by CppAD = "</span> <span style="color: #990000">&lt;&lt;</span> jac<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">&lt;&lt;</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>

      <span style="font-style: italic"><span style="color: #9A1900">// check if the derivative is correct</span></span>
      <span style="color: #009900">int</span> error_code<span style="color: #990000">;</span>
      <span style="font-weight: bold"><span style="color: #0000FF">if</span></span><span style="color: #990000">(</span> jac<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">==</span> <span style="color: #993399">142</span><span style="color: #990000">.</span> <span style="color: #990000">)</span>
            error_code <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>      <span style="font-style: italic"><span style="color: #9A1900">// return code for correct case</span></span>
      <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>  error_code <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span>      <span style="font-style: italic"><span style="color: #9A1900">// return code for incorrect case</span></span>

      <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> error_code<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre>
<br/>
<b><big><a name="Output" id="Output">Output</a></big></b>
<br/>
Executing the program above will generate the following output:
<code><font color='blue'><pre style='display:inline'> 
     f'(3) computed by CppAD = 142
</pre></font></code>
<br/>
<b><big><a name="Running" id="Running">Running</a></big></b>
<br/>
To build and run this program see <a href="cmake_check.xml" target="_top"><span style='white-space: nowrap'>cmake_check</span></a>
.


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

</body>
</html>
