%% Exercises from presentation Part #4

%% SL 12: Relational operators
% having the vector G
G = [pi/2 pi 3/2*pi 2*pi]
% greater than pi
G > pi
% lesser or equal to pi
G <= pi
% not equal to pi
G ~= pi

% try similar operations for H = G'
H = G'; 
% greater than pi
H > pi
% lesser or equal to pi
H <= pi
% not equal to pi
H ~= pi

% find out whether V >= U for V = [-pi pi 1 0] and U = [1 1 1 1]
V = [-pi pi 1 0]
U = [1 1 1 1]
V >= U

%% SL 16: Logical operators
% determine which of the elements of the vector A
A = [pi/2 pi 3/2*pi 2*pi]
% are equal to pi or are equal to 2*pi
A == pi | A == 2*pi
% are greater than pi/2 and at the same time are not equal 2*pi
A > pi/2 & A ~= 2*pi
% elements from the previous condition add to vector A
A = [A A(A > pi/2 & A ~= 2*pi)]

%% SL 18: Logical operators
% create a row vector in the interval from 1 to 20 with step of 3
% create the vector filled with elements from the previous vector that are 
% greater than 10 and at the same time smaller than 16; use logical operators
K = 1:3:20;
L = K(K > 10 & K < 16)

%% SL 19: Logical operators
% create matrix M = magic(3) and find out using functions all and any
M = magic(3);
% in which columns all elements are greater than 2
all(M > 2)
% in which rows at least one element is greater than or equal to 8
any(M' >= 8)
% whether the matrix A contains positive numbers only
all(all(M > 0))

%% SL 20: Logical operators
% test whether variable b is not equal to zero and then test whether at the same
% time a / b > 3
b ~= 0 && a/b > 3

%% SL 24: Loops #1
% create a script to calculate factorial N!
close all; clear; clc;
N = 6;
F = 1;
for n = 1:N
    F = F*n;
end
% can you come up with other solutions? (e.g. using vectorising…)
prod(1:N)

%% SL 28: Loops #2
% calculate the sum of integers from 1 to 100 using while cycle
close all; clear; clc;
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
% are you able to come up with another solution (using a Matlab function and 
% without cycle)?
sum(1:N)

%% SL 32: Loops #3
% fill in the matrix using loops A(m,n)=(m*n)/4 + m/(2*n)
close all; clear; clc;
% inputs
M = 100;
N = 20;
% allocation of memory
A = nan(M, N);
% filling of matrix
for m = 1:M
    for n = 1:N
        A(m,n) = (m*n)/4 + m/(2*n);
    end
end
pcolor(A)

%% SL 33: Loops #4
% in the previous task the loops can be avoided entirely by using vectorisin
close all; clear; clc;
M = 100;
N = 20;
[MV, NV] = meshgrid(1:M, 1:N);
A = (MV.*NV)/4 + MV./(2*NV);
pcolor(A');

%% SL 34: Loops #5
% visualize current distribution of a dipole antenna described as
%                                                  I = cos(x) * exp(1j*2*pi*t)
% in the interval t = (0, 4*pi), x = (-pi/2, pi/2) 
close all; clear; clc;
% constants
N   = 101;
om0 = 2*pi;
x   = linspace(-pi/2, pi/2, N);
t   = linspace(0, 4*pi, N);
I0  = cos(x);
%  time dependence
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 36: Loops #6
% try to write moving average code applied to following function
%                                            f  = sin(x).^2.*cos(x) + 0.1*r(x)
% where r(x) is represented by function of uniform distribution (rand())
close all; clear; clc;
signalSize = 1e3;
x  = linspace(0, 4*pi, signalSize);
f  = sin(x).^2.*cos(x) + 0.1*rand(1, signalSize);
windowSize = 15;
my_f = [zeros(1, windowSize-1) f] / windowSize;
my_averaged = nan(1, signalSize);
for thisWindow = 1:signalSize
   my_averaged(thisWindow) = ...
                 sum(my_f(thisWindow:thisWindow+windowSize-1));
end
plot(x, f, x, my_averaged);

%% SL 36: Loops #7
% for comparison it is possible to use Matlab built-in function filter
F          = ones(1, windowSize)/windowSize;
filtered_f = filter(F, 1, f);
hold on;
plot(x(1:15:end), filtered_f(1:15:end), 'xg');

%% SL 44: Program branching – if / else / elseif
% generate random numbers r = 2*rand(8, 1)-1;
% save the numbers in vectors Neq and Pos depending on whether each number is 
% negative or positive; use  for cycle, if–else statement and indexing for 
% storing values of r
% allocation
Pos = [];
Neq = [];
for n = 1:length(r)
    if r(n) >= 0
        Pos(end+1) = r(n); 
    else 
        Neq(end+1) = r(n); 
    end
end
% can you come up with a more elegant solution? (for cycle is not always 
% necessary)
Pos = r(r >= 0), Neq = r(r < 0)

%% SL 45: Program branching – if / else / elseif
% write a script generating a complex number and determining to what quadrant 
% the complex number belongs to
clear; close all; clc
v = rand(1) + 1j*rand(1) - 1/2 - 1j/2;
if real(v) >= 0 % I. or IV. quadrant
    if imag(v) >= 0
        disp('1st quadrant');
    else
        disp('4th quadrant');
    end
else         % II. or III. quadrant
    if imag(v) >= 0
        disp('2nd quadrant');
    else
        disp('3rd quadrant');
    end
end    

%% SL 47: Exercise #1
% try again to limit the signal by values smin a smax
% use relational operators (> / <) and logical indexing (s(a>b) = c) instead of 
% functions max, min
N = 5; V = 40;
t = linspace(0, N, N*V);
s_t = randn(1, N*V) + sqrt(2*pi)*sin(2*pi*t);
s_t(s_t > pi/2)  = pi/2;
s_t(s_t < -9/10) = -9/10;
stem(s_t);

%% SL 48, 49: Exercies #2, #3
% draft a script to calculate values of Fibonacci sequence up to certain value 
% limit. Plot the resulting series using function plot
%% 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);
% try to find out the relation of the series to the value of golden ratio and
% try to calculate it:
goldenRation = f(end)/f(end-1);

%% SL 50: Exercies #4
% consider following matrix: A = [1 1 2; 2 3 5];
% write a condition testing whether all elements of A are positive and at the 
% same time all elements of the first row are integers. if the condition is 
% fulfilled display the result using disp
A = [1 1 2; 2 3 5];
if all(all(A > 0)) & all(mod(A(1, :), 1) == 0)       
    disp('yep');
end

%% SL 51-53: Exercise #5-#7
close all; clear, clc
% straightforward but quite slow solution
tic
ord = 7;
for m = 1:ord 
    A(m) = 10^(m); 
    P    = primes(A(m)); 
    B(m) = length(P); 
%     disp(m); 
    clear P; 
end
toc
loglog(A, B, 'ro-');

tic
ord = 7;
A   = 10.^(1:ord);
B   = nan(1, ord);
P   = primes(10^ord); 
for m = 1:ord 
    B(m) = sum(P < 10^m); % true values are summed
%     disp(m); 
end
toc
loglog(A, B, 'ro-');