%==========================================================================
%% SL3 - Function interpolation
clear;
clc;
close all;

%% f(x) = tan(x)
x  = linspace(0, pi, 1e5);
fx = tan(x);

figure;
plot(x, fx, ':', 'LineWidth', 1);
ylim([-60 60]);
grid on;

%% g(x) as thresholding of f(x)
E  = 50;
gx = fx;

gx(gx > +E | gx < -E) = NaN;
hold on;
plot(x, gx, 'r');

%% linear interpolation between extremes
x1 = find(isnan(gx), 1, 'first') - 1;
x2 = find(isnan(gx), 1, 'last') + 1;
hx = fx;

kappa = (hx(x2) - hx(x1)) / (x(x2) - x(x1));
ir = (x1+1):(x2-1); % interpolation region
hx(ir) = fx(x1) + (x(ir) - x(x1)) * kappa;

plot(x, hx, 'k--');

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

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

%% SL17 - factorial
clear;
N = 6;
F = 1;
for n = 1:N
    F = F*n;
end

prod(1:N)
