0% found this document useful (0 votes)
26 views2 pages

Source Code ANNUM - 177011033 - Lucky Andiza Harris

This document defines and implements the secant method to find the root of a function. It defines the secant method function, applies it to sample inputs, and prints the output root.

Uploaded by

Lucky Andiza
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)
26 views2 pages

Source Code ANNUM - 177011033 - Lucky Andiza Harris

This document defines and implements the secant method to find the root of a function. It defines the secant method function, applies it to sample inputs, and prints the output root.

Uploaded by

Lucky Andiza
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/ 2

# Defining Function

def f(x):

return x**2 + 4*x - 6

# Implementing Secant Method

def secant(x0,x1,e,N):

print('\n\n*** SECANT METHOD IMPLEMENTATION ***')

step = 1

condition = True

while condition:

if f(x0) == f(x1):

print('Divide by zero error!')

break

x2 = x0 - (x1-x0)*f(x0)/( f(x1) - f(x0) )

print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

x0 = x1

x1 = x2

step = step + 1

if step > N:

print('Not Convergent!')

break

condition = abs(f(x2)) > e

print('\n Required root is: %0.8f' % x2)

# Input Section

x0 = 1
x1 = 2

e = 0.01

N = 10

# Converting x0 and e to float

x0 = float(x0)

x1 = float(x1)

e = float(e)

# Converting N to integer

N = int(N)

#Note: You can combine above three section like this

# x0 = float(input('Enter First Guess: '))

# x1 = float(input('Enter Second Guess: '))

# e = float(input('Tolerable Error: '))

# N = int(input('Maximum Step: '))

# Starting Secant Method

secant(x0,x1,e,N)

You might also like