Thread: Matlab
View Single Post
Old 07-25-2003, 09:43 PM   #28 (permalink)
slant eyes
Psycho
 
Location: cali
so you're saying i should hard code it? or just for the sake of testing purposes.



Code:
%Bisection Method

a = 0;
b = 0;
TOL = 0;
N0 = 0;
p = 0;
i = 1;

a = input('Please enter starting endpoint "a" for the Bisection Method');
b = input('Please enter starting endpoint "b" for the Bisection Method');
TOL = input('Please enter desired tolerance for the Bisection Method');
N0 = input('Please enter maximum number of iterations for the Bisection Method');

FA = f(a);

while i <= N0
    p = a + (b - a) / 2;
    FP = f(p);
    if FP = 0 | (b - a) / 2 < TOL;
        disp(sprintf('%5.6f', p);
    else i = i + 1;
        if FA * FP > 0;
            a = p;
            FA = FP;
        else b = p;
        end
    end
end
disp('Method failed after N0 iterations, N0 =', N0);
Quote:
for one... you should only say that the method has failed AFTER you are done with the loop. check your logic. I'm not going to do that for you.
i changed it, thanks for pointing that out for me. does that make sense now?

Quote:
Use a Boolean variable to keep track of whether the SUCCESS conditions have been met. Terminate the loop when you have succesfully done it. probably easier to do in in a WHILE loop in this case.
there is a boolean checking...

Code:
if FP = 0 | (b - a) / 2 < TOL;
    disp(sprintf('%5.6f', p);
end
Quote:
Store the result as a variable, don't print it out, and make this function output that variable
i thought i was storing it as a variable p, which i do print with the disp(sprintf function...

thanks for your help. oh yeah, does the last function that displays the message using the disp(sprintf make sense? is it wrong?
slant eyes is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73