MATLAB Script for Bisection Method

Click For Summary
SUMMARY

The forum discussion centers on a MATLAB script implementing the Bisection Method for solving equations in one variable. The user encountered an issue where the script enters an infinite loop due to a coding error in the calculation of the midpoint. The correct implementation requires updating the midpoint calculation to p2=(a1+b1)/2 instead of p2=a1+b1/2. This correction resolves the problem and allows the script to function as intended.

PREREQUISITES
  • Understanding of the Bisection Method for root-finding
  • Familiarity with MATLAB programming language
  • Knowledge of function evaluation in MATLAB using eval
  • Basic concepts of numerical methods and error tolerance
NEXT STEPS
  • Review MATLAB function syntax and best practices
  • Explore advanced numerical methods for root-finding, such as Newton's Method
  • Learn about error handling and debugging techniques in MATLAB
  • Investigate performance optimization for MATLAB scripts
USEFUL FOR

Students, educators, and professionals working with numerical methods in MATLAB, particularly those focused on implementing and troubleshooting algorithms for root-finding.

sandy.bridge
Messages
797
Reaction score
1

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:
Physics news on Phys.org
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!
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K