SUNY Geneseo Department of Mathematics
Wednesday, November 18
Math 230 01
Fall 2015
Prof. Doug Baldwin
bisect( f, xl, xu, tolerance )
xm = (xl + xu) / 2 % In case (xu-xl)/2 > tolerance at the very beginning
while ( xu - xl ) / 2 > tolerance
xm = (xl + xu) / 2
if f(xm)*f(xl) < 0
xu = xm
else if f(xm) * f(xl) > 0
xl = xm
else % f(xm) * f(xl)= 0
xu = xm % Force loop to exit
xl = xm
end of while
return xm
if condition1
statements1
elseif condition2
statements2
…
else
statements
end
if condition1
statements1
end
if condition2
statements2
end
…
elseif
and else
only executes the code that goes with
the first true condition% Demonstrations of Matlab's "if" statement
%% Classify numbers as positive vs negative vs zero
x = input( 'Enter a number ' );
if x > 0
fprintf( 'Yay, you found a positive number!\n' );
elseif x < 0
fprintf( 'Oh no, you gave me a negative number!\n' );
else
fprintf( 'Finally, you gave me zero\n' );
end
%% Only divide by x if x not equal to 0
x = input( 'Enter a number ' );
if x ~= 0
fprintf( 'Reciprocal of %f is %f\n', x, 1/x );
end