%% SL13
% script evaluates two hyperbolic 
%  functions and plot them
x  = linspace(-1, 1, 51);
f1 = sinh(x);
f2 = cosh(x);
plot(x, f1, 'bs-');
hold on;
plot(x, f2, 'rd--');
ylim([-1.5 1.5]);
legend('f_1(x)=sinh(x)', 'f_2(x)=cosh(x) ', ...
'Location', 'best');
xlabel('x'); 
ylabel('f_1(x), f_2(x)');
grid on;

%% SL17
close all; clear; clc;
t1 = -1:0.2:1;
t2 = -1:0.1:1;
t3 = -1:0.01:1;
f1 = t1.*sin(pi/2*(1+20*t1));
f2 = t2.*sin(pi/2*(1+20*t2));
f3 = t3.*sin(pi/2*(1+20*t3));
figure;
hold on; grid on;
plot(t1, f1, 'r');
plot(t2, f2, 'g');
plot(t3, f3, 'b');
legend('0.2', '0.1', '0.01');

%% SL28
L = 1e5;
tosses = randi(8, 1, L);
histogram(tosses);

%% SL30
N = 1e4; % number of series
M = 1e3; % number of throws in one set
throws = randi([0 1], M, N)*2-1; % generate numbers -1 and 1
noOnesOverAverage = sum(throws); % is vector
figure(1);
histogram(noOnesOverAverage, 60); % 60 bins

%% SL34-36
clear; close all; clc;
N      = round(logspace(1, 5, 10)); % number of shots
hits   = nan(1, length(N)); % number of hits
my_pi  = nan(size(N)); % approximations of pi
shots  = 2*rand(N(end), 2) - 1; % generation of shots
R      = hypot(shots(:, 1), shots(:, 2)); % radii
 
for iShot = 1:length(N)  
   hits(iShot)  = sum(R(1:N(iShot)) <= 1);
   my_pi(iShot) = 4*hits(iShot)/N(iShot);
end

figure;
semilogx(N, my_pi, 'x--', 'linewidth',2);
xlim([N(1) N(end)]);
hold on; grid on;
xlabel('shots','FontSize', 15);
ylabel('approximation of \pi','FontSize', 15);
line([N(1) N(end)],[pi pi],'color','r','linewidth',2);
