EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Bisection Method Matlab
 

Bisection Method Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated July 1, 2023

Bisection Method Matlab

 

 

Introduction to Bisection Method Matlab

Bisection method is used to find the root of equations in mathematics and numerical problems. This method can be used to find the root of a polynomial equation, given that the roots must lie in the interval defined by [a, b], and the function must be continuous in this interval. The bisection method requires 2 guesses initially and so is referred to as the ‘close bracket’ type. Compared with other root-finding methods, this method is relatively slow as it converges in a linear, steady, and slow manner.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

We do not have a pre-defined bisection method in MATLAB, so we create one to get the roots using this method.

How to create the Bisection method in MATLAB?

Let us now understand the syntax to create the bisection method in MATLAB:

Syntax

1. Z = input (‘Enter the input function and set right-hand side equal to zero:,’ ‘s’);

[Asking the user to enter the input function]

2. f = inline (Z);

[Calling inline command to create the function in MATLAB using the above input equation]

initialValue = input ('Enter the initial value for guess interval:');

  • [Asking user to enter ‘Initial value,’ which is used as the first value for 1st iteration]

endValue = input ('Enter the last value for guess interval:');

  • [Asking user to enter ‘End value’, which is used as the last value for 1st iteration]

allowedError = input ('Enter the error allowed:');

  • [Asking the user to enter tolerable error]

Code:

if f (endValue) * f (initialValue) < 0
else
fprintf('The guess entered is incorrect. Please enter the new guess\n');
initialValue = input ('Enter the initial value for guess interval:\n') ;
endValue = input ('Enter the last value for guess interval:\n');
end
for I = 2 : 500
iter1 = (endValue + initialValue) / 2;
if f (endValue) * f (iter1) < 0
initialValue = iter1;
else
endValue = iter1;
end
if f (initialValue) * f (iter1) < 0
endValue = iter1;
else
initialValue = iter1;
end
iterN (1) = 0;
iterN (i) = iter1;
if abs ((iterN (i) – iterN (i-1)) / iterN (i)) <allowedError, break, end
end
outStr = ['Required root for the input equation is: ', num2str(iter1), '']

[This is what is happening in the ‘if-else’ loop above:

1. Mid-point for the range [ initialValue, endValue ] is calculated. We have defined the midpoint as iter1, which is defined as:

iter1 = (initialValue + endValue) / 2

2. The value of the input function is calculated at ‘iter1’, giving the following scenarios:

  • If f (iter1) = 0; iter1 will be the required root
  • If f (endValue) * f (iter1)> 0, i.e, the product f (endValue) * f (iter1) is positive, then the root of the input function will lie between the range [firstValue, iter1].
  • If f (endValue) * f (iter1)< 0; i.e, the product f (endValue) * f (iter1) is negative, then the root of the input function will lie between the range [ endValue, iter1]]

Example to Implement Bisection Method Matlab

Below are the examples mentioned:

Example #1

In this example, we will take a polynomial function of degree 2 and will find its roots using the bisection method. We will use the code above and will pass the inputs as asked. For our first example, we will input the following values:

  • Pass the input function as 2*x.^2 + 3
  • Pass the firstValue as 1
  • Pass the endValue as 2
  • Pass the allowed error as 0.001
  • If the root does not lie between 1 & 2, then the code will again ask for firstValue and endValue
  • Pass the firstValue as 2 and endValue as 3 this time

Output:

Bisection Method Matlab - 1

As we can see in the output, we have obtained the root of our input function as 2.5.

Example #2

In this example, we will take a polynomial function of degree 3 and will find its roots using the bisection method. We will use the code above and will pass the inputs as asked. For this example, we will input the following values:

  • Pass the input function as 3*x.^3 + 2*x.^2
  • Pass the firstValue as 1
  • Pass the endValue as 3
  • Pass the allowed error as 0.001
  • The code will again ask for firstValue and endValue
  • Pass the firstValue as 2 and endValue as 3 this time

Output:

required equation

As we can see in the output, we have obtained the root of our input function as 2.5.

Next, let us see an example where we are not asked for the guess values the second time

Example #3

In this example, we will take a polynomial function of degree 2 and will find its roots using the bisection method. We will use the code above and will pass the inputs as asked. For this example, we will input the following values:

  • Pass the input function as x.^2 – 3
  • Pass the firstValue as 1
  • Pass the endValue as 2
  • Pass the allowed error as 0.001

Output:

required root for the input

As we can see in the output, we have obtained the root of our input function as 1.7344, after we input our guess values for the first time.

Conclusion

The bisection method is used to find the root of any polynomial function. As there is no direct function for the bisection rule in MATLAB, we define the code or logic for it manually.

Recommended Articles

This is a guide to Bisection Method Matlab. Here we discuss an introduction to Bisection Method Matlab, syntax, parameters, and examples for better understanding. You can also go through our other related articles to learn more –

  1. Matlab AND Operator
  2. Vectors in Matlab
  3. What is Simulink in Matlab?
  4. MATLAB Interpolation

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW