from numpy import mat,shape
from math import sqrt
#Possible Initial guess values for roots
A = mat([[ 2],[-8] ,[2],[12]]) # Coefficients of x terms in the decreasing order of power
n = shape(A)#
x1 = -A[1]/A[0]#
print 'The largest possible root is x1 =',x1
print 'No root can be larger than the value =',x1
x = sqrt((A[1]/A[0])**2 - 2*(A[2]/A[0])**2)
print '\nAll real roots lie in the interval (-%f,%f)\n'%(x,x)
print 'We can use these two points as initial guesses for the bracketing methods and one of them for open end methods'
from numpy import mat,shape
from math import sqrt
#Evaluating Polynomial using Horner's rule
#Coefficients of x terms in the increasing order of power
A = mat([[6],[1],[-4],[1]])
x = 2
N=shape(A)
n,c = N[0],N[1]
p=[0,0,0]
p.append(A[n-1])
print 'p(4) =',p[n-1][0,0]
for i in range(1,n-1):
p[n-i]= p[n-i+1-1]*x + A[n-i-1]
print '\n p(%d)= %d\n'%(n-i,p[n-i])
print '\n f(%d) = p(1) = %d'%(x,p[0])
from numpy import poly1d,polyval, sqrt
#Root of a Equation Using Bisection Method
#Coefficients in increasing order of power of x starting from 0
A = [-10 ,-4, 1]#
print 'First finding the interval that contains a root,this can be done by using Eq 6.10'
xmax = sqrt((A[1]/A[2])**2 - 2*(A[0]/A[2]))
print '\n Both the roots lie in the interval (-%d,%d) \n'%(xmax,xmax)
x = range(-6,7)
p= poly1d(A)# p = poly(A,'x'%('c'
fx = [p(xx) for xx in x]#
for i in range(0,12):
if fx[i]*fx[i] < 0:
break
print '\n The root lies in the interval (%d,%d)\n'%(x[i],x[i])
from numpy import poly1d,polyval
#False Position Method
#Coefficients of polynomial in increasing order of power of x
A = [-2 , -1, 1]
x1 = 1 #
x2 = 3 #
fx = poly1d(A)
for i in range(1,16):
print 'Iteration No. %d \n'%(i)
fx1 = fx(x1)
fx2 = fx(x2)
x0 = x1 - fx1*(x2-x1)/(fx2-fx1)
print 'x0 = %f \n'%(x0)#
fx0 = fx(x0)#
if fx1*fx0 < 0:
x2 = x0
else:
x1 = x0 #
from numpy import poly1d,polyval,polyder
#Root of the Equation using Newton Raphson Method
#Coefficients of polynomial in increasing order of power of x
A = [ 2, -3, 1]#
fx = poly1d(A)
dfx = polyder(fx)
x=[0]
f=[]
df=[]
for i in range(1,11):
f.append(fx(x[i-1]))
if f[i-1] != 0:
df.append(dfx(x[i-1]))
x.append(x[i-1] - f[i-1]/df[i-1])
print 'x%d = %f\n'%(i+1,x[i])#
else:
print 'Since f(%f) = 0, the root closer to the point x = 0 is %f \n'%(x[i-1],x[i-1] )
break
from numpy import poly1d,polyval,polyder
#Root of the Equation using Newton Raphson Method
#Coefficients of polynomial in increasing order of power of x
A = [ 6, 1 , -4 , 1 ]
fx = poly1d(A)
dfx = polyder(fx)
f=[];df=[]
x = [5.0] #
for i in range(1,7):
f.append(fx(x[i-1]))
if f[i-1] != 0:
df.append(dfx(x[i-1]))
x.append(x[i-1] - f[i-1]/df[i-1])
print 'x%d = %f\n'%(i+1,x[i])
print 'From the results we can see that number of correct digits approximately doubles with each iteration'
from __future__ import division
from numpy import poly1d
#Root of the equation using SECANT Method
#Coefficients of polynomial in increasing order of power of x
A = [ -10, -4, 1]
x1 = 4 #
x2 = 2 #
fx = poly1d(A)
for i in range(1,7):
print '\n For Iteration No. %d\n'%(i)
fx1 = fx(x1)
fx2 = fx(x2)
x3 = x2 - fx2*(x2-x1)/(fx2-fx1) #
print '\n x1 = %f\n x2 = %f \n fx1 = %f \n fx2 = %f \n x3 = %f \n'%(x1,x2,fx1,fx2,x3) #
x1 = x2#
x2 = x3#
print 'This can be still continued further for accuracy'
from numpy import poly1d
from __future__ import division
#Fixed point method
#Coefficients of polynomial in increasing order of power of x
A = [ -2, 1, 1 ]#
B = [ 2, 0, -1 ]#
gx = poly1d(B)
x = [0] ##initial guess x0 = 0
for i in range(2,11):
x.append (gx(x[i-2]))
print '\n x%d = %f\n'%(i-1,x[i-1])
if (x[i-1]-x[(i-2)]) == 0:
print '\n%f is root of the equation,since x%d - x%d = 0 \n'%(x[i-1],i-1,i-2)
break
#Changing initial guess x0 = -1
x[0] = -1 #
for i in range(2,11):
x[i-1]= gx(x[i-2])
print '\nx%d = %f\n'%(i-1,x[i-1])
if (x[i-1]-x[i-2]) == 0:
print '\n %f is root of the equation,since x%d - x%d = 0'%(x[i-1],i-1,i-2)
break
#Fixed point method
A = [ -5, 0, 1 ]#
def g(x):
x = 5.0/x
return x
x = [1] #
print '\n x0 = %f \n'%(x[0])
for i in range(2,6):
x.append(g(i))
print ' x%d = %f \n'%(i-1,x[i-1])
#Defining g(x) in different way
def g(x):
x = x**2 + x - 5
return x
x=[0]
print '\n x0 = %f \n'%(x[0])
for i in range(2,6):
x.append(g(i))
print ' x%d = %f \n'%(i-1,x[i-1])
#Third form of g(x)
def g(x):
x = (x + 5/x)/2
return x
x=[1]
print '\n x0 = %f \n'%(x[0])
for i in range(2,8):
x.append(g(i))
print ' x%d = %f \n'%(i-1,x[i-1])
#Solving System of non-linear equations using FIXED POINT METHOD
print ' x**2 - y**2 = 3 \n x**2 + x*y \n'
def f(x,y):
x = y + 3/(x+y)
return x
def g(x):
y = (6-x**2)/x
return y
x=[1]
y=[1]
print '\n x0 = %f \n y0 = %f \n'%(x[0],y[0])
for i in range(2,5):
x.append(f((i-1),(i-1)))
y.append(g((i-1)))
print '\n x%d = %f \n y%d = %f \n'%(i-1,x[i-1],i-1,y[i-1])
#Solving System of Non-linear equations using Newton Raphson Method
print 'x**2 + x*y = 6 \n x**2 - y**2 = 3 \n'#
def F(x,y):
f = x**2 + x*y - 6
return f
def G(x,y):
g = x**2 - y**2 -3
return g
def dFx(x,y):
f1 = 2*x + y
return f1
def dFy(x,y):
f2 = y
return f2
def dGx(x,y):
g1 = 2*x
return g1
def dGy(x,y):
g2 = -2*y
return g2
x=[1]
y=[1]
for i in range(2,4):
Fval = F(i,i)
Gval = G(i,i)
f1 = dFx(i-1,i-1)
f2 = dFy(i-1,i-1)
g1 = dGx(i-1,i-1)
g2 = dGy(i-1,i-1)
D = f1*g2 - f2*g1
x.append(x[i-2] - (Fval*g2 - Gval*f2)/D )
y.append(y[i-2] - (Gval*f1 - Fval*g1)/D )
print '\n x%d = %f \n y%d = %f \n'%(i-1,x[i-1],i-1,y[i-1])
from __future__ import division
from numpy import poly1d, arange
#Synthetic Division
a = [-9 ,15 ,-7, 1]
b=[0,0,0,0]
for i in arange(3,0,-1):
b[i]= a[i] + b[i]*3
print 'b%d = %f\n'%(i,b[i-1])
print 'Thus the polynomial is'
print poly1d(b)
#Quadratic factor of a polynomial using Bairstow's Method
a = [ 10, 1 ,0 ,1]#
n = len(a)#
u = 1.8 #
v = -1 #
b=[];c=[]
for nn in range(n):
b.append(0)
c.append(0)
b[n-1] = a[n-1]
b[n-2] = a[n-2] + u*b[n-1]
c[n-1] = 0
c[n-2] = b[n-1]
for i in range(n-2,0,-1):
b[i-1]= a[i-1]+ u*b[i] + v*b[i+1]
c[i-1]= b[i] + u*c[i] + v*c[i+1]
for i in range(n,0,-1):
print 'b%d = %f \n'%(i-1,b[i-1])
for i in range(n,0,-1):
print 'c%d = %f \n'%(i-1,b[i-1])
D = c[1]*c[1] - c[0]*c[2]
du = -1*(b[1]*c[1] - c[0]*c[2])/D
dv = -1*(b[0]*c[1] - b[1]*c[0])/D
u = u + du #
v = v + du #
print '\n D = %f \n du = %f \n dv = %f \n u = %f\n v = %f \n'%(D,du,dv,u,v)
from math import sqrt
#Solving Leonard's equation using MULLER'S Method
def f(x):
y = x**3 + 2*x**2 + 10*x - 20
return y
x1 = 0 #
x2 = 1 #
x3 = 2 #
for i in range(1,11):
f1 = f(x1)
f2 = f(x2)
f3 = f(x3)
h1 = x1-x3 #
h2 = x2-x3 #
d1 = f1 - f3 #
d2 = f2 - f3 #
D = h1*h2*(h1-h2)#
a0 = f3 #
a1 = (d2*h1**2 - d1*h2**2)/D #
a2 = (d1*h2 - d2*h1)/D #
if abs(-2*a0/( a1 + sqrt( a1**2 - 4*a0*a2 ) )) < abs( -2*a0/( a1 - sqrt( a1**2 - 4*a0*a2 ) )):
h4 = -2*a0/(a1 + sqrt(a1**2 - 4*a0*a2))#
else:
h4 = -2*a0/(a1 - sqrt(a1**2 - 4*a0*a2))
x4 = x3 + h4 #
print '\n x1 = %f\n x2 = %f\n x3 = %f\n f1 = %f\n f2 = %f\n f3 = %f\n h1 = %f\n h2 = %f\n d1 = %f\n d2 = %f\n a0 = %f\n a1 = %f\n a2 = %f\n h4 = %f\n x4 = %f\n '%(x1,x2,x3,f1,f2,f3,h1,h2,d1,d2,a0,a1,a2,h4,x4) #
relerr = abs((x4-x3)/x4)#
if relerr <= 0.00001:
print 'root of the polynomial is x4 = %f'%(x4)
break
x1 = x2 #
x2 = x3 #
x3 = x4 #