1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Script for Bisection Method

  1. Jan 12, 2015 #1
    1. The problem statement, all variables and given/known data
    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.

    3. The attempt at a solution
    Code (Text):

    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: Jan 12, 2015
  2. jcsd
  3. Jan 12, 2015 #2

    DrClaude

    User Avatar

    Staff: Mentor

    That should be
    Code (Text):

            p2=(a1+b1)/2;
     
     
  4. Jan 12, 2015 #3
    That'll do it! Thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted