%% SL. 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

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

%% SL. 61

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

t = 0:0.001:1;
g = my_func(t);
plot(t, I)
myFunc_hndl = @my_func;
G = integral(myFunc_hndl, t(1), 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)

%% SL. 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