% history100108.m

% In MATLAB, a large number of independent operations  that can be
% performed all at one time should be done using MATLAB's vectorized
% operations.

% For example, for each point in the vector x=-5:0.001:5 one wants to
% compute the corresponding vector of points y = 1 - tanh(3*x/5) using
% vectorized operations as shown before as method 1.

clear;
x=-5:0.001:5; 
% method 1: vectorized version (much faster and more compact to write)
tic
y1=1-tanh(3*x/5);
toc
% method 2: uses a loop (much slower and more cumbersome to write)
tic
for n=1:length(x), 
    y2(n) = 1 - tanh(3*x(n)/5);
end;
toc
%disp([ x'  y1' y2']);
% Both results, y1 and y2 are identical.
% So you should not use method 2!

% You should only use a loop if the operations depends on the results 
% of other operations and therefore cannot be vectorized (done at the same
% time).
% For example, Newton's method for finding the square root of a positive
% number p is to start with an initial guess x(1) (say given by p/2) for the
% square root, and iterate according to the algorithm:
%   x(k+1) = ( x(k) + p / x(k) ) / 2, for k = 1, 2, ..., kmax
% We compute x(2) from x(1), x(3) from x(2), etc.
% We cannot compute x(k+1) until we have computed x(k).
% Subsequent values are related to previous values: vectorization is
% not possible. We need a loop to perform this:
format long;
p = input('Enter a positive number whose square root you want to compute: ');
x = 0.5 * p;
disp('   Loop                Approx. sq. rt.     Exact result');  
for n = 1:6
    x = 0.5 * ( x + p / x );
    disp([ n x sqrt(p) ]);
end

% MATLAB has two types of loops: the for-loop and the while-loop.
% The for-loop has the following syntax:

% for vec = ...
%     statements
% end
% where vec is a vector of values.
% The statements inside the loop typically involves the variable vec.
% vec uses one of its values in the statements, starting with the first
% element.

% sumSeries1.m
% For a given kmax, compute the sum:
% sum_{n=1}^{kmax} 1/n^2
% using 2 separate ways:
% (1) using a for-loop 
% (2) using the built-in sum function (vectorized)

kmax = 10000000;
% (1) version using a for-loop
s1 = 0;
for n = 1:kmax
    s1 = s1 + 1/n^2;
end

% (2) vectorized version
s2 = sum(1./(1:kmax).^2);

disp(['Difference of the two results is: ' num2str(s1-s2)]);

for k = [ -5 2 9 -1 55]
    disp(k);
end

% One can loop over element of an array.
% The elements do not need to be integers. 
for n = [ pi exp(1) eps ]
    disp(n);
end

% Another example is:
disp('       x      sin(x)'); 
disp('    ================');
for x = [ pi/6 pi/4 pi/3 ], % loop thru each element in vector x
    disp([x, sin(x)]);
end

% Multiple for-loops can be nested (one inside the other),
% in which case indentation helps to improve readability.
% The following code forms the 5-by-5 symmetric matrix A
% with elements i/j for j>=i:
format short;
n = 5; A = eye(n);
for j = 2:n
    for i = 1:j-1
        A(i,j) = i/j;
        A(j,i) = A(i,j);
    end
end
disp('A = ');
disp(A);

% The syntax for the while-loop is:
% 
% while boolean-expression
%     statements
% end
% The boolean-expression is first evaluate to see if it is true or false.
% The statement is executed it the expression is true.
% The expression is test again, and if it is true, the statement is again
% executed. This continues until the expression is tested to be false.
% In a sense, the statement is executed while the boolean expression is
% true.
% The following example approximately finds the smallest positive
% floating point number that Matlab can represent
% (any positive number less than that number is treated as 0):
x = 1;
while x>0,  % this comma can be omitted
    xmin = x;
    x = x/2;
end
xmin,


% Given an arbitrary positive integer n, there is a very famous mathematic 
% conjecture that says that the following iterative algorithm for n will 
% always ends up to be 1.
n = input('Enter a positive integer for n: ');
i = 1;
sequence(i) = n;

while (n ~= 1)
    i = i+1;
    if (rem(n,2) == 0)
        n = n/2;
    else
        n = 3*n+1;
    end
    sequence(i) = n;
end
disp(sequence);

