#Kevin Arvai
#NewtonRapshon.py

#Determine real roots of f(x) = -1 + 5.5x -4x^2 + 0.5x^3
#graphically it looks like it has roots near 0, 1.5 and 6
#supplied
#x0 is initial guess
#es is the error limit
x0 = 0
es = 0.01
itermax = 30
eps = 0.1

iter = 0
xr = x0

#derivative of f(x) = 1.5x^2 – 8x + 5.5

while iter >= 0:

xrold = xr

if ( abs( (1.5*xrold**2) – (8.0*xrold) + 5.5 ) < eps):
print abs( (1.5*xrold**2) – (8.0*xrold) + 5.5 )
break

xr = xrold – ( (-1.0 + (5.5*xrold) – (4.0*xrold**2.0) + (0.5*xrold**3.0)) / ((1.5*xrold**2) – (8.0*xrold) + 5.5 ) )

iter = iter +1
#print iter
#print “xr : ” + str(xr)

if (xr != 0):
ea = 100*abs((xr-xrold)/xr)
print “ea: ” + str(ea)

if ( ea < es):
break

if (iter >= itermax):
break

print “x0: ” + str(x0)
print “xr: ” + str(xr)
print (-1.0 + (5.5*xr) – (4.0*xr**2.0) + (0.5*xr**3.0))

One thought on “NewtonRapshon.py

Comment Here:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s