Binary search Matlab

Discussion in 'Programmer's Corner' started by Cerkit, Jan 17, 2010.

  1. Cerkit

    Thread Starter Active Member

    Jan 4, 2009
    275
    3
    Hi. I am trying to calculate a root within a certain precision. I have written the following matlab code but for some reason it doesn't reach the specified precision and when analysing the values it gives it begins to become precise then suddenly jumps back to a value that it has already passed by.

    LB=Bracketrooteven(k) %Lower bound surrounding root UB=Bracketrooteven(k+1) %Upper bound surrounding root
    Prec=0.00001; %Precision that the root value must meet
    Intnum=log2((UB-LB)/Prec) %Calculating the number of bisections required
    %for the specified precision
    Int=floor(Intnum)+1 % Interval number in integer form

    for m=1:Int; %Loop for all the interval values

    midval=(LB+UB)/2 %Calculate median value

    if (((tan(((sqrt(2*me*midval*1.6e19))*L/(hbar))))-(sqrt(((V*1.6e-
    19)-(midval*1.6e-19))/(midval*1.6e-19))))<0)
    LB=midval
    else
    UB=midval
    end
    end

    Any help would be greatly appreciated. Thanks
     
Loading...