• Support PF! Buy your school textbooks, materials and every day products Here!

MATLAB Script for Bisection Method

  • #1
798
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:

Answers and Replies

  • #2
DrClaude
Mentor
7,167
3,311
  • #3
798
1
That should be
Code:
        p2=(a1+b1)/2;
That'll do it! Thanks!
 

Related Threads on MATLAB Script for Bisection Method

  • Last Post
Replies
1
Views
277
  • Last Post
Replies
7
Views
4K
  • Last Post
Replies
2
Views
11K
Replies
3
Views
6K
Replies
6
Views
2K
  • Last Post
Replies
2
Views
655
  • Last Post
Replies
10
Views
1K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
0
Views
1K
Top