import numpy
x = numpy.poly([0])
p = x**3-4*x-9
print "Finding roots of this equation by bisection method"
print 'f(2) is -ve and f(3) is +ve so root lies between 2 and 3'
l = 2.
m = 3.
def f(x):
y = x**3-4*x-9
return y
for i in range(1,5):
k = 1.0/2.*(l+m)
if(f(k)<0):
l = k
else:
m = k
print "The root is: ", k
import math
print "f(x)=xeˆx−cos(x)"
def f(x):
y = x*math.e**(x)-math.cos(x)
return y
print "We are required to find the roots of f(x) by the method of false position "
print "f(0)=−ve and f(1)=+ve so s root lie between 0 and 1 "
print "finding the roots by false position method "
l = 0.0
m = 1.0
for i in range(1,11):
k = l-(m-l)*f(l)/(f(m)-f(l))
if(f(k)<0):
l = k
else:
m = k
print "The root of the equation is :"
print k
import math
print "f(x) = x∗math.log(x)−1.2"
def f(x):
y = x*math.log10(x)-1.2
return y
print "We are required to find the roots of f(x) by the method of false position "
print "f(2)=−ve and f(3)=+ve so s root lie between 2 and 3 "
print "finding the roots by false position method "
l = 2.
m = 3.
for i in range(1,4):
k = l-(m-l)*f(l)/(f(m)-f(l))
if(f(k)<0):
l = k
else:
m = k
print "The root of the equation is : ",k
import math,numpy
from scipy.misc import derivative
print "To find the roots of f(x) = 3x−cos(x)−1 by newtons method "
print "f(0)=−ve and f(1) is +ve so a root lies between 0 and 1 "
l = 0
m = 1
def f(x):
y = 3*x-math.cos(x)-1
return y
x0 = 0.6
print "Let us take x0 =0.6 as the root is closer to 1 "
print "Root is given by r=x0−f(xn)/der(f(xn))"
print "Approximated root in each steps are "
for i in range(1,4):
k = x0-f(x0)/derivative(f,x0)
print k
x0 = k
from scipy.misc import derivative
print "To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 "
def f(x):
y = x**2-28
return y
print "To find the roots by newtons method"
print "f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 "
l = 5
m = 6
print "Let us take x0 = 5.5 "
print "Root is given by rn=xn−f(xn)/der(f(xn))"
print "Approximated root in each steps are"
x0 = 5.5
for i in range(1,5):
k = x0-f(x0)/derivative(f,x0)
print k
x0 = k
from scipy.misc import derivative
print "To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0 "
def f(x):
y = x**2-28
return y
print "To find the roots by newtons method"
print "f(5)=−ve and f(6) is +ve so a root lies between 5 and 6 "
l = 5
m = 6
print "Let us take x0 = 5.5 "
print "Root is given by rn=xn−f(xn)/der(f(xn))"
print "Approximated root in each steps are"
x0 = 5.5
for i in range(1,5):
k = x0-f(x0)/derivative(f,x0)
print k
x0 = k