0% found this document useful (0 votes)
73 views6 pages

Bisection Method Codes

The document provides implementations of the Bisection Method in both Python and Scilab for finding roots of nonlinear equations. It includes source code examples for each programming language, detailing the algorithm and necessary functions. Additionally, it demonstrates the output of the Python program with specified initial guesses and tolerable error.

Uploaded by

1613poojasvjc
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)
73 views6 pages

Bisection Method Codes

The document provides implementations of the Bisection Method in both Python and Scilab for finding roots of nonlinear equations. It includes source code examples for each programming language, detailing the algorithm and necessary functions. Additionally, it demonstrates the output of the Python program with specified initial guesses and tolerable error.

Uploaded by

1613poojasvjc
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/ 6

Bisection Method - Python & Scilab

Bisection Method - Python & Scilab

Bisection Method - Python

AIM: Write a program in PYTHON for bisection method.


SOURCE CODE:
# Python program for implementation
# of Bisection Method for
# solving equations

# An example function whose


# solution is determined using
# Bisection Method.
# The function is x^3 - x-1
def func(x):
return x*x*x - x -1

# Prints root of func(x)


# with error of EPSILON
def bisection(a,b):

if (func(a) * func(b) >= 0):


print("You have not assumed right a and b\n")
return

c = a
while ((b-a) >= 0.00001):

# Find middle point


c = (a+b)/2

# Check if middle point is root


if (func(c) == 0.0):
break

# Decide the side to repeat the steps


if (func(c)*func(a) < 0):
b = c
else:
a = c

print("The value of root is : ","%.4f"%c)

# Driver code
# Initial values assumed
a =1
b =2
bisection(a, b)

# This code is contributed


# by Olwinton Winson Pais.
Bisection Method - Python & Scilab

Output:
The value of root is : 1.3247
Bisection Method - Python & Scilab

Bisection Method - Scilab

PRACTICAL NO. 3 BISECTION METHOD


AIM: Write a program in SCILAB for bisection method.
SOURCE CODE:

Bisection Method Algorithm (Step Wise)


1. start

2. Define function f(x)

3. Choose initial guesses x0 and x1 such that f(x0)f(x1) < 0

4. Choose pre-specified tolerable error e.

5. Calculate new approximated root as x2 = (x0 + x1)/2

6. Calculate f(x0)f(x2)
a. if f(x0)f(x2) < 0 then x0 = x0 and x1 = x2
b. if f(x0)f(x2) > 0 then x0 = x2 and x1 = x1
c. if f(x0)f(x2) = 0 then goto (8)

7. if |f(x2)| > e then goto (5) otherwise goto (8)

8. Display x2 as root.

9. Stop

This program implements Bisection Method for finding real root of nonlinear equation in
Bisection Method - Python & Scilab

python programming language.


In this python program, x0 and x1 are two initial guesses, e is tolerable error and
nonlinear function f(x) is defined using python function definition def f(x):.

Python Source Code: Bisection Method

# Defining Function
def f(x):
return x**3-5*x-9

# Implementing Bisection Method


def bisection(x0,x1,e):
step = 1
print('\n\n*** BISECTION METHOD IMPLEMENTATION ***')
condition = True
while condition:
x2 = (x0 + x1)/2
print('Iteration-%d, x2 = %0.6f 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('\nRequired Root is : %0.8f' % x2)

# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

# Converting input to float


x0 = float(x0)
x1 = float(x1)
e = float(e)

#Note: You can combine above two section like this


# x0 = float(input('First Guess: '))
# x1 = float(input('Second Guess: '))
# e = float(input('Tolerable Error: '))

# Checking Correctness of initial guess values and bisecting


if f(x0) * f(x1) > 0.0:
Bisection Method - Python & Scilab

print('Given guess values do not bracket the root.')


print('Try Again with different guess values.')
else:
bisection(x0,x1,e)

Bisection Method Python Program Output


First Guess: 2
Second Guess: 3
Tolerable Error: 0.00001

*** BISECTION METHOD IMPLEMENTATION ***


Iteration-1, x2 = 2.500000 and f(x2) = -5.875000
Iteration-2, x2 = 2.750000 and f(x2) = -1.953125
Iteration-3, x2 = 2.875000 and f(x2) = 0.388672
Iteration-4, x2 = 2.812500 and f(x2) = -0.815186
Iteration-5, x2 = 2.843750 and f(x2) = -0.221588
Iteration-6, x2 = 2.859375 and f(x2) = 0.081448
Iteration-7, x2 = 2.851562 and f(x2) = -0.070592
Iteration-8, x2 = 2.855469 and f(x2) = 0.005297
Iteration-9, x2 = 2.853516 and f(x2) = -0.032680
Iteration-10, x2 = 2.854492 and f(x2) = -0.013700
Iteration-11, x2 = 2.854980 and f(x2) = -0.004204
Iteration-12, x2 = 2.855225 and f(x2) = 0.000546
Iteration-13, x2 = 2.855103 and f(x2) = -0.001829
Iteration-14, x2 = 2.855164 and f(x2) = -0.000641
Iteration-15, x2 = 2.855194 and f(x2) = -0.000048
Iteration-16, x2 = 2.855209 and f(x2) = 0.000249
Iteration-17, x2 = 2.855202 and f(x2) = 0.000101
Iteration-18, x2 = 2.855198 and f(x2) = 0.000027
Iteration-19, x2 = 2.855196 and f(x2) = -0.000011
Iteration-20, x2 = 2.855197 and f(x2) = 0.000008

Required Root is : 2.85519695

You might also like