Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - Newton Method
    primarykey
    data
    text
    <p>I am new to Python and I need to do a Newton Method script.</p> <p>I have been trying to do it, but I keep getting errors or no returns...</p> <p>Here is the assignment:</p> <hr> <p>A function <code>newton(f, x, feps, maxit)</code> which takes:</p> <ul> <li>a function <code>f(x)</code>, </li> <li>an initial guess <code>x</code> for the root of the function f(x),</li> <li>an allowed tolerance <code>feps</code>,</li> <li>and the maximum number of iterations that are allowed <code>maxit</code>.</li> </ul> <p>The newton function should use the following Newton-Raphson algorithm:</p> <pre><code>while |f(x)| &gt; feps, do x = x - f(x) / fprime(x) </code></pre> <p>where <code>fprime(x)</code> is an approximation of the first derivative (df(x)/dx) at position <code>x</code>. You should use the derivative function from the training part of this lab.</p> <p>Make sure you copy the derivative function definition from training7.py into lab7.py (there are more elegant ways of doing this, but for the purpose of the assessment, this is the most straightforward way we recommend).</p> <p>If <code>maxit</code> or fewer iterations are necessary for |f(x)| to become smaller than <code>feps</code>, then the value for x should be returned:</p> <pre><code>In [ ]: def f(x): ....: return x ** 2 - 2 ....: In [ ]: newton(f, 1.0, 0.2, 15) Out[ ]: 1.4166666666783148 In [ ]: newton(f, 1.0, 0.2, 15) - math.sqrt(2) Out[ ]: 0.002453104305219611 In [ ]: newton(f, 1.0, 0.001, 15) Out[ ]: 1.4142156862748523 In [ ]: newton(f, 1.0, 0.001, 15) - math.sqrt(2) Out[ ]: 2.1239017571339502e-06 In [ ]: newton(f, 1.0, 0.000001, 15) - math.sqrt(2) Out[ ]: 1.5949463971764999e-12 </code></pre> <hr> <p>This is what I tried to do but it is totally wrong:</p> <pre><code>def derivative(f, x): """A function derivative(f, x) which computes a numerical approximation of the first derivative of the function f(x) using central differences.""" R = (f(x + (10 ** -6) / 2.0) - f(x - (10 ** -6) / 2.0)) / (10 ** -6) return R def newton(f, x, feps): """A function newton(f, x, feps, maxit) which takes a function f(x) and an initial guess x for the root of the function f(x), an allowed tolerance feps and the maximum number of iterations that are allowed maxit. The newton function should use the following Newton-Raphson algorithm: while |f(x)| &gt; feps, do x = x - f(x) / fprime(x) where fprime(x) is an approximation of the first derivative (df(x)/dx) at position x.""" while abs(f(x) &gt; feps): fprime(x) = derivative(f, x) Result = x - f(x) / fprime(x) return Result </code></pre> <p>What should I doto make it work?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload