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

Bisection Method Code

The document contains a VBA code for implementing the Bisection Method to find roots of a given function. It prompts the user for lower and upper bounds, iteratively calculates midpoints, and checks for convergence within a specified tolerance. Results are recorded in a worksheet, and the final solution is displayed in a message box.

Uploaded by

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

Bisection Method Code

The document contains a VBA code for implementing the Bisection Method to find roots of a given function. It prompts the user for lower and upper bounds, iteratively calculates midpoints, and checks for convergence within a specified tolerance. Results are recorded in a worksheet, and the final solution is displayed in a message box.

Uploaded by

simimanelisi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BISECTION METHOD CODE

Option Explicit

Sub bisection()

Dim I As Integer

Dim a As Double

Dim b As Double

Dim Mid As Double

Dim fa As Double

Dim fb As Double

Dim fmid As Double

Dim ws As Worksheet

Dim H As Double

Dim r As Double

Dim tolerance As Double

a = InputBox("Enter the lower bound")

b = InputBox("Enter the higher bound")

tolerance = 0.005 ' Set the tolerance

Set ws = [Link]("Bisection")

For I = 2 To 100 ' Increase the maximum number of iterations to ensure convergence

Mid = (a + b) / 2

fa = given(a)

fb = given(b)

fmid = given(Mid)

If fa * fmid < 0 Then

b = Mid

Else
a = Mid

End If

H = fa * fmid

r = fb * fmid

[Link](I, 1).Value = I - 1 ' Numbering the iterations starting from 1

[Link](I, 8).Value = H

[Link](I, 9).Value = r

[Link](I, 2).Value = a

[Link](I, 4).Value = b

[Link](I, 3).Value = fa

[Link](I, 5).Value = fb

[Link](I, 6).Value = Mid

[Link](I, 7).Value = fmid

' Check if the interval is within the tolerance

If Abs(b - a) < tolerance Then

[Link](I, 10).Value = tolerance

Exit For

End If

Next I

MsgBox ("Solution is: " & (a + b) / 2)

End Sub

Function given(x As Double) As Double

given = x ^ 3 - 7 * x ^ 2 + 14 * x - 6

End Function

You might also like