+1 (315) 557-6473 

How to Find Zeros of Function by Bisection and Secant Methods using Matlab

April 25, 2025
Dr. Leonard Fischer
Dr. Leonard Fischer
Australia
MATLAB
Dr. Leonard Fischer, with over 12 years of experience in numerical analysis and computational mathematics, earned his Ph.D. from Murdoch University, Australia.

MATLAB is a popular tool for solving various mathematical problems, particularly when it comes to finding the zeros of functions. In many academic assignments, students are tasked with writing MATLAB functions to solve equations of the form f(x)=0 within a specified interval. Numerical methods like the bisection method and secant method are commonly used for this purpose. This blog will guide students to solve their MATLAB assignment using Bisection and Secant Methods, focusing on writing and implementing MATLAB functions that tackle the problem of finding zeros of functions. This guide will cover the implementation of core functions, how to test them, and how to troubleshoot common challenges.

Understanding the Problem

Before diving into the technicalities of MATLAB, it’s crucial to fully understand the problem at hand. Generally, the task will ask you to find the zeros of a given function f(x)=0 within a specified interval [a, b], with an additional requirement of an acceptable tolerance level. This means you need to determine the value of xxx where f(x) crosses zero, i.e., where the function intersects the x-axis. A fundamental part of this task is writing MATLAB functions that apply numerical methods like bisection and secant to find these zeros.

Finding Zeros of Functions using Bisection and Secant Methods

A good understanding of the problem allows you to identify the right numerical method for solving it and helps you break down the steps of writing the MATLAB code needed for your solution.

Numerical Methods for Finding Zeros

When solving for zeros of a function, you can use several numerical methods. Two of the most widely used methods are the bisection method and the secant method. These methods approach the problem in different ways, and both are implemented in MATLAB with slight variations.

Bisection Method

The bisection method is a root-finding algorithm that repeatedly divides an interval in half and selects the subinterval that contains a zero. This method is reliable and guarantees convergence as long as the function is continuous and the initial interval contains a root. The downside of the bisection method is that it can be slow, as it cuts the interval in half during each iteration.

Secant Method

The secant method, on the other hand, is more sophisticated. It doesn’t require the function to change signs at the endpoints of the interval, unlike the bisection method. Instead, the secant method uses two initial guesses and approximates the slope of the function between these points. This makes the secant method faster than bisection but with the tradeoff that it may fail to converge in some cases.

Both methods can be used for solving f(x)=0, and depending on the situation, one method may be preferred over the other. In some assignments, you may even be asked to combine these methods, such as using a bisection approach followed by the secant method for improved efficiency.

Writing the MATLAB Code

Let’s break down the steps required to write MATLAB functions that apply these numerical methods for finding zeros of functions. We will write the following two functions: findzero and findmanyzeros.

Step 1: Writing the findzero Function

The first task is to write a MATLAB function that implements a variant of the bisection and secant methods. This function, findzero, will iteratively compute an approximation of the root of a given function within the interval [a, b], using a combination of both methods.

Here’s the structure of the findzero function in MATLAB:

function p = findzero(f, a, b, tol) % Initializing the variable w for secant method w = 1; % Loop up to 100 iterations or until convergence for iter = 1:100 % Secant method formula p = a + w * (f(a) * (a - b)) / (f(b) - w * f(a)); % Output the results of the current iteration fprintf('Iteration: %d, a: %.6f, b: %.6f, p: %.6f, f(p): %.6f\n', iter, a, b, p, f(p)); % Check if we need to update the sign of f(p) if f(p) * f(b) > 0 w = 1 / 2; % Adjusting w to halve the step size else w = 1; % Keep the original step size a = b; % Update the left endpoint end % Update the right endpoint and check for convergence b = p; if abs(b - a) < tol || abs(f(p)) < tol break; % Stop if the solution is sufficiently accurate end end end

Explanation of the Code:

  • Input Parameters:
    • f is the function whose zero we are trying to find.
    • a and b are the endpoints of the interval where we suspect the root exists.
    • tol is the tolerance level, used to determine when to stop the iterations.
  • Iterative Process:
    • The method starts with an initial guess for the root and iterates up to 100 times.
    • The secant method formula is applied in each iteration to compute a new approximation, p, for the root.
  • Convergence Check:
    • The loop continues until the difference between b and a is smaller than the tolerance or until the function value at p is close enough to zero.
    • At each iteration, the current values of a, b, p, and f(p) are printed, allowing you to monitor the convergence process.

Step 2: Testing the findzero Function

Once you have written the findzero function, it’s time to test it. For this example, let’s use the function f(x)=cos, which has a root near x=0.739085. You will test this function with an interval [0, 1] and a tolerance of 10−10.

f = @(x) cos(x) - x; a = 0; b = 1; tol = 1e-10; p = findzero(f, a, b, tol);

What to Expect:

  • As the iterations progress, you should see the values of a, b, p, and f(p) get closer to the true root of the function.
  • The printed table will give you insight into how the algorithm converges with each iteration.

Step 3: Writing the findmanyzeros Function

For more advanced assignments, you might need to find multiple zeros of a function within a given interval. The findmanyzeros function can be written to accomplish this task. It computes equidistant points between the interval [a, b], and then applies the findzero function to any pairs of consecutive points where the function changes signs.

Here’s the structure of the findmanyzeros function:

function p = findmanyzeros(f, a, b, n, tol) % Compute n+1 equidistant points between a and b xk = linspace(a, b, n+1); p = []; % Iterate over the points and find zeros for k = 2:n+1 if f(xk(k-1)) * f(xk(k)) < 0 p = [p, findzero(f, xk(k-1), xk(k), tol)]; end end end

Explanation of the Code:

  • Input Parameters:
    • f is the function whose zeros are being sought.
    • a and b define the interval.
    • n is the number of intervals (or points) to divide the interval [a, b] into.
    • tol is the tolerance level for convergence.
  • Finding Multiple Zeros:
    • The function first computes n+1 equidistant points between a and b.
    • For each pair of consecutive points, if the function values have different signs, it calls the findzero function to find the root between those points.

Step 4: Testing the findmanyzeros Function

To test the findmanyzeros function, let’s use the following functions:

findmanyzeros Function

We will test these functions on the interval [0, 10] with 50 points and a tolerance of 10−10.

f = @(x) sin(x) - exp(-x); n = 50; tol = 1e-10; p = findmanyzeros(f, 0, 10, n, tol); plot(linspace(0, 10, 100), f(linspace(0, 10, 100)), 'b'); hold on; plot(p, zeros(size(p)), 'ro');

What to Expect:

  • You should see the function f(x) plotted along with the zeros that are computed by the findmanyzeros function.
  • The zeros should appear as red dots on the graph.

Conclusion

By following these steps, students can confidently write and test MATLAB functions for finding zeros of functions. The combination of bisection and secant methods in the findzero function, along with the ability to find multiple zeros in an interval using findmanyzeros, provides a solid foundation for solving similar mathematical assignments. With practice, you'll gain a deeper understanding of numerical methods and how MATLAB can be used to implement and test them effectively.

Whether you’re solving for a single zero or finding multiple zeros across an interval, MATLAB’s powerful tools and these techniques can help you tackle any related assignment with confidence.


Comments
No comments yet be the first one to post a comment!
Post a comment