<html>
<head>
<!-- Bootstrap style: vagrant 1.0 web pages -->

<!--
This style is adopted from the (now old) vagrant 1.0 web pages.

This style file should be copied and the following elements edited:

Logo heading:

 DiffEq
 101

Navigation links at the top:

 GO TO 1
 GO TO 2

Footer at the end
-->

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Doconce: http://code.google.com/p/doconce/" />

<title>Solve the world's simplest differential equation</title>

<!-- If you copy the css subdirectory (and, e.g., make optional edits):
<link rel="stylesheet" href="css/twitter_bootstrap.css">
<link rel="stylesheet" href="css/vagrant.css">
Otherwise, rely on the URLs below (vagrant.css adapted to Doconce layout)
-->

<link rel="stylesheet" href="https://raw.githubusercontent.com/hplgit/doconce/master/bundled/html_styles/style_vagrant/css/twitter_bootstrap.css">
<link rel="stylesheet" href="https://raw.githubusercontent.com/hplgit/doconce/master/bundled/html_styles/style_vagrant/css/vagrant.css">

<!-- Define color of headings here (last definition counts) -->
<style type="text/css">
h1, h2, h3, h4, h5, h6 {
  color: #000;     /* black */
  color: #999;     /* gray */
  color: #005580;  /* dark blue */
  color: #08c;     /* characteristic blue */
</style>
</head>
<body>

<div class="container">
 <div class="row Header with-border">
  <div class="span3 Module logo">
   <h1><a href="/">DiffEq<span class="subtitle">101</span></a></h1>
  </div>
  <div class="span9">
   <div class="Module navigation">
   <!-- Navigation at the top of the page -->
    <ul>
     <li> <a href="http://wikipedia.org">Wikipedia</a></li>
     <li> <a href="http://wolframalpha.com">WolframAlpha</a></li>
    </ul>
   </div>
  </div>
 </div>
</div>


<!-- Here goes the table of contents in the sidebar
     <li class="active"> means dark blue background for current section
-->
<div class="row">
 <div class="span3 Module sidebar">
  <div class="well" style="padding: 8px 0px;">
   <ul class="nav nav-list">
     <!-- navigation toc: --> <li><a href="#solve-the-world-s-simplest-differential-equation" style="font-size: 80%;"><b>Solve the world's simplest differential equation</b></a></li>
     <!-- navigation toc: --> <li><a href="#mathematical-problem" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Mathematical problem</a></li>
     <!-- navigation toc: --> <li><a href="#numerical-solution-method" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Numerical solution method</a></li>
     <!-- navigation toc: --> <li><a href="#implementation" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Implementation</a></li>
     <!-- navigation toc: --> <li><a href="#numerical-experiments" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Numerical experiments</a></li>

   </ul>
  </div>
 </div>

 <div class="span9">

<!-- tocinfo
{'highest level': 1,
 'sections': [("Solve the world's simplest differential equation",
               1,
               None,
               'solve-the-world-s-simplest-differential-equation'),
              ('Mathematical problem', 2, None, 'mathematical-problem'),
              ('Numerical solution method',
               2,
               None,
               'numerical-solution-method'),
              ('Implementation', 2, None, 'implementation'),
              ('Numerical experiments', 2, None, 'numerical-experiments')]}
end of tocinfo -->





<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: {
     equationNumbers: {  autoNumber: "AMS"  },
     extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
  }
});
</script>
<script type="text/javascript" async
 src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<!-- ------------------- main content ---------------------- -->

<br>
<h1 id="solve-the-world-s-simplest-differential-equation" class="anchor">Solve the world's simplest differential equation </h1>
<h2 id="mathematical-problem" class="anchor">Mathematical problem </h2>

<p>This exercise addresses the differential equation problem</p>

$$
\begin{align}
u'(t) &= -au(t), \quad t \in (0,T], \label{ode}\\
u(0)  &= I,                         \label{initial:value}
\end{align}
$$

<p>where \( a \), \( I \), and \( T \) are prescribed constant parameters, and \( u(t) \) is
the unknown function to be estimated. This mathematical model
is relevant for physical phenomena featuring exponential decay
in time.
</p>
<h2 id="numerical-solution-method" class="anchor">Numerical solution method </h2>

