%% Exercises from presentation Part #5

%% SL 8: Program branching – switch / case
% create a script that, given lengths of two sides of a right triangle,
% calculates the length of the third side (Pythagorean theorem) two sides are
% known together with string marking the type of unknown side ('leg' for leg or
% 'hyp' for hypotenuse)
s1   = 4; % for experiment purposes, choose so-called
s2   = 3; % Pythagorean triangle {3,4,5}
type = 'hyp';
switch type
   case 'hyp'
      c = sqrt(s1^2 + s2^2)
   case 'leg'
      a = sqrt(max(s1,s2)^2 - min(s1,s2)^2)
   otherwise % unknown type
      c = 0, a = 0,
end

%% SL 23:
x  = linspace(-1, 1, 1e2+1);
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;

%% SL 27: sampling
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');

%% SL 28: rounding
close all; clear; clc
x = -(3/2)*pi:pi/100:(3/2)*pi;
y = tan(x);
z = y.*(abs(y) < 1e10);
figure;
plot(x, y);
hold on;
plot(x, z, '--r', 'LineWidth', 2);
axis([-(3/2)*pi, (3/2)*pi, -40, 40]);
legend('Original', 'Limited values');
xlabel('x [rad]');
ylabel('tan(x) [-]');

%% SL 38: Exercise #1
L = 1e5;
tosses = randi(8, 1, L);
histogram(tosses);

%% SL 40
N = 1e4; % number of series
M = 1e3; % number of throws in one set
throws = randi([-1 1], M, N); % generate numbers (0,1)
noOnesOverAverage = sum(throws); % is vector
figure(1);
histogram(noOnesOverAverage);

N = 1e4; % economy code
histogram(sum(randi([-1 1], N/10, N)));

figure(2);
histogram(0 + 15.7743*randn(N,1));

%% SL 45:
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);

%% SL 49
close all; clear; clc;
tic;
n = 1;
err = 200;
tolerance = 1e-6;
my_pi = 4;
while err > tolerance
   my_pi = my_pi + (4*(-1)^n)/(2*n+1);
   err = abs(my_pi-pi);
   n = n + 1;
end
time = toc;
disp(['Pi was approximated with tolerance ', ...
   num2str(tolerance), ' in ', num2str(n), ...
   ' cycles in ', num2str(time), ' s.']);

%% SL 51:
close all; clear; clc;
tic;
n = 0;
err = 200;
tolerance = 1e-6;
my_pi = 0;
while err > tolerance
   my_pi = my_pi + 8*1/((4*n+1)*(4*n+3));
   err = abs(my_pi-pi);
   n = n + 1;
end
time = toc;
disp(['Pi was approximated with tolerance ', ...
   num2str(tolerance), ' in ', num2str(n), ...
   ' cycles in ', num2str(time), ' s.']);

%% SL 53
close all; clear; clc;
tic;
err = 200;
tolerance = 1e-6;
n = 0;
sum1 = 0; sum2 = 0; sum3 = 0;
while err > tolerance
   sum1 = sum1 + (-1)^n*((1/8)^(2*n+1)/(2*n+1));
   sum2 = sum2 + (-1)^n*(((1/57)^(2*n+1))/(2*n+1));
   sum3 = sum3 + (-1)^n*(((1/239)^(2*n+1))/(2*n+1));
   my_pi = 4*(6*(sum1) + 2*(sum2)+(sum3));
   err = abs(my_pi-pi);
   n = n + 1;
end
time = toc;
disp(['Pi was approximated with tolerance ', ...
    num2str(tolerance), ' in ', num2str(n), ...
    ' cycles in ', num2str(time), ' s.']);


%% SL 55:
% Fourier series of square signal
close all; clear; clc;
t = -1.1:0.01:1.1;
s = nan(1, length(t));
T = 1; A = 1;
figure;
hold on; grid on; axis tight;
xlabel('t');
ylabel('s');
for k = 0:10
   s = s + A*4/pi* ...
      sin(2*pi*t*(2*k+1)/T)/(2*k+1);
   plot(t, s);
end


