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 7789771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:23:58+00:00 2026-06-01T21:23:58+00:00

My program finds the roots using newton’s algorithm. I am having trouble with a

  • 0

My program finds the roots using newton’s algorithm. I am having trouble with a last part when if there is not enough iterations to find the root to print that the root has not been found.

for i in range(N):
    f= evaluate(p,deg,x0)
    s=evaluate(d,deg-1,x0)
    if s==0:
        print "Can't divide by 0"
        return -1
    x1=x0 - f/s
    print ("Iteration %d" %(i+1))
    print "%f" %x0
    if abs(x1-x0)<tol:
        print "Found a root %f" %x1
        return 0
    else:
        x0=x1
    if abs(x1-x0)>tol:
       print "root not found"

somehow it seems to skip the last if statement and doesn’t print anything, i tried to put it in different places. when i placed it before the previous if-statement then it skips the x0=x1 part. Im confused of whats wrong with it.

N is the number of iterations, x0 is the initial guess

  • 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-06-01T21:23:59+00:00Added an answer on June 1, 2026 at 9:23 pm

    The logic is not correct for displaying that a root is not found. You do not want to check that abs(x0 - x1) > tol, because this is not relevant for finding a root. Think about it: the difference could be very large between x0 and x1 but you could still be on the right track to finding the root. You wouldn’t want to jump out of the iterations just because x1 is different than x0 on some iteration.

    A better thing would simply be to place the error statement outside of the for loop, such as:

    for i in range(N):
        f = evaluate(p,deg,x0)
        s = evaluate(d,deg-1,x0)
    
        if s==0:
            print "Can't divide by 0"
            return -1
        x1=x0 - f/s
        print ("Iteration %d" %(i+1))
        print "%f" %x0
        if abs(x1-x0)<tol:
            print "Found a root %f" %x1
            return 0
        else:
            x0=x1
    
    # If we exhaust the for-loop without returning due to
    # a found root, then there must have been an error with
    # convergence, so just print that at exit.
    print "Error: did not converge to the root in %d iterations."%(N)
    print "Check your initial guess and check your functions for cyclic points."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm basically trying to play with recursions and created a small program that finds
I'm writing a java program that goes out and searches twitter, finds certain instagram
I want to write a program to find the n-th smallest element without using
I'm working on a program using doubly linked lists. I have already gotten the
Is there a good tutorial for Creating a database using msbuild ? jean paul
I'm making a program that is using Windows Shell Integration, and the registry changes
I primarily program in Linux, using tcsh shell. By default, my current directory is
I'm a first year computer science student having a problem with part of an
I'm working on project. One part of it is read given folders files. Program
I'm trying to convert a basic tkinter GUI program to an .exe using py2exe.

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.