<p>Derive the \( \theta \)-rule scheme for solving \eqref{ode} numerically
with time step \( \Delta t \):
</p>

$$
u^{n+1} = \frac{1 - (1-\theta) a\Delta t}{1 + \theta a\Delta t}u^n,
$$

<p>Here, \( n=0,1,\ldots,N-1 \).</p>

<b>Hint.</b>

<p>Set up the Forward Euler, Backward Euler, and the Crank-Nicolson (or
Midpoint) schemes first. Then generalize to the \( \theta \)-rule above.
</p>


<h2 id="implementation" class="anchor">Implementation </h2>

<p>The numerical method is implemented in a Python function
<code>solver</code> (found in the <a href="https://github.com/doconce/INF5620/tree/gh-pages/src/decay/experiments/decay_mod.py" target="_self"><tt>decay_mod</tt></a> module):
</p>


<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="cell border-box-sizing code_cell rendered">
  <div class="input">
    <div class="inner_cell">
      <div class="input_area">
        <div class="highlight" style="background: #f8f8f8">
  <pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">import</span> linspace, zeros

<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">solver</span>(I, a, T, dt, theta):
    <span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;Solve u&#39;=-a*u, u(0)=I, for t in (0,T] with steps of dt.&quot;&quot;&quot;</span>
    dt <span style="color: #666666">=</span> <span style="color: #008000">float</span>(dt)           <span style="color: #408080; font-style: italic"># avoid integer division</span>
    N <span style="color: #666666">=</span> <span style="color: #008000">int</span>(<span style="color: #008000">round</span>(T<span style="color: #666666">/</span>dt))     <span style="color: #408080; font-style: italic"># no of time intervals</span>
    T <span style="color: #666666">=</span> N<span style="color: #666666">*</span>dt                 <span style="color: #408080; font-style: italic"># adjust T to fit time step dt</span>
    u <span style="color: #666666">=</span> zeros(N<span style="color: #666666">+1</span>)           <span style="color: #408080; font-style: italic"># array of u[n] values</span>
    t <span style="color: #666666">=</span> linspace(<span style="color: #666666">0</span>, T, N<span style="color: #666666">+1</span>)  <span style="color: #408080; font-style: italic"># time mesh</span>

    u[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> I                 <span style="color: #408080; font-style: italic"># assign initial condition</span>
    <span style="color: #008000; font-weight: bold">for</span> n <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">0</span>, N):    <span style="color: #408080; font-style: italic"># n=0,1,...,N-1</span>
        u[n<span style="color: #666666">+1</span>] <span style="color: #666666">=</span> (<span style="color: #666666">1</span> <span style="color: #666666">-</span> (<span style="color: #666666">1-</span>theta)<span style="color: #666666">*</span>a<span style="color: #666666">*</span>dt)<span style="color: #666666">/</span>(<span style="color: #666666">1</span> <span style="color: #666666">+</span> theta<span style="color: #666666">*</span>dt<span style="color: #666666">*</span>a)<span style="color: #666666">*</span>u[n]
    <span style="color: #008000; font-weight: bold">return</span> u, t
</pre>
</div>
      </div>
    </div>
  </div>
  <div class="output_wrapper">
    <div class="output">
      <div class="output_area">
        <div class="output_subarea output_stream output_stdout output_text">          
        </div>
      </div>
    </div>
  </div>
</div>
<h2 id="numerical-experiments" class="anchor">Numerical experiments </h2>

<p>Fix the values of where \( I \), \( a \), and \( T \).
Then vary \( \Delta t \) for \( \theta=0,1/2,1 \).
Illustrate that if \( \Delta t \) is not sufficiently small,
\( \theta=0 \) and \( \theta=1/2 \) can give non-physical solutions
(more precisely, oscillating solutions).
</p>

<p>Perform experiments and determine empirically the convergence
rate for \( \theta=0,1/2,1 \).
</p>
<!-- ------------------- end of main content --------------- -->

 </div>

 <div class="row Footer">
  <div class="span12">
  <!-- footer --> Here goes a footer, if desired, maybe with author(s) and a copyright &copy;
  </div>
 </div>
</div>
</body>
</html>