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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:14:41+00:00 2026-06-13T09:14:41+00:00

def evaluatePoly(poly, x): ”’ Computes the value of a polynomial function at given value

  • 0
def evaluatePoly(poly, x):
    '''
    Computes the value of a polynomial function at given value x. Returns that
    value as a float.

    poly: list of numbers, length > 0
    x: number
    returns: float
    '''
    for n in range(len(poly)):
        poly[n] = (poly[n]) * (x**n)

    return float(sum(poly[:]))

def computeDeriv(poly):
    '''
    Computes and returns the derivative of a polynomial function as a list of
    floats. If the derivative is 0, returns [0.0].

    poly: list of numbers, length > 0
    returns: list of numbers (floats)

    >>> print computeDeriv([-13.39, 0.0, 17.5, 3.0, 1.0])
    [0.0, 35.0, 9.0, 4.0]
    >>> print computeDeriv([6, 1, 3, 0])
    [1.0, 6.0, 0.0]
    >>> print computeDeriv([20])
    [0.0]

    '''
    if len(poly) == 1:
        poly = [0.0]
        return poly
    for m in range(len(poly)):
        poly[m] = float(m) * poly[m]
    return poly[1:]

def computeRoot(poly, x_0, epsilon):
    '''
    Uses Newton's method to find and return a root of a polynomial function.
    Returns a list containing the root and the number of iterations required
    to get to the root.

    poly: list of numbers, length > 1.
         Represents a polynomial function containing at least one real root.
         The derivative of this polynomial function at x_0 is not 0.
    x_0: float
    epsilon: float > 0
    returns: list [float, int]

    >>> print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1,  .0001)
    [0.806790753796352, 7]
    >>> print computeRoot([1, 9, 8], -3, .01)
    [-1.0000079170005467, 5]
    >>> print computeRoot([1, -1, 1, -1], 2, .001)
    [1.0002210630197605, 4]
    '''
    x = x_0
    iter = 0
    list = []
    polyStart = poly[:]
    while abs(evaluatePoly(poly, x)) >= epsilon:
        poly = polyStart[:]
        l = evaluatePoly(poly,x)
        if abs(l) < epsilon:
            list.append(x)
            list.append(iter)
            return list
        else:
            poly = polyStart[:]
            d = computeDeriv(poly)
            dn = evaluatePoly(d, x)
            x = (x - (l/dn))
            iter = iter + 1
  • 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-13T09:14:42+00:00Added an answer on June 13, 2026 at 9:14 am

    I assume you mean this function is returning None on some particular input:

    def computeRoot(poly, x_0, epsilon):
        x = x_0
        iter = 0
        list = []
        polyStart = poly[:]
        while abs(evaluatePoly(poly, x)) >= epsilon:
            poly = polyStart[:]
            l = evaluatePoly(poly,x)
            if abs(l) < epsilon:
                list.append(x)
                list.append(iter)
                return list
            else:
                poly = polyStart[:]
                d = computeDeriv(poly)
                dn = evaluatePoly(d, x)
                x = (x - (l/dn))
                iter = iter + 1
    

    How do I know? Because there’s only one return, and it’s not at the end of the function. If abs(evaluatePoly(poly, x)) >= epsilon is False, then the while loop will end, and there’s nothing after it, so the function ends and returns None by default (any function which doesn’t explicitly return returns None).

    So, you need to figure out what the correct return value is in that situation and add a return statement and the end of the function.

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

Sidebar

Related Questions

def divideset(rows, column, value) split_function = nil if value.is_a?(Fixnum) || value.is_a?(Float) split_function = lambda{|row|
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def method(self, *args): def function(*args): #can this access all method's variables, data, etc.
def valueize(val) # randomly returns nil or random integer ([-100 to +100] + val)
def function(message): if 'START' in message: timer = 10 #start a thread which sleeps
def makecounter(): return collections.defaultdict(int) class RankedIndex(object): def __init__(self): self._inverted_index = collections.defaultdict(list) self._documents = []
def applejuice(q): print THE FUNCTION NAME! It should result in applejuice as a string.
def plural(value, string) #{value} #{value.abs == 1 ? string.singularize : string.pluralize} end If not,
def remove_whitespaces(value): Remove all whitespaces p = re.compile(r'\s+') return p.sub(' ', value) The above
def de_punctuate(xs): return re.findall(r[\w']+(?:-[\w']+)*, xs) numbers = partial(re.search, r'[0,9]') def no_numbers(xs): return filterfalse(numbers, xs)

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.