%% SL10 + SL11 - script loanRepayment.m
clear; clc;
r = 0.1:0.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

%% SL12 - script loanRepaymentVectorized.m
clear; clc; close all
r = 0.1:0.01:0.2;
A = 1e3;
n = 12;
k = 1:15;

[R, K] = meshgrid(r, k);

P = R*A.*(1 + R/n).^(n*K) ./ ...
      (n.*((1 + R/n).^(n*K) - 1));

surf(R, K, P)
xlabel('r (-)');
ylabel('k (years)');
zlabel('P (-)');

%% SL14 script loanRepayment.m calculates regular repayment
clear; clc;
r = 0.1:.01:0.2;
A = input('insert A = ');
n = input('insert 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);

%% SL 24: 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

%% SL25 
2 > 1 & 0 % Matlab evaluates expressions from left to right

r = 1/2;
0 < r < 1 % Matlab evaluates expressions from left to right

(1 > A) <= true %result of the bracket is false/true (0/1) overall result is always true

%% SL28 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)]

%% SL30 Logical operators
K = 1:3:20;
L = K(K > 10 & K < 16

%% SL31 Logical operators
M = magic(3);
all(M > 2)
any(M.' >= 8)
all(all(M > 0))

%% SL32 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

%% SL39
N = 5; V = 40;
t = linspace(0, N, N*V);
s_t = randn(1, N*V) + ...
sqrt(2*pi)*sin(2*pi*t);
plot(t,s_t);
hold on

s_t(s_t > pi/2)  = pi/2;
s_t(s_t < -9/10) = -9/10;
stem(t,s_t);

%% SL40
A = [1 1 2; 2 3 5];
if all(all(A > 0)) & all(mod(A(1, :), 1) == 0)       
    disp('yep');
end

% compare with
if all(all(A > 0)) && all(mod(A(1, :), 1) == 0)       
    disp('yep');
end

