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

  • Home
  • SEARCH
  • 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 6327207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:14:10+00:00 2026-05-24T17:14:10+00:00

I have got this code to solve Newton’s method for a given polynomial and

  • 0

I have got this code to solve Newton’s method for a given polynomial and initial guess value. I want to turn into an iterative process which Newton’s method actually is. The program should keeping running till the output value “x_n” becomes constant. And that final value of x_n is the actual root. Also, while using this method in my algorithm it should always produce a positive root between 0 and 1. So does converting the negative output (root) into a positive number would make any difference? Thank you.

import copy

poly = [[-0.25,3], [0.375,2], [-0.375,1], [-3.1,0]]

def poly_diff(poly):
    """ Differentiate a polynomial. """

    newlist = copy.deepcopy(poly)

    for term in newlist:
        term[0] *= term[1]
        term[1] -= 1

    return newlist

def poly_apply(poly, x):
    """ Apply a value to a polynomial. """

    sum = 0.0 

    for term in poly:
        sum += term[0] * (x ** term[1])

    return sum

def poly_root(poly):
    """ Returns a root of the polynomial"""

    poly_d = poly_diff(poly)
    x = float(raw_input("Enter initial guess:"))

    x_n = x - (float(poly_apply(poly, x)) / poly_apply(poly_d, x))

    print x_n

if __name__ == "__main__" :
    poly_root(poly)
  • 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-24T17:14:11+00:00Added an answer on May 24, 2026 at 5:14 pm

    First, in poly_diff, you should check to see if the exponent is zero, and if so simply remove that term from the result. Otherwise you will end up with the derivative being undefined at zero.

    def poly_root(poly):
        """ Returns a root of the polynomial"""
        poly_d = poly_diff(poly)
        x = None
        x_n = float(raw_input("Enter initial guess:"))
        while x != x_n:
            x = x_n
            x_n = x - (float(poly_apply(poly, x)) / poly_apply(poly_d, x))
        return x_n
    

    That should do it. However, I think it is possible that for certain polynomials this may not terminate, due to floating point rounding error. It may end up in a repeating cycle of approximations that differ only in the least significant bits. You might terminate when the percentage of change reaches a lower limit, or after a number of iterations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got this code and I am trying to understand the convention followed,
I have got some javascript code and I'd like to convert this to C#.
I have got a piece of code, that should countdown some number (in this
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
I have all written code and i want to this code standarise by php
i Have been given this simple task , I want to connect to facebook
I have got this error when using Enterprise Library 3.1 May 2007 version. We
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
So I've got this UpdatePanel. Inside it I have a nifty little jQuery scroll

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.