Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 10 Online
OpenStudy (anonymous):

x=4 y=0 numguesses=0 epsilon=.0001 tupofnum = [-13.39, 0.0, 17.5, 3.0, 1.0] strofnum=tupofnum n=numguesses=0 finalans=[] while x>=epsilon and x>=0: test=x-(evaluate_poly(tupofnum,x)/(evaluate_poly(compute_deriv(strofnum),x))) test=round(test,4) if test<0: break else: print test numguesses+=1 x=test finalans=(test,numguesses) print finalans my code for finding the f(x_0) using newtons successive approximation method. however I get a different final answer for different values of x to start at. is my code wrong?

OpenStudy (anonymous):

def evaluate_poly(tupofnum, x): """ Computes the polynomial function for a given value x. Returns that value. """ global y i=0 while i<len(tupofnum): for b in tupofnum: if tupofnum[i]==b and b>0: #print tupofnum[i],'tupofnum','index= ',i,'x=',x #debugging print y+=(x**i)*b i+=1 #print 'result of evaluate)poly-',y return y

OpenStudy (anonymous):

def compute_deriv(strofnums): """ Computes and returns the derivative of a polynomial function. If the derivative is 0, returns (0.0,).""" i=0 y=0 global result while i <len(strofnums): strofnums[i]=strofnums[i]*i result= strofnums[1:len(strofnums)] #print 'i- ',i #print "strofnums-",strofnums i+=1 #print 'result compute_deriv-',result return result

OpenStudy (anonymous):

ok discovered that for using the function eval_poly in an iteration, I needed to set y=0 at the beginning of each function call or it would continuously add every result to y of each iteration of the main while loop heheheheh. big difference.

OpenStudy (e.mccormick):

Yah, that would be a difference! Just as a note, there are a couple things you can do you make code paste better here. For one, ask the question part in thew question, then post the code in the next message with a ``` above and below it. That makes a code block like: ``` def evaluate_poly(tupofnum, x): """ Computes the polynomial function for a given value x. Returns that value. """ global y i=0 while i<len(tupofnum): for b in tupofnum: if tupofnum[i]==b and b>0: #print tupofnum[i],'tupofnum','index= ',i,'x=',x #debugging print y+=(x**i)*b i+=1 #print 'result of evaluate)poly-',y return y ``` If you try and copy and paste it without the ``` it collapses, but that version can be copied and pasted fine. There are also code pasting services for larger chunks of code, like pastebin and dpaste. http://pastebin.com/ http://dpaste.com/ I really like dpaste for simple things that only need to be around for a while.

OpenStudy (anonymous):

ok so , instructions are: ask the query in the first block, then in reply to my query, I can deliver the code, if I have ``` before the code and after such as...... ```def compute_deriv(strofnums): """ Computes and returns the derivative of a polynomial function. If the derivative is 0, returns (0.0,).""" i=0 y=0 global result while i <len(strofnums): strofnums[i]=strofnums[i]*i result= strofnums[1:len(strofnums)] #print 'i- ',i #print "strofnums-",strofnums i+=1 #print 'result compute_deriv-',result return result ```

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!