% problem 2e
clc, clear

% define time step (h) and time
h = 0.01;
time = 0:h:2;

% define solutions states as function handles
x1 = @(t) 1/2 - 21/5.*exp(-3.*t) + 167/20.*exp(-2.*t) - 4.*exp(-t) - 1/4.*exp(2.*t) + 3/5.*(cos(t)+sin(t));
x2 = @(t) -exp(-t);
x3 = @(t) 1/5.*(3.*cos(t)+sin(t)) + 7/5.*exp(-3.*t);
x4 = @(t) 0.*t;

% plot
f1 = figure();
set(f1, 'Position', [100, 100, 1000, 800]);

subplot(2,2,1);
plot(time, x1(time), 'LineWidth', 3, 'Color', 'r');
set(gca, "FontSize", 12);
grid;
title("\textbf{State $x_1(t)$}", "FontSize", 16, "Interpreter", "latex");
xlabel("time $t$ [s]", "FontSize", 16, "Interpreter", "latex");
ylabel("$x_1(t)$", "FontSize", 16, "Interpreter", "latex");

subplot(2,2,2);
plot(time, x2(time), 'LineWidth', 3, 'Color', 'c');
set(gca, "FontSize", 12);
grid;
title("\textbf{State $x_2(t)$}", "FontSize", 16, "Interpreter", "latex");
xlabel("time $t$ [s]", "FontSize", 16, "Interpreter", "latex");
ylabel("$x_2(t)$", "FontSize", 16, "Interpreter", "latex");

subplot(2,2,3);
plot(time, x3(time), 'LineWidth', 3, 'Color', 'g');
set(gca, "FontSize", 12);
grid;
title("\textbf{State $x_3(t)$}", "FontSize", 16, "Interpreter", "latex");
xlabel("time $t$ [s]", "FontSize", 16, "Interpreter", "latex");
ylabel("$x_3(t)$", "FontSize", 16, "Interpreter", "latex");

subplot(2,2,4);
plot(time, x4(time), 'LineWidth', 3, 'Color', 'm');
set(gca, "FontSize", 12);
grid;
title("\textbf{State $x_4(t)$}", "FontSize", 16, "Interpreter", "latex");
xlabel("time $t$ [s]", "FontSize", 16, "Interpreter", "latex");
ylabel("$x_4(t)$", "FontSize", 16, "Interpreter", "latex");

sgtitle("\textbf{Analytical Solution Plots for All States}",...
    "Interpreter", "latex", "FontSize", 18);