N1 = (-5:5:5)';N2 = [1:5;2:2:10;primes(11)];N3 = (1:4)'*(11:14);

% SL5
N1(end:-1:1)
N2(:, 2:2:end)
N3(1:2:end, :)
N2(2, [2 4 5])
A = zeros(3, 4), A(:) = 1:12, A = A',

% SL6:
clear; clc;
x = 1:20;
S = cumsum(x);

L = cumsum(x(2:2:20)),

L(end)

% SL5:
A = magic(10);
A([1,end], [1,end]) % C

% SL10:
A = magic(6);
B = ones(6);
A([2:3 5], :) = B([1 1 1], :)

A([2:3 5], :) = B(ones(1, 3), 1:6)

B = magic(5);
A([2:3 5], 1:5) = B(ones(1, 3), :)

% SL11:
M(:, :, 1) = eye(3),
M(:, :, 2) = ones(3),
M(:, :, 3) = diag([2 3 5]),

M(1:2, 1:2, 1) = NaN

% SL26:
clear; clc;        % clear Workspace
N  = 5; V = 40;    % period, sampling
mu = 0; sigma = 1; % normal distribution
C  = sqrt(2*pi); om0 = pi; % signal
t  = linspace(0, N*1, N*V);
s_t = C*sin(2*om0*t) + ...
   (mu + sigma*randn(1, N*V));
plot(t, s_t);


% SL27:
smin = -9/10; smax = pi/2;
sp_t_min = max(s_t, smin);
sp_t     = min(sp_t_min, smax);

close all;
plot(t, s_t); hold on;
stem(t, sp_t, 'r');

% SL35:
%% skript loanRepayment.m
clear; clc;
r = 0.1:.01:0.2; % interest on a loan is vectorized
A = 1e3;
n = 12;
k = 15;

P = r*A.*(1 + r/n).^(n*k) ./ (n.*((1 + r/n).^(n*k) - 1));

disp(P');         % monthly instalments
disp(P'*n*k - A); % overpayment

% SL36:
%% Vectorization of loanRepayment.m for r and k
clear; clc;
r = 0.1:0.01:0.2; % interest on a loan is vectorized
A = 1e3;
n = 12;
k = 10:1:15;
[R, K] = meshgrid(r, k);
P = R*A.*(1 + R/n).^(n*K) ./ (n.*((1 + R/n).^(n*K) - 1));

figure(1);
surf(r, k, P);
xlabel('r');
ylabel('k');
zlabel('P');

% SL39:
%% script loanRepayment.m calculates regular repayment
clear; clc;
r = 0.1:.01:0.2;
A = input('zadejte A = ');
n = input('zadejte n = ');
keyboard; % insert new data (A,n), then type „return“)
k = 15;
P = r*A.*(1 + r/n).^(n*k) ./ (n.*((1 + r/n).^(n*k) - 1));
pause; % program will continue after pressing any key
disp(P'); disp(P'*n*k - A);

% SL43:
X = zeros(1, 45);
X(6:15) = 1;
X(16:25) = 2;
X(25:end) = 2:-2/20:0;
stem(X, 'r');

% or...
X = [zeros(1, 5) ones(1, 10) 2*ones(1, 9) 2:-2/20:0];
stem(X, 'r');

%% SL46

clear;
% TIME VECTOR GENERATION
N = 5;      % number of periods
V = 40;     % no of samples per period
k = 5;      % beginning of the interval
t = linspace(k, N+k, N*V);
% NOISE VECTOR GENERATION
mu    = 0;    % mean
sigma = 1;    % standard deviation
n     = mu + sigma*randn(1, N*V);
% NOISY SIGNAL VECTOR GENERATION
omega = pi;   % angular frequency
s_t   = sqrt(2*pi)*sin(2*omega*t)+n;
% SIGNAL PLOTTING
plot(t, s_t);

%% SL47

clear;
Z0  = 50;                   % reference impedance
R   = 30;                   % real part of the impedance
X   = logspace(0, 3, 1e2);  % reactance vector
Z   = R + 1i*X;             % impedance
S11 = 10*log10(abs((Z-Z0)./(Z+Z0)).^2);  % reflection coeff. in dB
semilogx(X, S11)            % plotting using log. x-axis

