%% slide 6
function f = fibonacci(limit)
%% Fibonacci sequence
f = [1 1]; pos = 1;
while f(pos) + f(pos + 1) < limit
   f(pos + 2) = f(pos) + f(pos + 1);
   pos = pos + 1;
end
end

%% slide 11
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

doc band_wheel
help band_wheel
type band_wheel
lookfor band_wheel

%% slide 17
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

%% slide 21
close all; clear; clc
[x1, f1] = myFunc(-1:0.2:1);
[x2, f2] = myFunc(-1:0.1:1);
[x3, f3] = myFunc(-1:0.01:1)

figure;
hold on; grid on;
plot(x1, f1, 'r');
plot(x2, f2, 'g');
plot(x3, f3, 'b');
legend('0.2', '0.1', '0.01');

function [x, f] = myFunc(x)
    f = x .* sin(pi/2*(1+20*x));
end

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

%% slide 44
function pos = binarySearch(array, value)
left = 1;
right = length(array);
while left <= right
   mid = left + round((right - left)/2);
   if array(mid) == value
      pos = mid;
      return
   elseif array(mid) < value
      left = mid + 1;
   else
      right = mid - 1;
   end
end
pos = -1;
end

%% slide 61
t = 0:0.001:1;
g = my_func(t);
plot(t, I)
myFunc_hndl = @my_func;
G = integral(myFunc_hndl, t(1), t(end))

function I = my_func(t)
f = 50;
I = 10*cos(2*pi*f*t) + 5*cos(4*pi*f*t);
end

clear; close all; clc;
t_min = 0; t_max = 1;
f = 50;
g_hndl = @(t) 10*cos(2*pi*f*t) + 5*cos(4*pi*f*t);
G = integral(g_hndl, t_min, t_max)

%% slide 66
function fx = optim_fcn(x)
fx = x^3 + x - 3;
end

clear; close all; clc;

xk1 = 5;
err = 0.0001;
xk  = 10*xk1/(1+err);
k   = 1;
K   = 20;
while (abs((xk1-xk)/xk)  >= err) && (k < K)
    xk  = xk1;
    fx  = optim_fcn(xk);
    delta = 10*eps(xk);
    dfx = (optim_fcn(xk + delta) - optim_fcn(xk - delta))/(2*delta);
    xk1 = xk - fx/dfx;
    disp([k xk1 fx]);
    k = k + 1;
end

function [x0, k] = newton_method(xk1, fcn, err, K)
xk    = 10*xk1/(1+err);  % error high enough so the while cycle is entered
k     = 1;               % first step
while (abs((xk1-xk)/xk) >= err) && (k < K)
    xk  = xk1;
    delta = 10*eps(xk);
    fx  = feval(fcn, xk);
    dfx = (feval(fcn, xk + delta) - feval(fcn, xk-delta)) / (2*delta);
    xk1 = xk - fx/dfx;
    disp([k xk1 fx]);
    k = k + 1;
end
x0 = xk1;
end

%% slide 68
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 ... relative 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

%% slide 71
function I = integral_approx(f, a, b, N)
x = linspace(a, b, N + 1);
fx = f(x);
S = (fx(1:end-1) + fx(2:end)) .* ...
    (x(2:end) - x(1:end-1));
I = 0.5 * sum(S);
end

f = @(x) sqrt(2.^x + 1);
I = integral_approx(f, 0, 5, 10)
integral(f, 0, 5)