Roots of Equation - BiSection Method
Bisection method is one of the numerical methods to find roots of the equation, included in the syllabus of Third Year Mechanical Engineering course for colleges under University of Pune.
Let us consider an example
Figure 1: Polynomial function in f(x)=0 form. |
Create a function file in MATLAB as shown in figure 1, having a polynomial equation in a function body.
Save the function file with Bifun as a file name.
Figure 2: Program to find roots using bisection method. |
Create a script file shown in figure 2.
- Write code to allow a user to input initial guess x1, x2, and n.
- Check if the initial condition satisfying the condition or not, using while loop. When the condition is true, while loop will stop and next lines of code will be executed. The while loop will continue to execute till the condition becomes true.
- The for loop will be executed for n number of times. Higher the value of n, higher will be the accuracy.
- If the product of, f(x1) and f(x2) is negative, a mean value (x3) is assigned to x2. Else it will be assigned to x1.
The following figure shows the result
Figure 3: Executing script file. |
The first initial declaration does not satisfy the condition of, f(x1)*f(x2)<0, therefore it is required to define values of x1 and x2 again. Finally, the root of the equation is -0.97168.
Figure 4: Solution to the numerical method using MATLAB Solver. |
The MATLAB Solver 'roots()' allows a user to verify the answer.
The matrix p is a matrix of coefficients of terms in a polynomial equation. As this is second order polynomial equation, possible roots are two.
Note: MATLAB SOLVER 'roots()' is used to calculate roots of the linear polynomial equation only.
This concludes the first program in Numerical Methods and Optimization. Kindly share with fellow faculty member and students.
Kindly subscribe to r
Feel free to connect with me on any question.
Regards.
Comments
Post a Comment