clc;
fprintf('Beginning to run %s.m.\n', mfilename);
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 20;
data = readmatrix('Fitnlm_Gaussian_CoVid19.xlsx');
validRows = ~isnan(data(:, 2));
validRows = validRows & (data(:, 2) > 0);
data = data(validRows, :)
X = data(:, 1);
Y = data(:, 2);
hFig = figure;
plot(X, Y, 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
xlabel('Date', 'FontSize', fontSize);
ylabel('Daily Deaths', 'FontSize', fontSize);
title('CoVid-19 Daily Deaths', 'FontSize', fontSize);
datetick('x', 'mmm dd, yyyy', 'keepticks');
ax = gca;
ax.XTickLabelRotation = -45;
tbl = table(X(:), Y(:));
ModelFunction = @(b, x) b(1) + b(2) * exp(-(x(:, 1) - b(3)).^2/b(4));
beta0 = [0, max(X), mean(X), var(X)];
mdl = fitnlm(tbl, ModelFunction, beta0);
coefficients = mdl.Coefficients{:, 'Estimate'}
xFitted = linspace(min(X), max(X), 1920);
yFitted = ModelFunction(coefficients, xFitted(:));
yFitted = max(0, yFitted);
hold on;
plot(xFitted, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('CoVid-19 Daily Deaths in the USA -- Exponential Regression with fitnlm()', 'FontSize', fontSize);
darkGreen = [0, 0.5, 0];
xline(coefficients(3), 'Color', darkGreen, 'LineWidth', 3);
str = sprintf(' Peak at %s', datestr(coefficients(3)));
text(coefficients(3), 100, str, 'Color', darkGreen, 'FontSize', 14, 'FontWeight', 'bold');
legendHandle = legend('Actual Y', 'Fitted Y', 'Location', 'northwest');
legendHandle.FontSize = 25;
hFig.WindowState = 'maximized';
fprintf('Done running %s.m.\n', mfilename);
Best Answer