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