%------------------------------------------------
% simple example of a for loop
%------------------------------------------------
clear all

% make a vector, x, with values from -3 to 3 spaced by 1
x = -3:1:3;

% find length of vector
n = length(x);

% set up a blank array to store calculation in
y = zeros(1,n);

% print some stuff out
disp('BEFORE THE LOOP HAS RUN:')
disp(['x = ',num2str(x)])
disp(['y = ',num2str(y)])
disp(['n = ',num2str(n)])
disp([' '])

% repeat n times, incrementing i from 1 to n
disp('STARTING THE LOOP:')
for i = 1:n
  y(i) = x(i)^2;
  disp(['loop index i   = ',num2str(i),' (out of ',num2str(n),')'])
  disp(['  x(i)         = ',num2str(x(i))])
  disp(['  y(i)=x(i)^2  = ',num2str(y(i))])
end
disp([' '])

% print some stuff out
disp('AFTER THE LOOP HAS RUN:')
disp(['x = ',num2str(x)])
disp(['y = ',num2str(y)])
disp(['n = ',num2str(n)])
disp([' '])

% print a summary of variables
whos
