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

could anyone solve the newtons method prob in week 3 pset?? if yes could u pls gimme the code?

OpenStudy (microbot):

ye i did (and many others) but i wont give you the code-.-....if you want i could help you with your code though!

OpenStudy (anonymous):

def computeRoot(poly,x_0,epsilon): f = evaluatePoly(poly,x_0) fd=computeDeriv(poly,x_0) count = 0 while float(fd)!=0: dx = f / float(fd) if abs(dx) < epsilon * (1 + abs(x_0)): return int(count) x_0 = x_0 - dx f = evaluatePoly(poly,x_0) fd=computeDeriv(poly,x_0) count = count + 1

OpenStudy (anonymous):

i tried this code i feel its wrong il giv u the prev code temme wats wrong

OpenStudy (anonymous):

def evaluatePoly(poly, x): ''' Computes the value of a polynomial function at given value x. Returns that value as a float. poly: list of numbers, length > 0 x: number returns: float ''' l=len(poly) i=l-1 c=0 s=0.0 b=0.0 while c<l: b=poly[i]*(x**i) s=s+b c=c+1 i=i-1 return s >>> def computeDeriv(poly,x): ''' Computes and returns the derivative of a polynomial function as a list of floats. If the derivative is 0, returns [0.0]. poly: list of numbers, length &gt; 0 returns: list of numbers (floats) ''' l=len(poly) i=l-1 c=0 s=0.0 b=0.0 while c<l: poly[i]=poly[i]*(i) c=c+1 i=i-1 if len(poly)!=1: poly.remove(0) return evaluatePoly(poly, x)

OpenStudy (anonymous):

these are the codes used to calc val of polynomial and its deriv

OpenStudy (anonymous):

def computeRoot(poly,x_0,epsilon): count=0 while abs(evaluatePoly(poly,x_0))>epsilon: x1=x_0-(evaluatePoly(poly,x_0)/computeDeriv(poly,x_0)) x_0=x1 return x1 >>> print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1, .0001) Traceback (most recent call last): File "<pyshell#58>", line 1, in <module> print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1, .0001) File "<pyshell#57>", line 4, in computeRoot x1=x_0-(evaluatePoly(poly,x_0)/computeDeriv(poly,x_0)) ZeroDivisionError: float division by zero

OpenStudy (anonymous):

now solve ma prob pls i have only 30 mints

OpenStudy (microbot):

have you already passed the greader for evaluatePoly and computeDeriv?

OpenStudy (anonymous):

yo i have!!

OpenStudy (microbot):

ok sec

OpenStudy (anonymous):

i can upload the screenshots if u want

OpenStudy (microbot):

1st of all why your computederiv has 2 variables? its supposed to be computeDeriv(poly) not (poly,x)

OpenStudy (anonymous):

ive modified it yaar cz we need the value of derivative of poly at x_0

OpenStudy (microbot):

im not sure it will pass the grader but .....dunno...ok let me see it like you made it then.

OpenStudy (anonymous):

bro when IDLE says its wrong how will grader pass it?? if u can pls gimme ur code il understand my mistake n rem errors frm ma code!

OpenStudy (microbot):

nah you wont :P and im looking into your mistakes right now give me a sec:)

OpenStudy (anonymous):

okay but i have only 16 mints left

OpenStudy (microbot):

i am trying to help you and i know u have few time left:)

OpenStudy (microbot):

what im thinking now is why your derivative gives you 0?

OpenStudy (anonymous):

thats wat i am thinkin since yesterday

OpenStudy (anonymous):

its only 8 mints left m totally stuck

OpenStudy (microbot):

btw your poly remains always the same for every iteration if im not wrong

OpenStudy (microbot):

def computeRoot(poly,x_0,epsilon): count=0 while abs(evaluatePoly(poly,x_0))>epsilon: x1=x_0-(evaluatePoly(poly,x_0)/computeDeriv(poly,x_0)) x_0=x1 return x1 in this code i mean

OpenStudy (anonymous):

no yaar ive checked by printing evaluatePoly every time its gettin changed

OpenStudy (anonymous):

def computeRoot(poly,x_0,epsilon): count=0 while abs(evaluatePoly(poly,x_0))>epsilon: print evaluatePoly(poly,x_0) x1=x_0-(evaluatePoly(poly,x_0)/computeDeriv(poly,x_0)) print x1 x_0=x1 count=count+1 print count return [x1,count] >>> print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1, .0001) -13.2119 3.77609905398 1 475.866222176 2.03984812874 2 121.649030977 0.223007429836 3 23.3521783161 -0.75 4 24.0 Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> print computeRoot([-13.39, 0.0, 17.5, 3.0, 1.0], 0.1, .0001) File "<pyshell#63>", line 5, in computeRoot x1=x_0-(evaluatePoly(poly,x_0)/computeDeriv(poly,x_0)) ZeroDivisionError: float division by zero

OpenStudy (microbot):

lets say you have f(x)=x^2+34x+8......in every iteration you use this...while you have to use its derivative..... xn+1=xn−f(xn)f′(xn)

OpenStudy (anonymous):

thats ma progress if u giv the code itl be better n i will also be of grt help in future try n understand

OpenStudy (anonymous):

its ova times up!

OpenStudy (microbot):

i am not gonna give you full code im sry the point is to learn

OpenStudy (anonymous):

u can always learn frm the answer but whateva times up n thanks fr helpin

OpenStudy (microbot):

didnt manage rly to help you...and im sry time is up for you ...but if you want i can still try and resolve this with you...

OpenStudy (anonymous):

now can i see ur code??? i can not submit it anymore

OpenStudy (microbot):

about poly i was wrong and you were right...

OpenStudy (anonymous):

i told ya ive tried everything first time in life have i asked some one fr a code :-D

OpenStudy (microbot):

check your messages.

OpenStudy (anonymous):

Since you have the time mate it would be nice to sit here with people willing to help you to walk through the problem and understand how it can be solved! That's my suggestion anyway!

OpenStudy (anonymous):

times ova chris but im stilltryin to understand the error in ma code

OpenStudy (anonymous):

thanks micro im readin n tryin to understand it

OpenStudy (anonymous):

Please come back with specific questions so we can help you!

OpenStudy (microbot):

@ishesh by the way let me know how you will modify your solution by learning from mine:) its always nice to see different ways.

OpenStudy (anonymous):

@ishesh no questions at all???

OpenStudy (anonymous):

http://dpaste.com/818470/

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!