Results 1 to 9 of 9

Math Help - Bisection method recursive

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    23

    Bisection method recursive

    Code:
    function recbisect(f,a,b,n,err)
    
    c = a + ( b-a)/2;
    
    fx= inline(f);
    
    fc= feval(fx,c);
    
    fprintf('\n n c fc %g %g %g' , n,c,fc );
    
    if fc < err
       return 
    end
        
    sa = sign(feval(fx,a));
    sb = sign(feval(fx,b));
    sc = sign(fc);
    
    if ( sa == sc ) 
    recbisect(f,a,c,n,err);    
    else
     recbisect(f,c,b,n,err);
    end
    
    end
    I get a

    ??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
    to change the limit. Be aware that exceeding your available stack space can
    crash MATLAB and/or your computer.

    Error in ==> iscellstr at 22

    How can i stop the recursion when the if condition is met.

    Please help.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Newbie
    Joined
    Apr 2010
    Posts
    23
    okay i got it.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by AUCC View Post
    Code:
    function recbisect(f,a,b,n,err)
    
    c = a + ( b-a)/2;
    
    fx= inline(f);
    
    fc= feval(fx,c);
    
    fprintf('\n n c fc %g %g %g' , n,c,fc );
    
    if fc < err
       return 
    end
        
    sa = sign(feval(fx,a));
    sb = sign(feval(fx,b));
    sc = sign(fc);
    
    if ( sa == sc ) 
    recbisect(f,a,c,n,err);    
    else
     recbisect(f,c,b,n,err);
    end
    
    end
    I get a

    ??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
    to change the limit. Be aware that exceeding your available stack space can
    crash MATLAB and/or your computer.

    Error in ==> iscellstr at 22

    How can i stop the recursion when the if condition is met.

    Please help.
    There is no return value from this function (as well as no terminating condition)

    CB
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Apr 2010
    Posts
    23
    does this recursive implementation backtrack.

    Earlier i had a error value which was very small so thats why it had to recurse until a stack error came.

    Then later i increased the error value and the recursive calls terminated.

    I don't see any backtracking taking place?

    Why is that?
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by AUCC View Post
    does this recursive implementation backtrack.

    Earlier i had a error value which was very small so thats why it had to recurse until a stack error came.

    Then later i increased the error value and the recursive calls terminated.

    I don't see any backtracking taking place?

    Why is that?
    Your termination condition should be when abs(a-b) is small not when abs(fc) is small (let alone fc< err, as you have no guarantee that fc is positive).

    Also

    Code:
    if ( sa == sc ) 
      recbisect(f,a,c,n,err);    
    else
      recbisect(f,c,b,n,err);
    end
    is wrong you always should be calling recbisect with arguments which for which the function evaluates to opposite signs, the above does the reverse or worse).

    CB
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    This is untested but something more like this should work better:

    Code:
    function [cc,ll,uu,fcc]=recbisect(f,a,b,n,err)
     
      c = (a+b)/2;
     
      fx= inline(f);  %I will assume this works, though I don't see why it should
     
      fc= feval(fx,c);
     
      fprintf('\n n c fc %g %g %g' , n,c,fc );
     
      if abs(a-b) < err
         cc=c;ll=a;uu=b;fcc=fc;
         return  
      end
     
      if ( (feval(fx,a)*feval(fx,c)) < 0 ) 
        [cc,ll,uu,fcc]=recbisect(f,a,c,n,err);    
      else
        [cc,ll,uu,fcc]=recbisect(f,c,b,n,err);
      end
     
    end
    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Apr 2010
    Posts
    23
    Code:
    function [cc,ll,uu,fcc]=recbisect(f,a,b,n,err)
    what does this assignment [cc,ll,uu,fcc]=recbisect(f,a,b,n,err) really do.

    and also

    Code:
    if ( (feval(fx,a)*feval(fx,c)) < 0 ) 
        [cc,ll,uu,fcc]=recbisect(f,a,c,n,err);    
      else
        [cc,ll,uu,fcc]=recbisect(f,c,b,n,err);
      end
    Why all these assignment's [cc,ll,uu,fcc]=recbisect(f,a,c,n,err); and
    [cc,ll,uu,fcc]=recbisect(f,c,b,n,err);

    I cannot understand.

    Please help.
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Here is a cut down version that might work for you:

    Code:
    function y = recbisect(f,a,b,n,err)
     if n == 0;error('Cannot Find Solution in n Iterations');end
      c = (a+b)/2;
      if abs(a-b) < err
         y = c;
         return  
      end
      if f(a)*f(c) < 0  
        y = recbisect(f,a,c,n-1,err);    
      else
        y = recbisect(f,c,b,n-1,err);
      end
    Here is a test:

    Code:
    EDU>> f = @(x)x^2+2*x-10
    
    f = 
    
        @(x)x^2+2*x-10
    
    EDU>> recbisect(f,-100,100,100,0.000001)
    
    ans =
    
       -4.3166
    
    EDU>> roots([1 2 -10])
    
    ans =
    
       -4.3166
        2.3166
    
    EDU>>
    Sorry I cant elaborate more, I am a little short of time this afternoon.

    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by AUCC View Post
    Code:
    function [cc,ll,uu,fcc]=recbisect(f,a,b,n,err)
    what does this assignment [cc,ll,uu,fcc]=recbisect(f,a,b,n,err) really do.

    and also

    Code:
    if ( (feval(fx,a)*feval(fx,c)) < 0 ) 
        [cc,ll,uu,fcc]=recbisect(f,a,c,n,err);    
      else
        [cc,ll,uu,fcc]=recbisect(f,c,b,n,err);
      end
    Why all these assignment's [cc,ll,uu,fcc]=recbisect(f,a,c,n,err); and
    [cc,ll,uu,fcc]=recbisect(f,c,b,n,err);

    I cannot understand.

    Please help.
    They are there to provide return values from the function so you have the final estimate of the solution "cc", the lower and upper limits of the interval containing the actual solution and the function value at the estimated solution.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. bisection method
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 25th 2010, 10:20 AM
  2. Bisection method help
    Posted in the Calculus Forum
    Replies: 2
    Last Post: March 27th 2010, 11:38 AM
  3. Using the Bisection Method
    Posted in the Differential Geometry Forum
    Replies: 6
    Last Post: March 3rd 2010, 11:06 PM
  4. Bisection method
    Posted in the Calculus Forum
    Replies: 3
    Last Post: February 24th 2010, 05:31 AM
  5. Bisection Method Help
    Posted in the Math Topics Forum
    Replies: 4
    Last Post: November 20th 2008, 03:41 PM

Search Tags


/mathhelpforum @mathhelpforum