# Chapter 24: Numerical Methods¶

## Example 24.1, page no. 636¶

In [9]:
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

Finding roots of this equation by bisection method
f(2) is -ve and f(3) is +ve so root lies between 2 and 3
The root is:  2.6875


## Example 24.3, page no. 638¶

In [17]:
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

f(x)=xeˆx−cos(x)
We are required to find the roots of f(x) by the method of false position
f(0)=−ve and f(1)=+ve so s root lie between 0 and 1
finding the roots by false position method
The root of the equation is :
0.517747878322


## Example 24.4, page no. 638¶

In [16]:
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

f(x) = x∗math.log(x)−1.2
We are required to find the roots of f(x) by the method of false position
f(2)=−ve and f(3)=+ve so s root lie between 2 and 3
finding the roots by false position method
The root of the equation is :  2.74063625664


## Example 24.5, page no. 639¶

In [4]:
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

To find the roots of f(x) = 3x−cos(x)−1 by newtons method
f(0)=−ve and f(1) is +ve so a root lies between 0 and 1
Let us take x0 =0.6 as the root is closer to 1
Root is given by r=x0−f(xn)/der(f(xn))
Approximated root in each steps are
0.607290551153
0.607096741973
0.607101775605


## Example 24.6, page no. 640¶

In [5]:
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

To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0
To find the roots by newtons method
f(5)=−ve and f(6) is +ve so a root lies between 5 and 6
Let us take x0 = 5.5
Root is given by rn=xn−f(xn)/der(f(xn))
Approximated root in each steps are
5.29545454545
5.29150409676
5.29150262213
5.29150262213


## Example 24.7, page no. 641¶

In [6]:
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

To find square root of 28 by newtons method let x=sqrt(28) ie xˆ2−28=0
To find the roots by newtons method
f(5)=−ve and f(6) is +ve so a root lies between 5 and 6
Let us take x0 = 5.5
Root is given by rn=xn−f(xn)/der(f(xn))
Approximated root in each steps are
5.29545454545
5.29150409676
5.29150262213
5.29150262213