Unit1 Python
Unit1 Python
1+2
c= = 1.5 , f (1.5) = 0.25
2
since f (1).f (1.5) < 0 new interval will become (1, 1.5) {∵ here
b =c }
1 + 1.5
c= = 1.25 , f (1.25) = −0.4375
2
since f (1.25).f (1.5) < 0 new interval will become (1.25, 1.5) {∵
here a =c }
1.25 + 1.5
c= = 1.375 , f (1.375) = −0.109375
2
since f (1.375).f (1.5) < 0 new interval will become (1.375, 1.5)
{∵ here a=c }
1.375 + 1.5
c= = 1.4375 , f (1.4375) = 0.06640625
2
since f (1.375).f (1.4375) < 0 new interval will become (1.375, 1.4375)
{∵ here b=c }
1.375 + 1.4375
c= = 1.40625 , f (1.40625) = −0.0224609375
2
since f (1.40625).f (1.4375) < 0 new interval will become (1.40625, 1.4375)
{∵ here a=c }
def f(x):
return math.tan(x)-x
def bisection(a,b,tole):
#error,c = math.inf ,1
while (abs(a-b)>tole):
if f(a)*f(b)>0:
print("bisection is not possible ")
return
if f(a)*f(b)==0:
if f(a)==0:
print(a, "is the root ")
else:
print(b, "is the root ")
elif f(a)*f(b)<0:
c=(a+b)/2
if f(a)*f(c)<0:
b=c
error = np.abs(a-b)
elif f(a)*f(c)>0:
a=c
error = np.abs(a-b)
elif f(c)==0 :
print(c," is the root")
error = np.abs(a-b)
break
print("The root is in the interval (",a,",",b,") and error is " ,error*100/c,"%")
return c
a= float(input("Enter the lower limit "))
b = float(input("Enter the upper limit "))
tole = eval(input("enter the tolerance value = "))
r = bisection(a,b,tole)
print ("ans is ", r)
x= np.linspace(-10,10,500)
f = np.vectorize(f)
plt.plot(x,f(x))
plt.plot(x,f(x),color = 'blue' , label = 'f(x) ')
plt.legend()
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.grid()
plt.show()
Figure 2: tan(x) − x
def f(x):
return x**2-2
def bisection(a,b,tole):
while (np.abs(a-b)>tole):
if f(a)*f(b)>0:
print("bisection is not possible ")
return
if f(a)*f(b)==0:
if f(a)==0:
print(a, "is the root ")
else:
print(b, "is the root ")
elif f(a)*f(b)<0:
c=(a+b)/2
if f(a)*f(c)<0:
b=c
error = np.abs(a-b)
elif f(a)*f(c)>0:
a=c
error = np.abs(a-b)
elif f(c)==0 :
print(c," is the root")
error = np.abs(a-b)
break
print("The root is in the interval (",a,",",b,") and error is " ,error*100/c,"%")
return c
a= float(input("Enter the lower limit "))
b = float(input("Enter the upper limit "))
tole = eval(input("enter the tolerance value = "))
r = bisection(a,b,tole)
print ("Root is ", r)
short= fsolve(f,[-1.5,1.5])
print("fsolve gives the all roots of equation = " , short)
x= np.linspace(-2,2,100)
plt.plot(x,f(x))
plt.plot(x,f(x),color = 'blue' , label = 'f(x) ')
plt.legend()
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.grid()
plt.show()
Figure 3: x2 − 2
The Newton-Raphson method is an iterative root-finding technique that utilizes tangent lines to approximate the root of a
function. By iteratively refining the initial guess, the method converges rapidly to the root. The key idea is to move along the
tangent line of the function’s curve, which brings us closer and closer to the root.
Let’s consider a function f (x) and its derivative f ′ (x). Starting with an initial guess x0 , we can construct a tangent line at the
point (x0 , f (x0 )). The equation of this tangent line is given by:
x2 = 2.74
|2.74−3| Figure 4: Newton Raphson
error = 3 · 100 = 8.67%
f (x2 )
• 2nd Iteration : x3 = x2 −
f ′ (x2 )
.57
x3 = 2.74 − 22.52 = 2.72
x3 = 2.72
|2.74−2.72|
error = 2.74 · 100 = 0.4%
f (x3 )
• 3rd Iteration : x4 = x3 −
f ′ (x3 )
0.12
x4 = 2.72 − 22.19 = 2.715
x4 = 2.715
|2.72−2.715|
error = 2.72 · 100 = 0.18%
f (x4 )
• 4th Iteration : x5 = x4 −
f ′ (x4 )
0.01
x5 = 2.715 − 22.11 = 2.7146
x5 = 2.7146
|2.715−2.7146|
error = 2.715 · 100 = 0.014%
f (x5 )
• 5th Iteration : x6 = x5 −
f ′ (x5 )
0.004
x6 = 2.7146 − 22.10 = 2.7145
x6 = 2.7145
|2.7145−2.71456|
error = 2.71456 · 100 = 0.0066%
def newton(x0,tol):
def fun(x):
y = x**3 - 20
return y
def funp(x):
yp = 3*x**2
return yp
x1 =0
count =0
while True:
x1 = x0 - fun(x0)/funp(x0)
err = abs((x1 -x0)/x0)*100
x0 = x1
count+=1
if err<tol:
break
print(x1," is the closest root after the ",count ," iteration with relative error possible ", err)
newton(x0,tol)
Figure 5: x3 − 20
def newton(x0,tol):
def fun(x):
y = math.tan(x) -x
return y
def funp(x):
yp = (1/math.cos(x)**2)-1
return yp
x1 =0
count =0
while True:
x1 = x0 - fun(x0)/funp(x0)
err = abs((x1 -x0)/x0)*100
x0 = x1
count+=1
if err<tol:
break
print(x1," is the closest root after the ",count ," iteration with relative error possible ", err)
newton(x0,tol)
Figure 6: tan(x) − x
x3 = 3.35
|3.35−5.5|
error = 5.5 = 0.64
f (x3 ) · (x3 − x2 )
• 2nd Iteration : x4 = x3 −
f (x3 ) − f (x2 )
|3.35−3.05|
error = 3.35 = 0.09
f (x4 ) · (x4 − x3 )
• 3rd Iteration : x5 = x4 −
f (x4 ) − f (x3 )
x5 = 2.77
|3.05−2.77|
error = 3.05 = 0.1
Code and Output of Secant Method
def f(x):
y =math.tan(x) - x
return y
def secant(x1,x2,tol):
error = ((abs(x2-x1))/x2)
while error>tol:
x3 = x2 - f(x2)*(x2 -x1)/(f(x2) - f(x1))
x1,x2 = x2 ,x3
error = (abs(x1 -x2 )/x2)
print("The root is ",x3,"with possible error of ",error,count)
secant(x1,x2,tol)
Figure 8
def f(x):
y =x**3-20
return y
def secant(x1,x2,tol):
error = ((abs(x2-x1))/x2)
while error>tol:
x3 = x2 - f(x2)*(x2 -x1)/(f(x2) - f(x1))
x1,x2 = x2 ,x3
error = (abs(x1 -x2 )/x2)
print("The root is ",x3,"with possible error of ",error,count)
secant(x1,x2,tol)
Figure 9