%% SL 5
%% script calculates factorial of N
clear;

N = 6;
F = 1;
for n = 1:N
    F = F*n;
end

prod(1:N)

%% SL 9
%% script calculates sum from 1 by 1 to 100
clear;

N = 100; % calculate up to 100
k = 1;   % start with 1
s = 0;   % allocation for final sum
while k <= N
   s = s + k; % add current value to the sum
   k = k + 1; % move to the next value
end

%% SL 13
%% script fills a matrix
close all; clear;
M = 100;
N = 20;
A = nan(M, N);
for m = 1:M
    for n = 1:N
        A(m,n) = (m*n)/4 + m/(2*n);
    end
end
pcolor(A); % plot

%% SL 16
%% script shows current on a half-wave dipole
clear; close all;
N   = 101;
om0 = 2*pi;
x   = linspace(-pi/2, pi/2, N);
t   = linspace(0, 4*pi, N);
I0  = cos(x);
for n = 1:N
    I = I0*exp(1j*om0*t(n));
    figure(1);
    plot(x, real(I));
    axis([x(1) x(end) -1 1]);
    pause(0.1);
end

%% SL 26
r = 2*rand(8, 1)-1;
Pos = []; % allocation
Neq = []; % allocation
for n = 1:length(r)
    if r(n) >= 0
        Pos(end+1) = r(n); 
    else 
        Neq(end+1) = r(n); 
    end
end

%% SL 29

s1   = 4; % for experiment purposes, choose so-called
s2   = 3; % Pythagorean triangle {3,4,5}
type = 'hyp'; 
switch type
  case 'hyp'
      c = sqrt(s1^2 + s2^2)
  case 'leg'
      a = sqrt(max(s1,s2)^2 - min(s1,s2)^2)
  otherwise % unknown type
      c = 0, a = 0,
end

%% SL 30
% any()

%% SL 31
% all(all())

%% SL 36
% fibonacci sequence
f = [0 1]; % first two members
n = 1;     % index for series generation
limit = 1000;
while f(n) + f(n+1) < limit
    f(n+2) = f(n) + f(n+1);
    n = n + 1;
end
plot(f, '-o');
xlabel('Element n.')
ylabel('Fibonacci Numbers')
