% Jake Bobowski
% July 27, 2017
% Created using MATLAB R2014a

% This tutorial will introduce simple implementations of for loops.

clearvars

% Here's the simplest for loop.
for i = 1:10
    i
end;

% The loop can be made to increment in steps of two as follows:
for i = 1:2:10
    i
end;


% Often, you may wish to perform calculations within a loop and then store
% the results of those calculations in a vector.

% The first step is to determine how many interations your loop with
% execture.  This tells you the length of the vector you need to construct.
% Let's imagine that our loop will interate from 0 to 1.8 in steps of 0.1.
% We can build a vector of these values, called x, as follows:
x = 0:.1:1.8;

% The length of x tells us how big our results vector needs to be.
n = length(x);

% We perpare a vector of right length that is filled with zeros.  If we
% enter zeros(n) we get an n x n square matrix of zeros.  Entering zeros(n,
% 1) results in a column vector of zeros of length n, while entering 
% zeros(n, 1)' gives a row vector of n zeros.
results = zeros(n, 1)'

% We can now index our results vector within a for loop and assign values
% to each element of the vector and then display the results vector.
umax = 0.54; 
M = -4.2703; 
D = 1.8;
for i = 1:n;
  results(i) = (umax/M)*log(1 + (exp(M)-1)*(x(i)/D)*exp(1-x(i)/D));
end
results

% In fact, we can now easiy plot results vs x.
plot(x, results, 'bo')
xlabel('x');
ylabel('results');
axis([0 1.8 0 0.6])