0% found this document useful (0 votes)
13 views3 pages

Lab 6

Lab 6 programs

Uploaded by

akshayandani05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Lab 6

Lab 6 programs

Uploaded by

akshayandani05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB 6: Solution of algebraic and transcendental equation by Regula-Falsi and Newton-Raphson

method

Objectives:

Use python

1. to solve algebraic and transcendental equations by the Regula-Falsi method.

2. to solve algebraic and transcendental equations by the Newton-Raphson method

1.Regula-Falsi method to solve a transcendental equation

Obtain a root of the equation x3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method.

Perform 5 iterations.

from sympy import *

x=Symbol('x')

g=input('Enter the function ')

f=lambdify(x , g )

a=float(input ('Enter a valus :'))

b=float(input('Enter b valus :'))

N=int(input('Enter number of iterations :'))

for i in range (1 , N+1 ):

c=(a*f(b)-b*f(a))/(f(b)-f(a))

if((f(a)*f(c)<0)):

b=c

else :

a=c

print('itration %d \t the root %0.3f \t function value %0.3f \n'%(i ,c , f ( c ))) ;

output:
Enter the function x**3-2*x-5
Enter a valus :2
Enter b valus :3
Enter number of iterations :5
itration 1 the root 2.059 function value -0.391

itration 2 the root 2.081 function value -0.147

itration 3 the root 2.090 function value -0.055

itration 4 the root 2.093 function value -0.020

itration 5 the root 2.094 function value -0.007


Using tolerance value we can write the same program as follows: Obtain a root of the equation x3 −
2x − 5 = 0 between 2 and 3 by regula-falsi method. Correct to 3 decimal places.
from sympy import *

x=Symbol('x')

g=input ('Enter the function') #%x^3-2*x-5; % function

f=lambdify(x , g)

a=float(input('Enter a valus :')) # 2

b=float(input('Enter b valus :')) # 3

N=float(input('Enter tolarence :')) # 0.001

x=a ;

c=b ;

i=0

while(abs( x-c )>=N):

x=c

c=((a*f( b )-b*f( a ))/(f( b )-f( a ))) ;

if (( f( a )*f ( c )<0 )):

b=c

else :

a=c

i=i+1

print ('itration %d \t the root %0.3f \t function value %0.3f \n'%(i ,c , f ( c ))) ;

print ('final value of the root is %0.5f '%c )

output: Enter the functionx**3-2*x-5


Enter a valus :2
Enter b valus :3
Enter tolarence :0.001
itration 1 the root 2.059 function value -0.391

itration 2 the root 2.081 function value -0.147

itration 3 the root 2.090 function value -0.055

itration 4 the root 2.093 function value -0.020

itration 5 the root 2.094 function value -0.007

itration 6 the root 2.094 function value -0.003

final value of the root is 2.09431


2.Newton-Raphson method to solve a transcendental equation

Find a root of the equation 3x = cos x+ 1, near 1, by Newton Raphson method. Perform 5 iterations

from sympy import *

x=Symbol('x')

g=input('Enter the function')

f=lambdify(x , g)

dg=diff( g ) ;

df=lambdify(x , dg)

x0=float(input ('Enter the intial approximation')) ; # x0=1

n= int(input('Enter the number of iterations')) ; #n=5;

for i in range(1 , n+1):

x1 =(x0 - (f(x0)/df( x0 )))

print('itration %d \t the root %0.3f \t function value %0.3f \n'%(i , x1 , f ( x1 )))

x0 = x1

output:
Enter the function3*x-cos(x)-1
Enter the intial approximation1
Enter the number of iterations5
itration 1 the root 0.620 function value 0.046

itration 2 the root 0.607 function value 0.000

itration 3 the root 0.607 function value 0.000

itration 4 the root 0.607 function value 0.000

itration 5 the root 0.607 function value 0.000

You might also like