Can someone help me understand what we're being asked to do for Spring 2011, PS2 #1 "Implement the evaluate_poly function." Should our code start with asking for raw input for x and for a tuple of numbers for the poly, and then set up the function def evaluate_poly (poly, x)? I looked at the solution ps2_newton.py, but it just starts with setting up the function def evaluate_poly. When run, nothing happens because the solution code does not have any inputs. Did I misunderstand the assignment?
nope, they just want you to write the function - you don't have to 'ask' for inputs
Thanks. I'm completely new to programming and initially didn't get how to run the function in the python shell and the lecture screen shots are sometimes a little fuzzy. I figured out that after running it, in the python shell, we have to define poly, x, then type evaluate_poly(poly, x). Really basic, I know. I was also wondering if there were a couple tiny errors in the PS2 #3 Newton solution set. First, I tried running compute_root as is, without "return" statements in evaluate_poly and compute_deriv. The error is: line 11, in evaluate_poly for i in range(0,len(poly)): TypeError: object of type 'NoneType' has no len() When I put in "return sumpoly" in evaluate_poly and "return derivTerm" in compute_deriv, this does not return an error. Is this because "return" somehow saves the answer for later use, but print does something else? Second, in the function compute_deriv, should the range in "for" statement start at index 1, not 0 (as indicated in the solution set)? The result for compute_root is slightly different .80679 vs. .80678 and iterations of 7 vs. 140, but the code doesn't seem to be representing the math that should occur. poly=(-13.39, 0.0, 17.5, 3.0, 1.0) represents f(x)=x^4+ 3.0x^3 +17.5x^2 +0x - 13.39. Compute_deriv starting at index 0 results in a tuple of 5 terms: (-0.0, 0.0, 35.0, 9.0, 4.0). The Newton method entails a step to evaluate_poly on the derivative tuple, which will interpret the index as the exponent, ex: 4x^4, rather than 4x^3 because there are 5 terms. Is the best fix for this to start the range at index 1? Third, I can't seem to get the 8 iterations from the example in the instructions. The closest I got was with using range starting at 1 indicated above, 7 iterations. Thanks again.
a function will always return something even if you do not include a return statement. Without a return statement a function returns None. return 'returns' an object to the thing that called the function poly is a tuple of coefficients of a polynomial where the index of each item in the tuple is the power of that term in the function. the derivative of a five term poly should only have four terms - you lose the constant each derivative. so yep it should probably start at index one. ?with epsilon = .00001 you get 8 iterations?
Join our real-time social learning platform and learn together with your friends!