
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />

    <title>Case Study: Numerical Analysis inside a Fit &#8212; lsqfit 13.0.1 documentation</title>
    <link rel="stylesheet" type="text/css" href="_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="_static/pyramid.css" />
    <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
    <script src="_static/jquery.js"></script>
    <script src="_static/underscore.js"></script>
    <script src="_static/_sphinx_javascript_frameworks_compat.js"></script>
    <script src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Case Study: Fitting a Spline" href="case-spline.html" />
    <link rel="prev" title="Case Study: Simple Extrapolation" href="case-extrapolation.html" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Neuton&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->

  </head><body>

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="case-spline.html" title="Case Study: Fitting a Spline"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="case-extrapolation.html" title="Case Study: Simple Extrapolation"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">lsqfit 13.0.1 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">Case Study: Numerical Analysis inside a Fit</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="case-study-numerical-analysis-inside-a-fit">
<h1>Case Study: Numerical Analysis inside a Fit<a class="headerlink" href="#case-study-numerical-analysis-inside-a-fit" title="Permalink to this heading">¶</a></h1>
<p>This case study shows how to fit a differential equation,
using <code class="xref py py-mod docutils literal notranslate"><span class="pre">gvar.ode</span></code>, and how to deal with uncertainty in
the independent variable of a fit (that is, the <code class="docutils literal notranslate"><span class="pre">x</span></code> in
a <code class="docutils literal notranslate"><span class="pre">y</span></code> versus <code class="docutils literal notranslate"><span class="pre">x</span></code> fit).</p>
<section id="the-problem">
<h2>The Problem<a class="headerlink" href="#the-problem" title="Permalink to this heading">¶</a></h2>
<p>A pendulum is released at time 0 from angle 1.571(50) (radians). It’s
angular position is measured at intervals of approximately a tenth of
second:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">t</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>       <span class="n">theta</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<span class="o">----------------------</span>
<span class="mf">0.0</span>          <span class="mf">1.571</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="mf">0.10</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>      <span class="mf">1.477</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.20</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>      <span class="mf">0.791</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.30</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>     <span class="o">-</span><span class="mf">0.046</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.40</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>     <span class="o">-</span><span class="mf">0.852</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.50</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>     <span class="o">-</span><span class="mf">1.523</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.60</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>     <span class="o">-</span><span class="mf">1.647</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.70</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>     <span class="o">-</span><span class="mf">1.216</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.80</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>     <span class="o">-</span><span class="mf">0.810</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">0.90</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>      <span class="mf">0.185</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
<span class="mf">1.00</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>      <span class="mf">0.832</span><span class="p">(</span><span class="mi">79</span><span class="p">)</span>
</pre></div>
</div>
<p>Function <code class="docutils literal notranslate"><span class="pre">theta(t)</span></code> satisfies a differential equation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">d</span><span class="o">/</span><span class="n">dt</span> <span class="n">d</span><span class="o">/</span><span class="n">dt</span> <span class="n">theta</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>  <span class="o">=</span>  <span class="o">-</span><span class="p">(</span><span class="n">g</span><span class="o">/</span><span class="n">l</span><span class="p">)</span> <span class="n">sin</span><span class="p">(</span><span class="n">theta</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
</pre></div>
</div>
<p>where <code class="docutils literal notranslate"><span class="pre">g</span></code> is the acceleration due to gravity and <code class="docutils literal notranslate"><span class="pre">l</span></code> is
the pendulum’s length. The challenge is to use the data to improve
our very approximate <em>a priori</em> estimate 40±20 for <code class="docutils literal notranslate"><span class="pre">g/l</span></code>.</p>
</section>
<section id="pendulum-dynamics">
<h2>Pendulum Dynamics<a class="headerlink" href="#pendulum-dynamics" title="Permalink to this heading">¶</a></h2>
<p>We start by designing a data type that solves the differential
equation for <code class="docutils literal notranslate"><span class="pre">theta(t)</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">gvar</span> <span class="k">as</span> <span class="nn">gv</span>

