Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:51:24+00:00 2026-05-29T09:51:24+00:00

I want to use the newton function loaded as from scipy.optimize import newton in

  • 0

I want to use the newton function loaded as

from scipy.optimize import newton

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 help(newton) I saw which parameters takes the function and the relative explanation:

newton(func, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50)

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.

In which way I have to pass my function? If I use for func e.g. x**3 (and its first derivative) the response is NameError: name 'x' is not defined. 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

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)

But I get the output

    102         for iter in range(maxiter):
    103             myargs = (p0,) + args
--> 104             fder = fprime(*myargs)
    105             if fder == 0:
    106                 msg = "derivative was zero."

TypeError: DF() takes exactly 2 arguments (1 given)

and the same issue for F if I omit DF. Looking at the code in /usr/local/share/src/scipy/scipy/optimize/zeros.py I see that it evaluates the first derivative with fder=fprime(*myargs) so maybe I have to put in args something that make it working. I was thinking about it but no solution comes to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:51:25+00:00Added an answer on May 29, 2026 at 9:51 am

    First, be aware that using eval makes your program vulnerable to malicious users. If that concern does not apply, you can create F and DF like this:

    F = eval('lambda x :'+fie)
    DF = eval('lambda x :'+dfie)
    

    Then both functions expect only a single argument, and you can leave the args argument empty.

    EDIT. If you really want to stick to your code as closely as possible, this should also work, but it does not look very nice to me. newton will send the same args to both functions.

    def F(x,fie,dfie):
        y = eval(fie)
        return y 
    
    def DF(x,fie,dfie):
        dy = eval(dfie)
        return dy
    
    print newton(F,x0,DF,(fie,dfie))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use Haskell higher order function Foldr to calculate the length of a
i want use some data from a website with web service. i have a
So I want use Leaks to find the leaks in my app. In Xcode,
Example I want use the_excerpt in two locations. On functions.php I add function new_excerpt_length($length)
I want use localized strings from resources in xsl template as in aspx page,
I want use a query like such SELECT personId FROM Person p Inner Join
I want to use private drawable from android. for that i have downloade androi-8.jar
I want use ORDER BY RAND() query mysql. But I have some question want
I want use php curl with oauth to get the JSON data from twitter
I want use $.ajax to read some infomation from xml file,here is my js

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.