You are on page 1of 12

MATHS PRACTICAL

ASSIGNMENT 9
NAME:- Shubham Ramchandra Gavali
DIV-A
ROLL NO-43
1. Using Newton- Raphson’s method solve x log10 x =
12.34 with x0 = 10.
ANS)
def newtonRaphson(f,g,x0,e,N):
x0=float(x0)
e=float(e)
N=int(N)
step=1
flag=1
condition=True
while condition:
if g(x0)==0.0:
print('Divide by zero error!')
break
x1=x0-f(x0)/g(x0)
print('Iteration-%d,x1=%f,f(x1)=%f'%(step,x1,f(x1)))
x0=x1
step=step+1
if step>N:
flag=0
break
condition=abs(f(x1))>e
if flag==1:
print('\nRequired root is %f'%x1)
else:
print('\nNot convergent')

from math import*


def f(x):
return x*log10(x)-12.34
def g(x):
return log10(x)+(1/log(10))
newtonRaphson(f,g,0.5,0.0001,5)
OUTPUT:
Iteration-1,x1=94.227259,f(x1)=173.681245

Required root is 94.227259


Iteration-2,x1=22.114602,f(x1)=17.397044

Required root is 22.114602


Iteration-3,x1=12.335343,f(x1)=1.119725

Required root is 12.335343


Iteration-4,x1=11.601312,f(x1)=0.009679

Required root is 11.601312


Iteration-5,x1=11.594854,f(x1)=0.000001

2. Using Newton- Raphson’s method find an


approximation value of √5 to 10 decimal places.
ANS)
def newtonRaphson(f,g,x0,e,N):
x0=float(x0)
e=float(e)
N=int(N)
step=1
flag=1
condition=True
while condition:
if g(x0)==0.0:
print('Divide by zero error!')
break
x1=x0-f(x0)/g(x0)
print('Iteration-%d,x1=%f,f(x1)=%f'%(step,x1,f(x1)))
x0=x1
step=step+1
if step>N:
flag=0
break
condition=abs(f(x1))>e
if flag==1:
print('\nRequired root is %f'%x1)
else:
print('\nNot convergent')
from math import*
def f(x):
return x**2-5
def g(x):
return 2*x
newtonRaphson(f,g,2.0,0.0001,5)
OUTPUT:
3. Using Newton- Raphson’s method approximate the
solution to the equation x = cos(x)
ANS)
def newtonRaphson(f,g,x0,e,N):
x0=float(x0)
e=float(e)
N=int(N)
step=1
flag=1
condition=True
while condition:
if g(x0)==0.0:
print('Divide by zero error!')
break
x1=x0-f(x0)/g(x0)
print('Iteration-%d,x1=%f,f(x1)=%f'%(step,x1,f(x1)))
x0=x1
step=step+1
if step>N:
flag=0
break
condition=abs(f(x1))>e
if flag==1:
print('\nRequired root is %f'%x1)
else:
print('\nNot convergent')
from math import*
def f(x):
return x-cos(x)
def g(x):
return 1+sin(x)
newtonRaphson(f,g,0.1,0.0001,5)
OUTPUT:
4. Find approximate root of f(x) = xe^x − cos x in the
interval (0, 1) by false position method.
ANS)
def falsePosition(f,x0,x1,e):
x0=float(x0)
x1=float(x1)
e=float(e)
if f(x0)*f(x1)>0.0:
print('Given guess values do not bracket the root:')
print('Try again with different guess values')
else:
step=1
condition=True
while condition:
x2=x0-(x1-x0)*f(x0)/(f(x1)-f(x0))
print('Iteration',step,'x2-',x2,'and f(x2))=',f(x2))
if f(x0)*f(x2)<0:
x1=x2
else:
x0=x2
step=step+1
condition=abs(f(x2))>e
print('\nRequired rootnis:',x2)
from math import *
def f(x):
return x*e**x-cos(x)
falsePosition(f,0,1,0.00001)
OUTPUT:

4. Find approximate root of f(x) = tan x in the interval


(1.1, 1.2) by false position method.
ANS)
def falsePosition(f,x0,x1,e):
x0=float(x0)
x1=float(x1)
e=float(e)
if f(x0)*f(x1)>0.0:
print('Given guess values do not bracket the root:')
print('Try again with different guess values')
else:
step=1
condition=True
while condition:
x2=x0-(x1-x0)*f(x0)/(f(x1)-f(x0))
print('Iteration',step,'x2-',x2,'and f(x2))=',f(x2))
if f(x0)*f(x2)<0:
x1=x2
else:
x0=x2
step=step+1
condition=abs(f(x2))>e
print('\nRequired rootnis:',x2)
from math import *
def f(x):
return tan(x)
falsePosition(f,1.1,1.2,0.0001)
OUTPUT:

You might also like