could anyone solve the newtons method prob in week 3 pset?? if yes could u pls gimme the code?
ye i did (and many others) but i wont give you the code-.-....if you want i could help you with your code though!
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
i tried this code i feel its wrong il giv u the prev code temme wats wrong
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 > 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)
these are the codes used to calc val of polynomial and its deriv
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
now solve ma prob pls i have only 30 mints
have you already passed the greader for evaluatePoly and computeDeriv?
yo i have!!
ok sec
i can upload the screenshots if u want
1st of all why your computederiv has 2 variables? its supposed to be computeDeriv(poly) not (poly,x)
ive modified it yaar cz we need the value of derivative of poly at x_0
im not sure it will pass the grader but .....dunno...ok let me see it like you made it then.
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!
nah you wont :P and im looking into your mistakes right now give me a sec:)
okay but i have only 16 mints left
i am trying to help you and i know u have few time left:)
what im thinking now is why your derivative gives you 0?
thats wat i am thinkin since yesterday
its only 8 mints left m totally stuck
btw your poly remains always the same for every iteration if im not wrong
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
no yaar ive checked by printing evaluatePoly every time its gettin changed
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
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)
thats ma progress if u giv the code itl be better n i will also be of grt help in future try n understand
its ova times up!
i am not gonna give you full code im sry the point is to learn
u can always learn frm the answer but whateva times up n thanks fr helpin
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...
now can i see ur code??? i can not submit it anymore
about poly i was wrong and you were right...
i told ya ive tried everything first time in life have i asked some one fr a code :-D
check your messages.
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!
times ova chris but im stilltryin to understand the error in ma code
thanks micro im readin n tryin to understand it
Please come back with specific questions so we can help you!
@ishesh by the way let me know how you will modify your solution by learning from mine:) its always nice to see different ways.
@ishesh no questions at all???
Join our real-time social learning platform and learn together with your friends!