clc

% Task 3
N = 75;

[x, w, A, B] = FiniteDifferenceGrid (N);

% Code to display the results
%disp(x);
%disp(w);
%disp(A);
%disp(B);

% Task 4

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

%Creating an empty vector
f = zeros(N,1);

% Filling the vector with the log-normal values
for i = 1:N
    f(i,1) = exp(-(log(x(i)) - my)^2/(2*sigma^2))/...
        (x(i)*sigma*sqrt(2*pi));
end
% Need to correct the first value, as it currently is NaN
f(1,1) = 0;


%disp(f)

% Calculating the integral
int_f = sum(w*f);

fprintf('The integral of f between 0 and 1 is: %1.4f \n', int_f)

% Calculating the derivatives
dfdx = A*f;
df2dx2 = B*f;

%Making the plot prettier
figProps = struct('Color', [1 1 1], 'OuterPosition', [170, 170, 1000, 700]);
fontProps = struct('FontName', 'Calibri', 'FontSize', 18, 'FontWeight', 'bold');

fig = figure(1);
ax = axes;
set(ax, 'FontSize', fontProps.FontSize);
hold on;
%Must plot in a subplot due to there being large variation in magnitude of
%the axes
subplot(3,1,1)
plot(x, f, 'Color', 'r', 'LineWidth', 2, 'Marker', 'o', 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'none', 'DisplayName', 'f(x)');
xlim([0 1]);
box("on");
grid("on");
set(legend(),'FontName' , fontProps.FontName)
set(title('Plot of the log-normal distribution with derivatives'), fontProps);
subplot(3,1,2)
plot(x, dfdx, 'Color', 'g', 'LineWidth', 2, 'Marker', 'o', 'MarkerEdgeColor', 'g', 'MarkerFaceColor', 'none', 'DisplayName', 'dfdx');
xlim([0 1]);
box("on");
grid("on");
set(legend(),'FontName' , fontProps.FontName)
subplot(3,1,3)
plot(x, df2dx2, 'Color', 'b', 'LineWidth', 2, 'Marker', 'o', 'MarkerEdgeColor', 'b', 'MarkerFaceColor', 'none', 'DisplayName', 'df2dx2');
set(xlabel('x-values'), fontProps);
xlim([0 1]);
box("on");
grid("on");
set(legend(),'FontName' , fontProps.FontName)
hold off;
set(fig, figProps);
saveas(fig, 'Ex5_plot', 'jpg')

