+ Reply to Thread
Results 1 to 3 of 3

Bisection Method VBA Code Problem

  1. #1
    Registered User
    Join Date
    11-24-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2010
    Posts
    1

    Bisection Method VBA Code Problem

    I have a test this Friday and I still can't get my Excel code for Bisection Method to run properly. The problem is that the program only runs 1 iteration, instead of running the loops until the approximate relative error is less than the tolerance value. How do I solve this?

    This is my code :

    Sub bisection()
    Dim i As Integer
    Dim a As Double, b As Double, abserror As Double, xm As Double, f1 As Double, f2 As Double, fm As Double, TOL As Double
    a = InputBox("Enter the value of the lower bound")
    b = InputBox("Enter the value of the upper bound")
    TOL = InputBox("Enter the value of the TOL")
    f1 = 3 * a + Sin(a) - Exp(a)
    f2 = 3 * b + Sin(b) - Exp(b)
    If f1 * f2 < 0 Then
    MsgBox ("There is at least one root within this interval")
    For i = 1 To 5000 Step 1
    xm = (a + b) / 2
    fm = 3 * xm + Sin(xm) - Exp(xm)
    f1 = 3 * a + Sin(a) - Exp(a)
    f2 = 3 * b + Sin(b) - Exp(b)
    If f1 * fm < 0 Then
    a = a
    b = xm
    abserror = (Abs(xm - b) / xm) * 100
    Worksheets("Sheet1").Cells(i, 1) = i
    Worksheets("Sheet1").Cells(i, 2) = a
    Worksheets("Sheet1").Cells(i, 3) = b
    Worksheets("Sheet1").Cells(i, 4) = xm
    Worksheets("Sheet1").Cells(i, 5) = f1
    Worksheets("Sheet1").Cells(i, 6) = f2
    Worksheets("Sheet1").Cells(i, 7) = abserror
    Else
    a = xm
    b = b
    abserror = (Abs(xm - a) / xm) * 100
    Worksheets("Sheet1").Cells(i, 1) = i
    Worksheets("Sheet1").Cells(i, 2) = a
    Worksheets("Sheet1").Cells(i, 3) = b
    Worksheets("Sheet1").Cells(i, 4) = xm
    Worksheets("Sheet1").Cells(i, 5) = f1
    Worksheets("Sheet1").Cells(i, 6) = f2
    Worksheets("Sheet1").Cells(i, 7) = abserror
    End If
    If abserror <= TOL Then
    MsgBox ("Root = " & xm & ", Iteration = " & i & ", Approximate Relative Percentile Error = " & abserror & " ")
    Exit For
    Else
    End If
    Next i


    Else
    MsgBox ("There are no roots within this interval")
    End If

    End Sub


    Thank you in advance! :D

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Bisection Method VBA Code Problem

    Hi, szhengyi,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,484

    Re: Bisection Method VBA Code Problem

    @HaHoBe: sorry, didn't refresh before posting the formatted code. TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Problem with RemoveDuplicates method
    By Mayda89 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2013, 12:38 PM
  2. Find Method Problem
    By Bob@Sun in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 03-28-2010, 04:11 PM
  3. Bisection method
    By rosh72851 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2008, 03:01 PM
  4. [SOLVED] Little problem with the REPLACE method.
    By jase in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-17-2005, 09:55 AM
  5. [SOLVED] Problem with find method
    By Ralph Heidecke in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2005, 04:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1