Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Practical no 9

Name: Ayush Tyagi


Roll no: 8914 Date:06/12/2022

Q.1. Write a python program to solve nonlinear equation using


i. Newton-Raphsons Method

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:%0.4f and f(x1):%0.6f'%(step,x1,f(x1)))
x0=x1
step=step+1
if step>N:
flag=0
break
condition=abs(f(x1))>e
if flag==1:
print('Requred root is: %0.6f'%x1)
else:
print('\n Not convergent')
ii. Regula Falsi(False Position) Method

def falsePosition(f,x0,x1,e):
x0=float(x0)
x1=float(x1)
e=float(e)
if f(x0)*f(x1)>0.0:
print('Given value do not bracket root')
print('Try with different values')
else:
step=1
condition=True
while condition:
x2=((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0))
print('Iteration:%d, x2:%0.4f and f(x2):%0.6f'%(step,x2,f(x2)))
if f(x0)*f(x2)<0:
x1=x2
else:
x0=x2
step=step+1
condition=abs(f(x2))>e
print('\nRequred root is: %0.6f'%x2)

Q.2. Compute the roots of the following equations using above two methods
i. x3 − 10x2 + 5 = 0
def f(x):
return x**3-10*x**2+5
def g(x):
return 3*x**2-20*x
newtonRaphson(f,g,1,0.00001,100)
Iteration:1, x1:0.7647 and f(x1):-0.400570
Iteration:2, x1:0.7351 and f(x1):-0.006770
Iteration:3, x1:0.7346 and f(x1):-0.000002
Requred root is: 0.734604

falsePosition(f,1,0,0.00001)
Iteration:1, x2:0.5556 and f(x2):2.085048
Iteration:2, x2:0.7078 and f(x2):0.344218
Iteration:3, x2:0.7310 and f(x2):0.047085
Iteration:4, x2:0.7341 and f(x2):0.006270
Iteration:5, x2:0.7345 and f(x2):0.000832
Iteration:6, x2:0.7346 and f(x2):0.000110
Iteration:7, x2:0.7346 and f(x2):0.000015
Iteration:8, x2:0.7346 and f(x2):0.000002
Requred root is: 0.734603

ii. xlogx = 20
from math import *
def f(x):
return x*log(x)-20
def g(x):
return 1+log(x)

newtonRaphson(f,g,1,0.00001,100)
Iteration:1, x1:21.0000 and f(x1):43.934971
Iteration:2, x1:10.1372 and f(x1):3.479794
Iteration:3, x1:9.0878 and f(x1):0.056287
Iteration:4, x1:9.0703 and f(x1):0.000017
Iteration:5, x1:9.0703 and f(x1):0.000000
Requred root is: 9.070281

falsePosition(f,9,10,0.00001)
Iteration:1, x2:9.0692 and f(x2):-0.003444
Iteration:2, x2:9.0703 and f(x2):-0.000052
Iteration:3, x2:9.0703 and f(x2):-0.000001

Requred root is: 9.070281

iii. x = sinx

def f(x):
return x-sin(x)
def g(x):
return 1-cos(x)

newtonRaphson(f,g,1,0.00001,100)
Iteration:1, x1:0.6551 and f(x1):0.045871
Iteration:2, x1:0.4336 and f(x1):0.013459
Iteration:3, x1:0.2881 and f(x1):0.003971
Iteration:4, x1:0.1918 and f(x1):0.001174
Iteration:5, x1:0.1278 and f(x1):0.000348
Iteration:6, x1:0.0852 and f(x1):0.000103
Iteration:7, x1:0.0568 and f(x1):0.000031
Iteration:8, x1:0.0379 and f(x1):0.000009
Requred root is: 0.037853
falsePosition(f,0,2,0.00001)
Iteration:1, x2:0.0000 and f(x2):0.000000
Requred root is: 0.000000

iv. x2 − 7 = 0
def f(x):
return x**2-7
def g(x):
return 2*x

newtonRaphson(f,g,1,0.00001,100)
Iteration:1, x1:4.0000 and f(x1):9.000000
Iteration:2, x1:2.8750 and f(x1):1.265625
Iteration:3, x1:2.6549 and f(x1):0.048448
Iteration:4, x1:2.6458 and f(x1):0.000083
Iteration:5, x1:2.6458 and f(x1):0.000000
Requred root is: 2.645751

falsePosition(f,0,3,0.00001)
Iteration:1, x2:2.3333 and f(x2):-1.555556
Iteration:2, x2:2.6250 and f(x2):-0.109375
Iteration:3, x2:2.6444 and f(x2):-0.006914
Iteration:4, x2:2.6457 and f(x2):-0.000434
Iteration:5, x2:2.6457 and f(x2):-0.000027
Iteration:6, x2:2.6458 and f(x2):-0.000002
Requred root is: 2.645751

You might also like