bracketing methods in matlab

In MATLAB, there are several methods for performing bracketing, or root-finding, which aim to locate the root of a function within a given interval. Two commonly used bracketing methods in MATLAB are the Bisection method and the Ridder's method.

  1. Bisection Method: The Bisection method is a simple and robust bracketing method that repeatedly bisects the interval and selects the subinterval that contains a root. Here is an example implementation:
main.m
function root = bisectionMethod(f, a, b, tol)
    while abs(b - a) > tol
        c = (a + b) / 2;
        
        if f(c) == 0
            break;
        elseif f(a) * f(c) < 0
            b = c;
        else
            a = c;
        end
    end
    
    root = (a + b) / 2;
end
282 chars
16 lines

Explanation:

  • f is the function for which the root is being found.
  • a and b are the initial interval boundaries.
  • tol is the desired tolerance or stopping criterion.
  • In each iteration, the interval [a, b] is bisected by calculating the midpoint c.
  • If f(c) is zero, we have found the root and exit the loop.
  • Otherwise, the subinterval that has a sign change is selected and becomes the new [a, b] interval.
  • The loop continues until the interval size is less than the specified tolerance tol.
  • Finally, the estimated root is calculated as the midpoint of the final interval.
  1. Ridder's Method: Ridder's method is a more efficient bracketing method that works by constructing an interpolating parabola through three points. Here is an example implementation:
main.m
function root = riddersMethod(f, a, b, tol)
    fa = f(a);
    fb = f(b);
    
    for k = 1:100
        c = (a + b) / 2;
        fc = f(c);
        
        s = sqrt(fc^2 - fa*fb);
        if s == 0
            break;
        end
        
        dx = (c - a) * fc / s;
        
        if (fa - fb) < 0
            dx = -dx;
        end
        
        x = c + dx;
        fx = f(x);
        
        if fx == 0
            break;
        end
        
        if sign(fx) == sign(fc)
            if sign(fa) ~= sign(fx)
                b = x;
                fb = fx;
            else
                a = x;
                fa = fx;
            end
        else
            a = c;
            b = x;
            fa = fc;
            fb = fx;
        end
        
        if abs(b - a) < tol
            break;
        end
    end
    
    root = (a + b) / 2;
end
866 chars
49 lines

Explanation:

  • Similar to the Bisection method, this implementation takes f as the function, a and b as the initial interval, and tol as the desired tolerance.
  • The main difference is the introduction of dx, which calculates the change in x using an interpolated root estimate.
  • The interpolating parabola is used to estimate the root's location more accurately.
  • The loop continues until the estimated root's accuracy is within the specified tolerance or a maximum number of iterations is reached.

You can call these functions by passing the appropriate function handle and interval boundaries as arguments. For example:

main.m
f = @(x) x^3 - 2*x - 5;  % Your function
a = 1;  % Initial interval boundary
b = 3;  % Initial interval boundary
tol = 1e-6;  % Tolerance

root_bisection = bisectionMethod(f, a, b, tol);
root_ridder = riddersMethod(f, a, b, tol);

disp(root_bisection);
disp(root_ridder);
272 chars
11 lines

Remember to adjust the function and interval boundaries according to your specific problem.

gistlibby LogSnag