-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_comparison_results.m
More file actions
95 lines (83 loc) · 3.75 KB
/
plot_comparison_results.m
File metadata and controls
95 lines (83 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function plot_comparison_results(results_list, labels, Ref, u_min, u_max, dist_time, dist_amp, tf, save_prefix)
if nargin < 9
save_prefix = '';
end
num_experiments = numel(results_list);
colors = lines(num_experiments);
set(0, 'DefaultAxesFontSize', 12);
set(0, 'DefaultTextFontSize', 14);
%% Create save directory
if ~isempty(save_prefix)
base_dir = fullfile(getenv('USERPROFILE'), 'Downloads', 'simulation_results');
full_dir = fullfile(base_dir, save_prefix);
if ~exist(full_dir, 'dir')
mkdir(full_dir);
end
end
%% Plot 1: Output and Control Input
fig1 = figure;
subplot(2,1,1); hold on;
for i = 1:num_experiments
plot(results_list{i}.time, results_list{i}.Y, 'Color', colors(i,:), 'LineWidth', 1.5);
end
plot(results_list{1}.time, Ref, 'k', 'LineWidth', 1);
if dist_amp ~= 0, xline(dist_time, 'k--', 'Label', 'Dist Start', 'LabelVerticalAlignment', 'bottom', 'LineWidth', 1); end
ylabel('y'); title('System Output Comparison');
legend([labels(:); "Reference"]); grid on;
xlim([0, tf-0.2]); ylim([-3, 4]);
subplot(2,1,2); hold on;
for i = 1:num_experiments
stairs(results_list{i}.time, results_list{i}.U, 'Color', colors(i,:), 'LineWidth', 1.5);
end
if dist_amp ~= 0, xline(dist_time, 'k--', 'Label', 'Dist Start', 'LabelVerticalAlignment', 'bottom', 'LineWidth', 1); end
if isfinite(u_max), yline(u_max, 'k--'); end
if isfinite(u_min), yline(u_min, 'k--'); end
ylabel('u'); xlabel('Time [s]');
title('Actual Control Input Comparison');
legend([labels(:); "u_{max}"; "u_{min}"]); grid on;
xlim([0, tf-0.2]); ylim([-7, 7]);
if ~isempty(save_prefix)
saveas(fig1, fullfile(full_dir, 'comparison_output_input.fig'));
end
%% Plot 2: Virtual Control and Δu
fig2 = figure;
subplot(2,1,1); hold on;
for i = 1:num_experiments
stairs(results_list{i}.time, results_list{i}.V, 'Color', colors(i,:), 'LineWidth', 1.5);
end
if dist_amp ~= 0, xline(dist_time, 'k--', 'Label', 'Dist Start', 'LabelVerticalAlignment', 'bottom', 'LineWidth', 1); end
ylabel('v'); title('Virtual Control Input Comparison');
legend(labels); grid on; xlim([0, tf-0.2]);
subplot(2,1,2); hold on;
for i = 1:num_experiments
stairs(results_list{i}.time, results_list{i}.DU, 'Color', colors(i,:), 'LineWidth', 1.5);
end
if dist_amp ~= 0, xline(dist_time, 'k--', 'Label', 'Dist Start', 'LabelVerticalAlignment', 'bottom', 'LineWidth', 1); end
ylabel('\Delta u'); xlabel('Time [s]');
title('Control Input Increment Comparison');
legend(labels); grid on; xlim([0, tf-0.2]);
if ~isempty(save_prefix)
saveas(fig2, fullfile(full_dir, 'comparison_virtual_du.fig'));
end
%% Plot 3: States
fig3 = figure;
subplot(2,1,1); hold on;
for i = 1:num_experiments
plot(results_list{i}.time, results_list{i}.X(:,1), 'Color', colors(i,:), 'LineWidth', 1.5);
end
plot(results_list{1}.time, Ref, 'k', 'LineWidth', 1);
if dist_amp ~= 0, xline(dist_time, 'k--', 'Label', 'Dist Start', 'LabelVerticalAlignment', 'bottom', 'LineWidth', 1); end
ylabel('x_1'); title('State x_1 Comparison');
legend([labels(:); "Reference"]); grid on;
xlim([0, tf-0.2]);
subplot(2,1,2); hold on;
for i = 1:num_experiments
plot(results_list{i}.time, results_list{i}.X(:,2), 'Color', colors(i,:), 'LineWidth', 1.5);
end
if dist_amp ~= 0, xline(dist_time, 'k--', 'Label', 'Dist Start', 'LabelVerticalAlignment', 'bottom', 'LineWidth', 1); end
ylabel('x_2'); xlabel('Time [s]');
title('State x_2 Comparison'); grid on; xlim([0, tf-0.2]);
if ~isempty(save_prefix)
saveas(fig3, fullfile(full_dir, 'comparison_states.fig'));
end
end