clc
close all

%% Testing the created functions
%% Task 1
N = 5;
% Calling the functioon
x_values = distribution(N);

% Creating an array using linspace
lin_values = linspace(0,1,N);

% Calculating the difference
difference = x_values - lin_values;

%disp(x_values)
%disp(difference)

%% Task 2
% Using the array created from the function in task 1, we can insert it
% into the formula

% Defining the given parameters
my = -0.530;
sigma = 0.136;

%Creating an empty vector
log_norm_vec = zeros([1 N]);

% Filling the vector with the log-normal values
for i = 1:N
    log_norm_vec(i) = exp(-(log(x_values(i)) - my)^2/(2*sigma^2))/...
        (x_values(i)*sigma*sqrt(2*pi));
end

disp(log_norm_vec)

%% Task 3

%% a
% Had to change the numbers in OuterPosition du to screen size
figProps = struct('Color', [1 1 1], 'OuterPosition', [170, 170, 1000, 700]);

%% b
fontProps = struct('FontName', 'Calibri', 'FontSize', 18, 'FontWeight', 'bold');

%% c
plotter(x_values, log_norm_vec, figProps, fontProps);

%% Task 4
%% a
peak = -1;
peak_index = 0;
length = numel(log_norm_vec);

for i = 1:length
    if log_norm_vec(i+1)<log_norm_vec(i)
        peak = log_norm_vec(i);
        peak_index = i;
        break
    end
end
fprintf('For N = %d\n', N)
fprintf('For the peak, the index is %d, x = %f, and y = %f\n', peak_index, x_values(peak_index), peak);

%% b
tailing_edge = -1;
tail_index = 0;

for j = 1:length
    if log_norm_vec(j+1) > 0.001*peak
        tailing_edge = log_norm_vec(j);
        tail_index = j;
        break
    end
end
fprintf('For the tailing edge, the index is %d, x = %f, and y = %f\n', tail_index, x_values(tail_index), tailing_edge);

%% c
leading_edge = -1;
lead_index = 0;

for k = length:-1:1
    if log_norm_vec(k-1) > 0.001*peak
        leading_edge = log_norm_vec(k);
        lead_index = k;
        break
    end
end
fprintf('For the leading edge, the index is %d, x = %f, and y = %f\n', lead_index, x_values(lead_index), leading_edge);

%% Task 1
% Creating the function
function f = distribution(N)
    % Pre-creating the array for the values
    array = zeros([1 N]);
    % Calculating the difference between each value, there are N-1 steps
    diff = 1/(N-1);
    % Looping to fill the array with the wanted values
    for i = 1:N
        array(i) = (i-1)*diff;
    end    
    % Defining what to return in th function
    f = array;
end

%% Task 3
function plotter(x, y, figpro, fontpro)
    fig = figure(1);
    ax = axes;
    % Plotting the values with the given proerties
    plot(x, y, 'Color', 'r', 'LineWidth', 2, 'Marker', 'o', 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'none', 'DisplayName', 'f(x)');
    % Set the properties of the figure to the figpro values
    set(fig, figpro);
    % Set the fontsize of the ax = axes object
    set(ax, 'FontSize', fontpro.FontSize);
    % Set labels and title, with the properties defined in fontProps
    set(xlabel('x-values'), fontpro);
    set(ylabel('y-values'), fontpro);
    set(title('Plot of the log-normal distribution'), fontpro);
    % Setting limits on the x-axis
    xlim([0 1]);
    % Place a border around the plot
    box("on");
    % Turn on gridlines
    grid("on");
    % Add a legend and set its font to the font in fontProps
    set(legend(), 'FontName', fontpro.FontName)
    %saveas(fig, 'Ex4_plot', 'jpg')
end


