# 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 totaldef 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_derivdef 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]

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.

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 totaldef 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_derivdef 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^4x = -13print (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^4x = -13print (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^4print (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^4x_0 = 0.1epsilon = .0001print (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^4print (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^4x_0 = 0.1epsilon = .0001print (compute_root(poly, x_0, epsilon))

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…

Introduction
On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected, yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…

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 modules and packages in Python.
Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…