#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL a AS DOUBLE
    LOCAL b AS DOUBLE
    LOCAL mid AS DOUBLE
    LOCAL fa AS DOUBLE
    LOCAL fb AS DOUBLE
    LOCAL fmid AS DOUBLE
    LOCAL tolerance AS DOUBLE
    LOCAL iter AS LONG
    LOCAL max_iter AS LONG
    LOCAL temp_str AS STRING

    ' Bisection method: repeatedly halve the interval [a,b]
    ' where f(a) and f(b) have opposite signs

    PRINT "Bisection Root Finding"
    PRINT "====================="
    PRINT ""

    ' f(x) = x^2 - 4, find root between 1 and 3
    PRINT "f(x) = x^2 - 4, interval [1, 3]:"
    a = 1.0
    b = 3.0
    tolerance = 0.0001
    max_iter = 30
    iter = 0

    fa = a * a - 4.0
    fb = b * b - 4.0

    IF fa * fb < 0 THEN
        WHILE iter < max_iter
            mid = (a + b) / 2.0
            fmid = mid * mid - 4.0

            IF ABS(fmid) < tolerance OR (b - a) / 2.0 < tolerance THEN
                iter = max_iter
            ELSE
                IF fa * fmid < 0 THEN
                    b = mid
                    fb = fmid
                ELSE
                    a = mid
                    fa = fmid
                END IF
            END IF
            iter = iter + 1
        WEND

        temp_str = STR$(mid)
        PRINT "  Root: " + temp_str
        temp_str = STR$(mid * mid - 4.0)
        PRINT "  f(root) = " + temp_str
        temp_str = STR$(iter)
        PRINT "  Iterations: " + temp_str
    ELSE
        PRINT "  No sign change in interval"
    END IF

    ' f(x) = x^3 - 2x - 5, interval [2, 3]
    PRINT ""
    PRINT "f(x) = x^3 - 2x - 5, interval [2, 3]:"
    a = 2.0
    b = 3.0
    iter = 0

    fa = a * a * a - 2.0 * a - 5.0
    fb = b * b * b - 2.0 * b - 5.0

    IF fa * fb < 0 THEN
        WHILE iter < max_iter
            mid = (a + b) / 2.0
            fmid = mid * mid * mid - 2.0 * mid - 5.0

            IF ABS(fmid) < tolerance OR (b - a) / 2.0 < tolerance THEN
                iter = max_iter
            ELSE
                IF fa * fmid < 0 THEN
                    b = mid
                    fb = fmid
                ELSE
                    a = mid
                    fa = fmid
                END IF
            END IF
            iter = iter + 1
        WEND

        temp_str = STR$(mid)
        PRINT "  Root: " + temp_str
        temp_str = STR$(mid * mid * mid - 2.0 * mid - 5.0)
        PRINT "  f(root) = " + temp_str
        temp_str = STR$(iter)
        PRINT "  Iterations: " + temp_str
    END IF

    ' f(x) = cos(x) - x, interval [0, 1]
    PRINT ""
    PRINT "f(x) = cos(x) - x, interval [0, 1]:"
    a = 0.0
    b = 1.0
    iter = 0

    fa = COS(a) - a
    fb = COS(b) - b

    IF fa * fb < 0 THEN
        WHILE iter < max_iter
            mid = (a + b) / 2.0
            fmid = COS(mid) - mid

            IF ABS(fmid) < tolerance OR (b - a) / 2.0 < tolerance THEN
                iter = max_iter
            ELSE
                IF fa * fmid < 0 THEN
                    b = mid
                    fb = fmid
                ELSE
                    a = mid
                    fa = fmid
                END IF
            END IF
            iter = iter + 1
        WEND

        temp_str = STR$(mid)
        PRINT "  Root: " + temp_str
        temp_str = STR$(COS(mid) - mid)
        PRINT "  f(root) = " + temp_str
    END IF

    ' Convergence rate demo
    PRINT ""
    PRINT "Convergence (interval halving each step):"
    a = 1.0
    b = 3.0
    iter = 0
    mid = 0

    WHILE iter < 8
        mid = (a + b) / 2.0
        temp_str = STR$(a)
        temp_str = STR$(b)
        temp_str = STR$(b - a)
        PRINT "  iter " + STR$(iter) + ": [" + STR$(a) + ", " + STR$(b) + "] width=" + temp_str

        fmid = mid * mid - 4.0
        fa = a * a - 4.0
        IF fa * fmid < 0 THEN
            b = mid
        ELSE
            a = mid
        END IF
        iter = iter + 1
    WEND

    PRINT ""
    PRINT "Bisection method complete."

    FUNCTION = 0
END FUNCTION
