I am struggling with Problem Set 2, problem 1 and the solution doesnt help me either. Does someone have some sample code to get me started?
Hey Jenks, so what I ended up using for the first evaluate_poly function is a for each loop, but I used a slightly modified version that allows for the current index to be referenced. The index represents the actual power of x. For example: If we have the polynomial of \[3x^{2} + 2x +1\] This can be represented in the poly form of (1.0, 2.0, 3.0). This is the case because \[1.0 * x^{0} = 1\]\[2.0 * x^{1} = 2x\]\[2.0 * x^{2} = 3x^{2}\] If you notice each of the powers of x correlate with the index of where the coefficient is stored. Therefore, the lowest ranking coefficient of 1.0 is the first item with an index of 0 to represent x to the zero power. The next coefficient of 2.0 is found in the second position with an index of 1. The solution is to multiply each coefficient in the tuple by the provided power of x raised to the index of that coefficient. Using our example above: \[3x^{2} + 2x +1\] Lets choose the value of 3 for x. poly = (1.0, 2.0, 3.0) \[1.0 * 3^{0} = 1.0\]\[2.0 * 3^{1} = 6.0\]\[3.0 * 3^{2} = 27.0\]Then we add each of the values together 1.0 + 6.0 + 27.0 = 34.0 We learned how to use a for each loop to access each element in a tuple. We can use one again to select each coefficient in our tuple(poly), but we also want to reference the index to raise our provided x. There is a modified for each that allows each current elements index to also be referenced. letters = ('a','b','c') for index,letter in enumerate(letters): print '"%s" is at index: %i' % (letter, index) Let me know if this helps any, Grant
Thanks for your help Grant. I understand the concept but am having trouble getting the code to work. I have the following code def evaluate_poly(poly,x): poly=(0.0, 0.0, 5.0, 9.3, 7.0) x=-13 sumpoly=0 for i in range (0,len(poly)) sumPoly = sumPoly+(poly[i]*x**i) return sumpoly print evaluate_poly
Jenks, You are pretty close, just a few small changes. I've renamed the variables for clarification. You need to pass them to the function as arguments. You also had a few indentation issues and were missing a colon at the end of the for. def evaluate_poly(poly,x): sumpoly=0 for i in range (0,len(poly)): sumPoly = sumPoly+(poly[i]*x**i) return sumpoly poly1=(0.0, 0.0, 5.0, 9.3, 7.0) x1=-13 print evaluate_poly(poly1,x1)
ditto .... pls use a code pasting site like; http://dpaste.com ot http://pastebin.com the Python Tutorial in the documentation is a good reference - it should be installed on your computer. when you define a function, the 'variables'/'names' that are inside the parens are arguments that you must pass to the function - those arguments are defined outside of the function. you were close, i've reworked your code but didn't test it http://dpaste.com/799928/ http://docs.python.org/tutorial/controlflow.html#defining-functions http://docs.python.org/reference/compound_stmts.html#function-definitions
Guys, really appreciate all of your help! Thanks for the tip about pasting code as well bwCA.
Glad it worked for you. Also thanks bwCA for the code pasting links, been some time since I've used forums.
For problem 2, i can get the final tuple but i get (-0.0, 0.0, 35.0, 9.0, 4.0). Is the -0.0 at the start correct? My code is below http://dpaste.com/800607/
that's kinda weird... but when you take the derivative you lose the constant - this is how it should be ... http://dpaste.com/800778/
I had just used your code and i get the same answer. Does it work when you use it or do you get -0.0 at the beginning?
when you take a derivative of an equation you lose the constant - here is what i got with may last paste/post - http://dpaste.com/801711/ didn't spend any time on the -0.0, but that is kinda wierd, bet there is an answer out there
Join our real-time social learning platform and learn together with your friends!