# MATLAB Script for Bisection Method

• sandy.bridge
In summary, the conversation is about a script for the Bisection Method used for solving equations in one variable. The person is having trouble getting the script to work properly and is seeking help. They provide a function and explain the variables used in the script. Through the conversation, they realize that a line of code in the while loop is incorrect and should be changed to p2=(a1+b1)/2.

## Homework Statement

Not really for homework, but it is a script I am working on. It involves utilizing the Bisection Method for solving equations in one variable. However, I cannot seem to get it to work properly. It seems to enter the while loop and become stuck their. When I execute it by hand, it appears to work, but obviously there is some sort of code fallacy that I am not seeing.

## The Attempt at a Solution

Code:
function bisection(F, a1, b1, e)
%F represents the function we are analyzing
%F must be passed into the function as a string
%i.e., F='x^2+3*x';
%[a1,b1] represents the interval in which the zero exists
%e represents the desired tolerance

p1=(a1+b1)/2;
p2=0;

x=p1;
f=eval(F);
if f == 0
p1
else
while abs(p1-p2)-e >= 0
p1=(a1+b1)/2;
x=a1;
F1=eval(F);
x=p1;
F2=eval(F);

if F1*F2 < 0
b1=p1;
else
a1=p1;
end
p2=a1+b1/2;

end
p2
end
end

Last edited:
sandy.bridge said:
Code:
        p2=a1+b1/2;
That should be
Code:
        p2=(a1+b1)/2;

DrClaude said:
That should be
Code:
        p2=(a1+b1)/2;
That'll do it! Thanks!

## 1. What is the "Bisection Method" in MATLAB?

The Bisection Method in MATLAB is a numerical method used for finding the roots of a given function. It is based on the intermediate value theorem and works by repeatedly narrowing down the interval in which the root lies until a desired level of accuracy is reached.

## 2. How does the Bisection Method work in MATLAB?

The Bisection Method in MATLAB works by first defining an initial interval [a,b] in which the root of the function is known to lie. Then, the interval is divided into two equal parts and the function is evaluated at the midpoint. Depending on the sign of the function at the midpoint, the new interval is selected by replacing either a or b with the midpoint value. This process is repeated until the desired level of accuracy is achieved.

## 3. What are the advantages of using the Bisection Method in MATLAB?

The Bisection Method in MATLAB is advantageous because it is relatively simple to implement and is guaranteed to converge to a root as long as the function is continuous and the initial interval is chosen appropriately. Additionally, it does not require derivative information which makes it applicable to a wider range of functions.

## 4. What are the limitations of the Bisection Method in MATLAB?

The Bisection Method in MATLAB may not be the most efficient method for finding roots, especially for functions with multiple roots or when the initial interval is not chosen carefully. It also requires a continuous function, which may not always be the case.

## 5. How do I use the Bisection Method in MATLAB?

To use the Bisection Method in MATLAB, you will need to define the function you want to find the root of, choose an appropriate initial interval, and set a tolerance level for the desired accuracy. Then, you can use a while loop to repeatedly narrow down the interval until the desired accuracy is achieved. Alternatively, you can use the built-in function "fzero" in MATLAB which uses the Bisection Method to find roots.