%% SL 9
function L = band_wheel(d1,d2,d)
%% band_wheel: calculation of band-wheel length
% 
% Syntax:
%     [L] = band_wheel(d1,d2,d);
% Example:
%     L = band_wheel(4,8,12)
% Input:
%     d1 ... diameter of 1st wheel
%     d2 ... diameter of 2nd wheel
%     d  ... distance between wheels
% Output:
%     L  ... length of band-wheel
% 
% ver. 2, 03-2013, miloslav.capek@fel.cvut.cz
 
r1  = d1/2;                  % radius of 1st wheel
r2  = d2/2;                  % radius of 2nd wheel
L2  = sqrt(d^2 - (r2-r1)^2); % length of l2 segment
phi = asin((r2 - r1)/d);     % angle phi
om1 = pi - 2*phi;
om2 = pi + 2*phi;
 
L1  = om1*r1;     % length of 1st arc
L3  = om2*r2;     % length of 2nd arc
L   = L1 + 2*L2 + L3; % overall length
end

%% SL 21

function [out1, out2] = fibonacciFcn(in1, in2)
% documentation is here!!!
nArgIn = nargin;
if nArgIn == 0
    limit = 1e3;
    f     = [0, 1];
elseif nArgIn == 1
    limit = in1;
    f     = [0, 1];
elseif nArgIn == 2
    limit = in1;
    f     = in2;
end

n = 1;     % index for series generation
while f(n+1) < limit
    f(n+2) = f(n) + f(n+1);
    n = n + 1;
end
f(end) = [];

out1 = f;
if nargout == 2
    out2 = plot(f);
end
end

%% SL 31
A = @(p) [cos(p).^2, sin(p)+cos(p), 1*ones(size(p))]
p = linspace(0, 2*pi)';
plot(p, A(p));

%% SL 32
x   = 1.15;
N   = 10;
n   = 0:N;
f   = (x.^n)./factorial(n);
f1  = sum(f), 
f2  = exp(x),
err = 100*abs(1 - f1/f2)

%% 34
function [f1, f2, err] = exp_approx(x, N)
%% exp_approx: calculates an aproximation of exp(x)
% [f1, f2, err] = exp_approx(x,N)
% x ... function argument, N ... order of approximation
% f1 ... calculated value
% f2 ... „exact“ value (given by built-in function)
% err ... relativ. error
% example: [fa, fp] = exp_approx(4, 30);

n   = 0:N;
f   = (x.^n)./factorial(n);   % it is vectorized
f1  = sum(f);                 % approximative value
f2  = exp(x);                 % exact value
err = 100*abs(1 - f1/f2);     % relative error (in [%])
end

%% SL 35
clear; close all;
Order  = 1:10;
x      = 0.9;
Error  = zeros(size(Order));
for mErr = 1:length(Order)
    [~,~,Error(mErr)] = exp_approx(x, Order(mErr));
end
plot(Order, Error);
% semilogy(Order, Error);
xlabel('N (-)'); ylabel('Error (%)');

%% SL 44
function out = test_function(rek)
%% for testing purposes

rek = rek + 1
out = test_function(rek);
end

test_function(0)