EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests
 11+ Hours of HD Videos
5 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more