<h1><aclass="reference external"href="https://en.wikipedia.org/wiki/Newton%27s_method">Newton’s method</a><aclass="headerlink"href="#newton-s-method"title="Permalink to this headline">¶</a></h1>
<p>Newton-Raphson for finding the root of an equation.</p>
<p>Let’s use the Newton-Raphson method for finding the root of an equation
to write a function that can compute the square root of a number.</p>
<p>Cf. <aclass="reference external"href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
<h2>A Generator for Approximations<aclass="headerlink"href="#a-generator-for-approximations"title="Permalink to this headline">¶</a></h2>
<p>In <aclass="reference internal"href="Generator Programs.html"><spanclass="doc">Using x to Generate Values</span></a> we derive a function (called <codeclass="docutils literal notranslate"><spanclass="pre">make_generator</span></code> in the dictionary) that accepts an initial value and a quoted program and returns a new quoted program that, when driven by the <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> combinator (<aclass="reference internal"href="../library.html#joy.library.x"title="joy.library.x"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">joy.library.x()</span></code></a>), acts like a lazy stream.</p>
<p>To make a generator that generates successive approximations let’s start by assuming an initial approximation and then derive the function that computes the next approximation:</p>
<p>To make a generator that generates successive approximations let’s start
by assuming an initial approximation and then derive the function that
<h3>A Function to Compute the Next Approximation<aclass="headerlink"href="#a-function-to-compute-the-next-approximation"title="Permalink to this headline">¶</a></h3>
<p>Looking at the equation again:</p>
<p>This is the equation for computing the next approximate value of the
<p>With <codeclass="docutils literal notranslate"><spanclass="pre">n</span></code> as part of the function <codeclass="docutils literal notranslate"><spanclass="pre">F</span></code>, but <codeclass="docutils literal notranslate"><spanclass="pre">n</span></code> is the input to the <codeclass="docutils literal notranslate"><spanclass="pre">sqrt</span></code> function we’re writing. If we let 1 be the initial approximation:</p>
<p>With n as part of the function F, but n is the input to the sqrt
function we’re writing. If we let 1 be the initial approximation:</p>
<h3>A Generator of Square Root Approximations<aclass="headerlink"href="#a-generator-of-square-root-approximations"title="Permalink to this headline">¶</a></h3>
<h2>Finding Consecutive Approximations <codeclass="docutils literal notranslate"><spanclass="pre">within</span></code> a Tolerance<aclass="headerlink"href="#finding-consecutive-approximations-within-a-tolerance"title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>The remainder of a square root finder is a function <em>within</em>, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance.</div></blockquote>
<h2>Finding Consecutive Approximations within a Tolerance<aclass="headerlink"href="#finding-consecutive-approximations-within-a-tolerance"title="Permalink to this headline">¶</a></h2>
<p>From <aclass="reference external"href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
Hughes</a></p>
Hughes</a>:</p>
<blockquote>
<div>The remainder of a square root finder is a function <em>within</em>, which
takes a tolerance and a list of approximations and looks down the
list for two successive approximations that differ by no more than
the given tolerance.</div></blockquote>
<p>(And note that by “list” he means a lazily-evaluated list.)</p>
<p>Using the <em>output</em><codeclass="docutils literal notranslate"><spanclass="pre">[a</span><spanclass="pre">G]</span></code> of the above <aclass="reference internal"href="Generator Programs.html"><spanclass="doc">generator</span></a> for square root approximations, and further assuming that the first term <codeclass="docutils literal notranslate"><spanclass="pre">a</span></code> has been generated already and epsilon <codeclass="docutils literal notranslate"><spanclass="pre">ε</span></code> is handy on the stack…</p>
<p>Using the <em>output</em><codeclass="docutils literal notranslate"><spanclass="pre">[a</span><spanclass="pre">G]</span></code> of the above generator for square root
approximations, and further assuming that the first term a has been
generated already and epsilon ε is handy on the stack…</p>
<li>Use <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> combinator to generate next term from <codeclass="docutils literal notranslate"><spanclass="pre">G</span></code>.</li>
<li>Run <codeclass="docutils literal notranslate"><spanclass="pre">within</span></code> with <codeclass="docutils literal notranslate"><spanclass="pre">i</span></code> (it is a <codeclass="docutils literal notranslate"><spanclass="pre">primrec</span></code> function.)</li>
<h3>Setting up<aclass="headerlink"href="#setting-up"title="Permalink to this headline">¶</a></h3>
<p>The recursive function we have defined so far needs a slight preamble:<codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> to prime the generator and the epsilon value to use:</p>
<p>The recursive function we have defined so far needs a slight preamble:
<codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> to prime the generator and the epsilon value to use:</p>
<h3><codeclass="docutils literal notranslate"><spanclass="pre">within</span></code><aclass="headerlink"href="#within"title="Permalink to this headline">¶</a></h3>
<li><aclass="reference internal"href="#a-generator-for-approximations">A Generator for Approximations</a><ul>
<li><aclass="reference internal"href="#a-function-to-compute-the-next-approximation">A Function to Compute the Next Approximation</a></li>
<li><aclass="reference internal"href="#make-it-into-a-generator">Make it into a Generator</a></li>
<li><aclass="reference internal"href="#a-generator-of-square-root-approximations">A Generator of Square Root Approximations</a></li>
</ul>
</li>
<li><aclass="reference internal"href="#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations <codeclass="docutils literal notranslate"><spanclass="pre">within</span></code> a Tolerance</a><ul>
<li><aclass="reference internal"href="#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations within a Tolerance</a><ul>
<liclass="toctree-l2"><aclass="reference internal"href="Newton-Raphson.html#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations within a Tolerance</a></li>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.