Note that there are some explanatory texts on larger screens.

plurals
  1. POHow use the newton function for root finding of the Scipy's optimize package
    primarykey
    data
    text
    <p>I want to use the <code>newton</code> function loaded as </p> <pre><code>from scipy.optimize import newton </code></pre> <p>in order to find the zeros of a function enetered by the user. I write a script that first ask to the user to specify a function together with its first derivative, and also the starting point of the algorithm. First of all typing <code>help(newton)</code> I saw which parameters takes the function and the relative explanation: </p> <p><code>newton(func, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50)</code></p> <pre><code>func : function The function whose zero is wanted. It must be a function of a single variable of the form f(x,a,b,c...), where a,b,c... are extra arguments that can be passed in the `args` parameter. </code></pre> <p>In which way I have to pass my function? If I use for func e.g. <code>x**3</code> (and its first derivative) the response is <code>NameError: name 'x' is not defined</code>. On the internet I find that first I have to define my function and its first derivative and pass the names as parameters. So I made the following</p> <pre><code>fie = raw_input('Enter function in terms of x (e.g. x**2 - 2*x). F= ') dfie = raw_input('Enter first derivative of function above DF = ') x0 = input('Enter starting point x0 = ') def F(x,fie): y = eval(fie) return y def DF(x, dfie): dy = eval(dfie) return dy print newton(F,x0,DF) </code></pre> <p>But I get the output </p> <pre><code> 102 for iter in range(maxiter): 103 myargs = (p0,) + args --&gt; 104 fder = fprime(*myargs) 105 if fder == 0: 106 msg = "derivative was zero." TypeError: DF() takes exactly 2 arguments (1 given) </code></pre> <p>and the same issue for <code>F</code> if I omit <code>DF</code>. Looking at the code in <code>/usr/local/share/src/scipy/scipy/optimize/zeros.py</code> I see that it evaluates the first derivative with <code>fder=fprime(*myargs)</code> so maybe I have to put in <code>args</code> something that make it working. I was thinking about it but no solution comes to me. </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.
    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