%% ASS 1
% a)

n = 0;
i = 0;
v = [];
while n <26
    if mod(i, 4) == 3
      v = [v i] ;
      n = n + 1;
    end
    i = i + 1;
end
disp(v)

%
%% b)

u = flip(v(1:5:end))

%% ASS 2
% a)

f = @(x) sin(x).*cos(x).^2; 
x=linspace(0,3, 1000);
plot(x, f(x)) 

fplot(f,[0 3])
title('Title goes here')
xlabel('x'); ylabel('y');
%ylim([-0.1 0.4])
axis([0 3 -0.1 0.4])

% c)

n = [1000:500:4000];
%
%res_left = integrera(f, [0 3], n, "Left")
res_left = [];
for i = n
    res_left = [res_left integrera(f, [0 3], i, "Left")];
end
plot(n, res_left)

%% ASS 3
% Fresnel's integral C(x). Find approximate maximum.
% Somehow use fzero().
% Plot integrand first.

close all % close all figures

a = 0;
b = 5;
n = 200; % n values between a and b.
x = linspace(a,b,200);
fresnelc = zeros(size(x)); % Initialize result vector.
f = @(x) cos(x.^2);

%subplot(211)
plot(x, f(x))
grid on
hold on
root = fzero(f, 0.5) % Find first zero crossing of cos(x^2). Since this is the largest area, and the function oscillates around zero, the integral will be largest here.

for k = 1:n
    fresnelc(k) = integrera(f, [0 x(k)], 100, 'Trapezoid');
end
%subplot(212)
plot(x, fresnelc)
plot(root, integrera(f, [0 root], 100, 'Trapezoid'), 'or')
grid on

% Clearly the maximum is at the first peak. The function subsequently converges at a lower value.
% More precise below vvv

% hold on
% 
% C = zeros(size(x));
% for k=2:n
%     C(k)=C(k-1)+integral(@(t)cos((t.^2)), x(k-1), x(k));
% end
% plot(x, C)