Learn how to a build a cloud-first strategyRegister Now

x
Solved

# could you give an example calling the 3 functions.

Posted on 2014-08-04
Medium Priority
303 Views
Last Modified: 2014-08-06
``````# 6.00 Problem Set 2
#
# Successive Approximation: Newton's Method
#
# Name          : Solutions
# Collaborators : <your collaborators>
# Time spent    : <total time>
#

def evaluate_poly(poly, x):
"""
Computes the value of a polynomial function at given value x. Returns that value.

Example:
>>> poly = [0.0, 0.0, 5.0, 9.3, 7.0]    # f(x) = 5x^2 + 9.3x^3 + 7x^4
>>> x = -13
>>> print evaluate_poly(poly, x)  # f(-13) = 5(-13)^2 + 9.3(-13)^3 + 7(-13)^4
180339.9

poly: list of numbers, length > 0
x: number
returns: float
"""
total = 0.0
for i in xrange(len(poly)):
total += poly[i] * (x ** i)
return total

def compute_deriv(poly):
"""
Computes and returns the derivative of a polynomial function. If the
derivative is 0, returns [0.0].

Example:
>>> poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
>>> print compute_deriv(poly)        # 35^x + 9x^2 + 4x^3
[0.0, 35.0, 9.0, 4.0]

poly: list of numbers, length > 0
returns: list of numbers
"""
poly_deriv = []
if len(poly) < 2:
return [0.0]
for j in xrange(1, len(poly)):
poly_deriv.append(float(j * poly[j]))
return poly_deriv

def compute_root(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.

Example:
>>> poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
>>> x_0 = 0.1
>>> epsilon = .0001
>>> print compute_root(poly, x_0, epsilon)
[0.80679075379635201, 8]

poly: list of numbers, length > 1.
Represents a polynomialfunction 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]
"""
root = x_0
counter = 1
while abs(evaluate_poly(poly, root)) >= epsilon:
root = (root - evaluate_poly(poly, root) /
evaluate_poly(compute_deriv(poly), root))
counter += 1
return [root, counter]
``````

how is this function called

note: this is an old class from mit in 2011
0
Question by:rgb192
• 2
• 2
4 Comments

LVL 27

Expert Comment

ID: 40241653
Have you installed the Python and the Idle GUI on your computer yet?

The Problem Set includes example calls and results for each function:

def evaluate_poly(poly, x):

Example:
>>> poly = [0.0, 0.0, 5.0, 9.3, 7.0]    # f(x) = 5x^2 + 9.3x^3 + 7x^4
>>> x = -13
>>> print evaluate_poly(poly, x) # f(-13) = 5(-13)^2 + 9.3(-13)^3 + 7(-13)^4
180339.9

def compute_deriv(poly):

Example:
>>> poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
>>> print compute_deriv(poly)        # 35^x + 9x^2 + 4x^3
[0.0, 35.0, 9.0, 4.0]

def compute_root(poly, x_0, epsilon):

Example:
>>> poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
>>> x_0 = 0.1
>>> epsilon = .0001
>>> print compute_root(poly, x_0, epsilon)
[0.80679075379635201, 8]
0

Author Comment

ID: 40242640
Have you installed the Python and the Idle GUI on your computer yet?
Hours ago and I will learn how to use it for python 2.5

I am currently using python 3 portable for this problem

Message      File Name      Line      Position
Traceback
<module>      C:\Users\Acer\Documents\portable-python\myfiles\ps2_sol\ps2_newton_sol1.py      84
evaluate_poly      C:\Users\Acer\Documents\portable-python\myfiles\ps2_sol\ps2_newton_sol1.py      25
NameError: global name 'xrange' is not defined

``````# 6.00 Problem Set 2
#
# Successive Approximation: Newton's Method
#
# Name          : Solutions
# Collaborators : <your collaborators>
# Time spent    : <total time>
#

def evaluate_poly(poly, x):
"""
Computes the value of a polynomial function at given value x. Returns that value.

Example:
>>> poly = [0.0, 0.0, 5.0, 9.3, 7.0]    # f(x) = 5x^2 + 9.3x^3 + 7x^4
>>> x = -13
>>> print evaluate_poly(poly, x)  # f(-13) = 5(-13)^2 + 9.3(-13)^3 + 7(-13)^4
180339.9

poly: list of numbers, length > 0
x: number
returns: float
"""
total = 0.0
for i in xrange(len(poly)):
total += poly[i] * (x ** i)
return total

def compute_deriv(poly):
"""
Computes and returns the derivative of a polynomial function. If the
derivative is 0, returns [0.0].

Example:
>>> poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
>>> print compute_deriv(poly)        # 35^x + 9x^2 + 4x^3
[0.0, 35.0, 9.0, 4.0]

poly: list of numbers, length > 0
returns: list of numbers
"""
poly_deriv = []
if len(poly) < 2:
return [0.0]
for j in xrange(1, len(poly)):
poly_deriv.append(float(j * poly[j]))
return poly_deriv

def compute_root(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.

Example:
>>> poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
>>> x_0 = 0.1
>>> epsilon = .0001
>>> print compute_root(poly, x_0, epsilon)
[0.80679075379635201, 8]

poly: list of numbers, length > 1.
Represents a polynomialfunction 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]
"""
root = x_0
counter = 1
while abs(evaluate_poly(poly, root)) >= epsilon:
root = (root - evaluate_poly(poly, root) /
evaluate_poly(compute_deriv(poly), root))
counter += 1
return [root, counter]

#def evaluate_poly(poly, x):
poly = [0.0, 0.0, 5.0, 9.3, 7.0]    # f(x) = 5x^2 + 9.3x^3 + 7x^4
x = -13
print (evaluate_poly(poly, x)) # f(-13) = 5(-13)^2 + 9.3(-13)^3 + 7(-13)^4

#def evaluate_poly(poly, x):
poly = [0.0, 0.0, 5.0, 9.3, 7.0]    # f(x) = 5x^2 + 9.3x^3 + 7x^4
x = -13
print (evaluate_poly(poly, x)) # f(-13) = 5(-13)^2 + 9.3(-13)^3 + 7(-13)^4

#def compute_deriv(poly):
poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
print (compute_deriv(poly))        # 35^x + 9x^2 + 4x^3

#def compute_root(poly, x_0, epsilon):
poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
x_0 = 0.1
epsilon = .0001
print (compute_root(poly, x_0, epsilon))
poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
print (compute_deriv(poly))        # 35^x + 9x^2 + 4x^3)

#def compute_root(poly, x_0, epsilon):
poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
x_0 = 0.1
epsilon = .0001
print (compute_root(poly, x_0, epsilon))
``````
0

LVL 27

Accepted Solution

d-glitch earned 2000 total points
ID: 40243878
Here is a screen shot of my version of the evaluate_poly function.
Some of the lines of code are taken directly from the problem definition, but I did have to write code for the actual function definition.  You should not need any special libraries for these problems.

I typed the code in a .py file and saved it.  Then I ran it, which opened the Python Shell and executed the code properly.  At this point, the function was available interactively.  So I could change the value of x, and the values of the polynomial coefficients.

I don't have or use Portable Python, but if you post screen shots of your work and error messages I can probably help.

Here is the evaluate_poly function definition in pastable form:
def evaluate_poly(poly, x):
value = (    0 + poly[4])*x
value = (value + poly[3])*x
value = (value + poly[2])*x
value = (value + poly[1])*x
value = (value + poly[0])
return value
0

Author Closing Comment

ID: 40244495
this worked for the function

thankls
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
###### Suggested Courses
Course of the Month21 days, 6 hours left to enroll

#### 810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.