<span class="k">class</span> <span class="nc">Pendulum</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Integrator for pendulum motion.</span>

<span class="sd">    Input parameters are:</span>
<span class="sd">        g/l .... where g is acceleration due to gravity and l the length</span>
<span class="sd">        tol .... precision of numerical integration of ODE</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">g_l</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">g_l</span> <span class="o">=</span> <span class="n">g_l</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">odeint</span> <span class="o">=</span> <span class="n">gv</span><span class="o">.</span><span class="n">ode</span><span class="o">.</span><span class="n">Integrator</span><span class="p">(</span><span class="n">deriv</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">deriv</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="n">tol</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">theta0</span><span class="p">,</span> <span class="n">t_array</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Calculate pendulum angle theta for every t in t_array.</span>

<span class="sd">        Assumes that the pendulum is released at time t=0</span>
<span class="sd">        from angle theta0 with no initial velocity. Returns</span>
<span class="sd">        an array containing theta(t) for every t in t_array.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># initial values</span>
        <span class="n">t0</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">y0</span> <span class="o">=</span> <span class="p">[</span><span class="n">theta0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">]</span>              <span class="c1"># theta and dtheta/dt</span>

        <span class="c1"># solution  (keep only theta; discard dtheta/dt)</span>
        <span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">odeint</span><span class="o">.</span><span class="n">solution</span><span class="p">(</span><span class="n">t0</span><span class="p">,</span> <span class="n">y0</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">y</span><span class="p">(</span><span class="n">t</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">t_array</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">deriv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="s2">&quot; Calculate [dtheta/dt, d2theta/dt2] from [theta, dtheta/dt].&quot;</span>
        <span class="n">theta</span><span class="p">,</span> <span class="n">dtheta_dt</span> <span class="o">=</span> <span class="n">y</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">dtheta_dt</span><span class="p">,</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_l</span> <span class="o">*</span> <span class="n">gv</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">theta</span><span class="p">)])</span>
</pre></div>
</div>
<p>A <code class="docutils literal notranslate"><span class="pre">Pendulum</span></code> object is initialized with a value for <code class="docutils literal notranslate"><span class="pre">g/l</span></code> and a tolerance
for the differential-equation integrator, <code class="xref py py-class docutils literal notranslate"><span class="pre">gvar.ode.Integrator</span></code>.
Evaluating the object for a given value of <code class="docutils literal notranslate"><span class="pre">theta(0)</span></code> and <code class="docutils literal notranslate"><span class="pre">t</span></code> then
calculates <code class="docutils literal notranslate"><span class="pre">theta(t)</span></code>; <code class="docutils literal notranslate"><span class="pre">t</span></code> is an array. We use <code class="xref py py-mod docutils literal notranslate"><span class="pre">gvar.ode</span></code> here,
rather than some other integrator, because it works with <code class="xref py py-class docutils literal notranslate"><span class="pre">gvar.GVar</span></code>s,
allowing errors to propagate through the integration.</p>
</section>
<section id="two-types-of-input-data">
<h2>Two Types of Input Data<a class="headerlink" href="#two-types-of-input-data" title="Permalink to this heading">¶</a></h2>
<p>There are two ways to include data in a fit: either as
regular data, or as fit parameters with priors. In general dependent
variables are treated as regular data, and independent variables with
errors are treated as fit parameters, with priors. Here the dependent
variable is <code class="docutils literal notranslate"><span class="pre">theta(t)</span></code> and the independent variable is <code class="docutils literal notranslate"><span class="pre">t</span></code>. The
independent variable has uncertainties, so we treat the individual
values as fit parameters whose priors equal the initial values <code class="docutils literal notranslate"><span class="pre">t[i]</span></code>.
The value of <code class="docutils literal notranslate"><span class="pre">theta(t=0)</span></code> is also independent data, and so becomes
a fit parameter since it is uncertain. Our fit code therefore
is:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>   <span class="c1"># makes this work for python2 and 3</span>

