Quote:
Originally posted by slant eyes
ok, here is one algorithm. forgive me if it's newb-ish.
Code:
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);
for i = i:N0
p = a + (b - a) / 2;
FP = f(p);
if FP = 0 | (b - a) / 2 < TOL;
disp(sprintf('%5.6f', p);
end
else i = i + 1;
if FA * FP > 0;
a = p;
FA = FP;
else b = p;
disp('Method failed after N0 iterations, N0 =', N0);
end
|
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.
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.
Store the result as a variable, don't print it out, and make this function output that variable.
I normally code the inputs into the function itself, so that its easy rerunning the function in the command window if it doesnt work. you just press the 'up' button.