%% SL11:
% script loanRepayment.m
clear; clc;

%% definitions of variables
r = 0.1:0.01:0.2;
A = input('input A = ');
n = input('input n = ');
k = 15;

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

%% dislay results
disp(P.'); disp(P.'*n*k - A);

%% SL13:
% 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 (-)');

%% SL15:
% script loanRepayment.m 
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);

%% SL25: 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 29: 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)]

%% SL31
K = 1:3:20;
L = K(K > 10 & K < 16)

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

%% SL33
a = 5;
b = 3;
b ~= 0 && a/b > 3

%% SL40
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);
hold off;
