% testIf.m
% Script to test the use of the if-elseif structure.
% K. Ming Leung, September 24, 2008

clear;
x = 1.973655172837123;
if ( x * (1/x) == 1)
    disp('For this value of x, x * (1/x) is equal to 1.');
else
    disp('For this value of x, x * (1/x) is NOT EQUAL to 1!!!');
end
% You will see the display: NOT OK for this value of x!

% A number-guessing game:
% The program randomly chooses an integer from 0 to 9: 
answer = floor( 10*rand);
% The player enters his choice of a number:
guess = input('Enter a number as your guess:');
% Program displays "Too high" if the number is higher than the answer,
% "Too low" if the number is lower than the answer, and "Correct answer"
% if the guess is correct.
% Depending on the choice of the player, there are 3 and only 3 possible
% cases and they are mutually exclusive cases (1 and only 1 of those cases
% can be true).
% The structure to handle mutually exclusive cases if the
% if-elsif structure.
% Example:
if (guess > answer)
    disp('Your guess is too high.');
elseif (guess < answer)
    disp('Your guess is too low.');
else
    disp('You have the right answer!');
end

% Another example:
% Given an exam score, we want to assign a letter grade:
r = rand;
score = floor(100*r); % generate randomly a score between 0 to 99
if score >= 90
    grade = 'A';
elseif (score >=80) % don't write: ( score >= 80 & score < 90 )
    grade = 'B';
elseif (score >=70)
    grade = 'C'
elseif (score >= 60)
    grade = 'D';
else    % no Boolean expression that needs to be checked! 
    grade = 'F'
end
disp(['The randomly generated score translates to a grade of: ' grade])
    
% Recall a computer can only represent a limited finite number of real
% numbers. Those that can be represented are called floating-point numbers.
% There is an infinite numbers of real numbers that cannot be represented
% exactly by a computer, they are represented approximately by the 
% nearest floating number. This is the cause of rounding error in
% numerical computations.
% To illustrate the roundoff error, create the following plot and use
% the zoom in feature to inspect the graph very near the origin.
% You need to zoom in several times to see the effect of rounding.
x=0:0.000001:1;
y = (1-cos(x))./x.^2;
plot(x,y);
title('Zoom in the region near the origin to see wacky behavior due to cancellation.');
% When x is very small, cos(x) is very close to unity.
% The numerator become a very small number, but so does the denominator.
% The problem is that in the numerator, we are subtract two numbers
% that are extremely close to each other, the resulting value suffers 
% tremendous loss of precision.
% This phenomenon is called catatrophic cancellation, and it should be
% avoided in performing high-precision calculations.
    
% For any machine, there is a constant called the machine number: eps
% that determines how precise a numeric calculation can be.
% In MATLAB, eps, with no arguments, 
% is the distance from 1.0 to the next larger double
% precision floating-point number, 
% it has a value of 2^(-52), which is about 2.2e-16.
 
    
    
    