<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">gvar</span> <span class="k">as</span> <span class="nn">gv</span>
<span class="kn">import</span> <span class="nn">lsqfit</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="c1"># pendulum data exhibits experimental error in theta and t</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">gv</span><span class="o">.</span><span class="n">gvar</span><span class="p">([</span>
        <span class="s1">&#39;0.10(1)&#39;</span><span class="p">,</span> <span class="s1">&#39;0.20(1)&#39;</span><span class="p">,</span> <span class="s1">&#39;0.30(1)&#39;</span><span class="p">,</span> <span class="s1">&#39;0.40(1)&#39;</span><span class="p">,</span>  <span class="s1">&#39;0.50(1)&#39;</span><span class="p">,</span>
        <span class="s1">&#39;0.60(1)&#39;</span><span class="p">,</span>  <span class="s1">&#39;0.70(1)&#39;</span><span class="p">,</span>  <span class="s1">&#39;0.80(1)&#39;</span><span class="p">,</span>  <span class="s1">&#39;0.90(1)&#39;</span><span class="p">,</span> <span class="s1">&#39;1.00(1)&#39;</span>
        <span class="p">])</span>
    <span class="n">theta</span> <span class="o">=</span> <span class="n">gv</span><span class="o">.</span><span class="n">gvar</span><span class="p">([</span>
        <span class="s1">&#39;1.477(79)&#39;</span><span class="p">,</span> <span class="s1">&#39;0.791(79)&#39;</span><span class="p">,</span> <span class="s1">&#39;-0.046(79)&#39;</span><span class="p">,</span> <span class="s1">&#39;-0.852(79)&#39;</span><span class="p">,</span>
        <span class="s1">&#39;-1.523(79)&#39;</span><span class="p">,</span> <span class="s1">&#39;-1.647(79)&#39;</span><span class="p">,</span> <span class="s1">&#39;-1.216(79)&#39;</span><span class="p">,</span> <span class="s1">&#39;-0.810(79)&#39;</span><span class="p">,</span>
        <span class="s1">&#39;0.185(79)&#39;</span><span class="p">,</span> <span class="s1">&#39;0.832(79)&#39;</span>
        <span class="p">])</span>

    <span class="c1"># priors for all fit parameters: g/l, theta(0), and t[i]</span>
    <span class="n">prior</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">()</span>
    <span class="n">prior</span><span class="p">[</span><span class="s1">&#39;g/l&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">gv</span><span class="o">.</span><span class="n">gvar</span><span class="p">(</span><span class="s1">&#39;40(20)&#39;</span><span class="p">)</span>
    <span class="n">prior</span><span class="p">[</span><span class="s1">&#39;theta(0)&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">gv</span><span class="o">.</span><span class="n">gvar</span><span class="p">(</span><span class="s1">&#39;1.571(50)&#39;</span><span class="p">)</span>
    <span class="n">prior</span><span class="p">[</span><span class="s1">&#39;t&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">t</span>

    <span class="c1"># fit function: use class Pendulum object to integrate pendulum motion</span>
    <span class="k">def</span> <span class="nf">fitfcn</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">t</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">t</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">t</span> <span class="o">=</span> <span class="n">p</span><span class="p">[</span><span class="s1">&#39;t&#39;</span><span class="p">]</span>
        <span class="n">pendulum</span> <span class="o">=</span> <span class="n">Pendulum</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="s1">&#39;g/l&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">pendulum</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="s1">&#39;theta(0)&#39;</span><span class="p">],</span> <span class="n">t</span><span class="p">)</span>

    <span class="c1"># do the fit and print results</span>
    <span class="n">fit</span> <span class="o">=</span> <span class="n">lsqfit</span><span class="o">.</span><span class="n">nonlinear_fit</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">theta</span><span class="p">,</span> <span class="n">prior</span><span class="o">=</span><span class="n">prior</span><span class="p">,</span> <span class="n">fcn</span><span class="o">=</span><span class="n">fitfcn</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">fit</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">maxline</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
</pre></div>
</div>
<p>The prior is a dictionary containing <em>a priori</em> estimates  for every fit
parameter. The fit parameters are varied to give the best fit
to both the data and the priors. The fit function uses a <code class="xref py py-class docutils literal notranslate"><span class="pre">Pendulum</span></code> object
to  integrate the differential equation for <code class="docutils literal notranslate"><span class="pre">theta(t)</span></code>, generating values
for each value of <code class="docutils literal notranslate"><span class="pre">t[i]</span></code> given a value for <code class="docutils literal notranslate"><span class="pre">theta(0)</span></code>.
The function returns an array that has the same shape as array <code class="docutils literal notranslate"><span class="pre">theta</span></code>.</p>
<p>The fit is excellent with a <code class="docutils literal notranslate"><span class="pre">chi**2</span></code> per degree of freedom of 0.7:</p>
<a class="reference internal image-reference" href="_images/case-pendulum.png"><img alt="_images/case-pendulum.png" src="_images/case-pendulum.png" style="width: 80%;" /></a>
<p>The red band in the figure shows the best fit to the data, with the
error bars on the fit. The output from this fit is:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Least</span> <span class="n">Square</span> <span class="n">Fit</span><span class="p">:</span>
  <span class="n">chi2</span><span class="o">/</span><span class="n">dof</span> <span class="p">[</span><span class="n">dof</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.7</span> <span class="p">[</span><span class="mi">10</span><span class="p">]</span>    <span class="n">Q</span> <span class="o">=</span> <span class="mf">0.73</span>    <span class="n">logGBF</span> <span class="o">=</span> <span class="mf">6.359</span>

<span class="n">Parameters</span><span class="p">:</span>
            <span class="n">g</span><span class="o">/</span><span class="n">l</span>    <span class="mf">39.82</span> <span class="p">(</span><span class="mi">87</span><span class="p">)</span>     <span class="p">[</span>    <span class="mi">40</span> <span class="p">(</span><span class="mi">20</span><span class="p">)</span> <span class="p">]</span>  
       <span class="n">theta</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>    <span class="mf">1.595</span> <span class="p">(</span><span class="mi">32</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">1.571</span> <span class="p">(</span><span class="mi">50</span><span class="p">)</span> <span class="p">]</span>  
            <span class="n">t</span> <span class="mi">0</span>   <span class="mf">0.0960</span> <span class="p">(</span><span class="mi">91</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.100</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">1</span>   <span class="mf">0.2014</span> <span class="p">(</span><span class="mi">74</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.200</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">2</span>   <span class="mf">0.3003</span> <span class="p">(</span><span class="mi">67</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.300</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">3</span>   <span class="mf">0.3982</span> <span class="p">(</span><span class="mi">76</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.400</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">4</span>   <span class="mf">0.5043</span> <span class="p">(</span><span class="mi">93</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.500</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">5</span>    <span class="mf">0.600</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.600</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">6</span>   <span class="mf">0.7079</span> <span class="p">(</span><span class="mi">89</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.700</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">7</span>   <span class="mf">0.7958</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.800</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">8</span>   <span class="mf">0.9039</span> <span class="p">(</span><span class="mi">78</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">0.900</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  
              <span class="mi">9</span>   <span class="mf">0.9929</span> <span class="p">(</span><span class="mi">83</span><span class="p">)</span>     <span class="p">[</span> <span class="mf">1.000</span> <span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">]</span>  

<span class="n">Fit</span><span class="p">:</span>
      <span class="n">key</span>         <span class="n">y</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>      <span class="n">f</span><span class="p">(</span><span class="n">p</span><span class="p">)[</span><span class="n">key</span><span class="p">]</span>
<span class="o">---------------------------------------</span>
        <span class="mi">0</span>     <span class="mf">1.477</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>     <span class="mf">1.412</span> <span class="p">(</span><span class="mi">42</span><span class="p">)</span>  
        <span class="mi">1</span>     <span class="mf">0.791</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>     <span class="mf">0.802</span> <span class="p">(</span><span class="mi">56</span><span class="p">)</span>  
        <span class="mi">2</span>    <span class="o">-</span><span class="mf">0.046</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>    <span class="o">-</span><span class="mf">0.044</span> <span class="p">(</span><span class="mi">60</span><span class="p">)</span>  
        <span class="mi">3</span>    <span class="o">-</span><span class="mf">0.852</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>    <span class="o">-</span><span class="mf">0.867</span> <span class="p">(</span><span class="mi">56</span><span class="p">)</span>  
        <span class="mi">4</span>    <span class="o">-</span><span class="mf">1.523</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>    <span class="o">-</span><span class="mf">1.446</span> <span class="p">(</span><span class="mi">42</span><span class="p">)</span>  
        <span class="mi">5</span>    <span class="o">-</span><span class="mf">1.647</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>    <span class="o">-</span><span class="mf">1.594</span> <span class="p">(</span><span class="mi">32</span><span class="p">)</span>  
        <span class="mi">6</span>    <span class="o">-</span><span class="mf">1.216</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>    <span class="o">-</span><span class="mf">1.323</span> <span class="p">(</span><span class="mi">49</span><span class="p">)</span>  <span class="o">*</span>
        <span class="mi">7</span>    <span class="o">-</span><span class="mf">0.810</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>    <span class="o">-</span><span class="mf">0.776</span> <span class="p">(</span><span class="mi">61</span><span class="p">)</span>  
        <span class="mi">8</span>     <span class="mf">0.185</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>     <span class="mf">0.158</span> <span class="p">(</span><span class="mi">66</span><span class="p">)</span>  
        <span class="mi">9</span>     <span class="mf">0.832</span> <span class="p">(</span><span class="mi">79</span><span class="p">)</span>     <span class="mf">0.894</span> <span class="p">(</span><span class="mi">63</span><span class="p">)</span>  

<span class="n">Settings</span><span class="p">:</span>
  <span class="n">svdcut</span><span class="o">/</span><span class="n">n</span> <span class="o">=</span> <span class="mf">1e-12</span><span class="o">/</span><span class="mi">0</span>    <span class="n">tol</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1e-08</span><span class="o">*</span><span class="p">,</span><span class="mf">1e-10</span><span class="p">,</span><span class="mf">1e-10</span><span class="p">)</span>    <span class="p">(</span><span class="n">itns</span><span class="o">/</span><span class="n">time</span> <span class="o">=</span> <span class="mi">7</span><span class="o">/</span><span class="mf">0.1</span><span class="p">)</span>

</pre></div>
</div>
<p>The final result for <code class="docutils literal notranslate"><span class="pre">g/l</span></code> is 39.8(9), which is accurate to about 2%.
Note that the fit generates (slightly) improved estimates for several of
the <code class="docutils literal notranslate"><span class="pre">t</span></code> values and for <code class="docutils literal notranslate"><span class="pre">theta(0)</span></code>.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3><a href="index.html">Table of Contents</a></h3>
    <ul>
<li><a class="reference internal" href="#">Case Study: Numerical Analysis inside a Fit</a><ul>
<li><a class="reference internal" href="#the-problem">The Problem</a></li>
<li><a class="reference internal" href="#pendulum-dynamics">Pendulum Dynamics</a></li>
<li><a class="reference internal" href="#two-types-of-input-data">Two Types of Input Data</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="case-extrapolation.html"
                          title="previous chapter">Case Study: Simple Extrapolation</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="case-spline.html"
                          title="next chapter">Case Study: Fitting a Spline</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/case-pendulum.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="case-spline.html" title="Case Study: Fitting a Spline"
             >next</a> |</li>
        <li class="right" >
          <a href="case-extrapolation.html" title="Case Study: Simple Extrapolation"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">lsqfit 13.0.1 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">Case Study: Numerical Analysis inside a Fit</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2009-2019, G. P. Lepage.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.0.2.
    </div>
  </body>
</html>