% 设置三条曲线的参数data
epochs1 = 0:160; % 第一条曲线训练轮次
final_MAPE1 = 0.5819;
epochs2 = 0:148; % 第二条曲线训练轮次
final_MAPE2 = 0.2506;
epochs3 = 0:125; % 第三条曲线训练轮次
final_MAPE3 = 0.0974;
% 公共参数
initial_MAPE = 10; % 初始MAPE值
decay_rate = 0.05; % 衰减率参数
noise_intensity = 0.4; % 噪声强度系数0.8
% 计算理论MAPE值(指数衰减模型)
MAPE_theoretical1 = final_MAPE1 + (initial_MAPE - final_MAPE1) * exp(-decay_rate * epochs1);
MAPE_theoretical2 = final_MAPE2 + (initial_MAPE - final_MAPE2) * exp(-decay_rate * epochs2);
MAPE_theoretical3 = final_MAPE3 + (initial_MAPE - final_MAPE3) * exp(-decay_rate * epochs3);
% 添加随机噪声模拟真实训练波动(收敛阶段减小噪声)
rng(42); % 设置随机种子保证可重复性
% 第一条曲线噪声(收敛阶段减小噪声)
noise1 = noise_intensity * randn(size(epochs1));
converge_start1 = round(0.65 * length(epochs1)); % 60%处开始收敛
noise1(converge_start1:end) = noise1(converge_start1:end) * 0.3; % 收敛阶段噪声减小
MAPE_noisy1 = MAPE_theoretical1 + noise1;
MAPE_noisy1 = max(MAPE_noisy1, 0);
MAPE_noisy1(end) = final_MAPE1; % 确保终点收敛值准确
% 第二条曲线噪声
noise2 = noise_intensity * randn(size(epochs2));
converge_start2 = round(0.6 * length(epochs2));% 60%处开始收敛
noise2(converge_start2:end) = noise2(converge_start2:end) * 0.3;
MAPE_noisy2 = MAPE_theoretical2 + noise2;
MAPE_noisy2 = max(MAPE_noisy2, 0);
MAPE_noisy2(end) = final_MAPE2;
% 第三条曲线噪声
noise3 = noise_intensity * randn(size(epochs3));
converge_start3 = round(0.55 * length(epochs3));% 55%处开始收敛
noise3(converge_start3:end) = noise3(converge_start3:end) * 0.3;
MAPE_noisy3 = MAPE_theoretical3 + noise3;
MAPE_noisy3 = max(MAPE_noisy3, 0);
MAPE_noisy3(end) = final_MAPE3;
% 创建图表
figure('Color', 'white', 'Position', [100, 100, 1000, 700]);
hold on;
% 绘制三条曲线(不同颜色和线型)并添加终点标记
p1 = plot(epochs1, MAPE_noisy1, 'Color', [0, 0.447, 0.741], 'LineWidth', 1.8, ...
'Marker', 'o', 'MarkerSize', 6, 'MarkerFaceColor', [0, 0.447, 0.741], ...
'MarkerIndices', length(epochs1), ... % 只在终点添加标记
'DisplayName', sprintf('MLP'));
p2 = plot(epochs2, MAPE_noisy2, 'Color', [0.85, 0.325, 0.098], 'LineWidth', 1.8, ...
'Marker', 's', 'MarkerSize', 6, 'MarkerFaceColor', [0.85, 0.325, 0.098], ...
'MarkerIndices', length(epochs2), ...
'DisplayName', sprintf('WOA-MLP'));
p3 = plot(epochs3, MAPE_noisy3, 'Color', [0.466, 0.674, 0.188], 'LineWidth', 1.8, ...
'Marker', 'd', 'MarkerSize', 6, 'MarkerFaceColor', [0.466, 0.674, 0.188], ...
'MarkerIndices', length(epochs3), ...
'DisplayName', sprintf('FT-WOA-MLP'));
% 添加收敛值文本标注
text(epochs1(end)+3, final_MAPE1, sprintf('MAPE: %.4f', final_MAPE1), ...
'FontSize', 11, 'FontWeight', 'bold', 'Color', [0, 0.447, 0.741]);
text(epochs2(end)+3, final_MAPE2, sprintf('MAPE: %.4f', final_MAPE2), ...
'FontSize', 11, 'FontWeight', 'bold', 'Color', [0.85, 0.325, 0.098]);
text(epochs3(end)+3, final_MAPE3, sprintf('MAPE: %.4f', final_MAPE3), ...
'FontSize', 11, 'FontWeight', 'bold', 'Color', [0.466, 0.674, 0.188]);
% 设置坐标轴和标签
xlabel('迭代次数(Iterations)', 'FontSize', 13, 'FontWeight', 'bold');
ylabel('平均绝对误差 (MAPE)', 'FontSize', 13, 'FontWeight', 'bold');
title('多模型迭代误差MAPE对比', 'FontSize', 17, 'FontWeight', 'bold');
xlim([0, max([epochs1, epochs2, epochs3]) + 25]);
ylim([-1, initial_MAPE + 5]);
grid on;
box on;
% 添加图例和注释
legend('Location', 'northeast', 'FontSize', 11);
set(gca, 'FontSize', 12, 'FontWeight', 'bold');
set(gcf, 'Color', [0.96, 0.96, 0.96]);
% ===== 创建自适应尺寸的数据表格 =====
model_data = table([epochs1(end); epochs2(end); epochs3(end)], ...
[final_MAPE1; final_MAPE2; final_MAPE3], ...
[converge_start1; converge_start2; converge_start3], ...
'VariableNames', {'迭代次数', '最终MAPE', '收敛起点'}, ...
'RowNames', {'MLP', 'WOA-MLP', 'FT-WOA-MLP'});
% 计算表格尺寸(基于内容自适应)
column_widths = [80, 80, 80]; % 每列宽度(像素)
row_height = 25; % 每行高度(像素)
header_height = 30; % 表头高度(像素)
% 计算表格总尺寸
table_width = sum(column_widths)+122;
table_height = 83.5;
% 获取图形尺寸
fig_pos = get(gcf, 'Position');
fig_width = fig_pos(3);
fig_height = fig_pos(4);
% 计算表格位置(水平居中,距离顶部100像素)
table_x = (fig_width - table_width) / 2;
table_y = fig_height - table_height - 60;
% 创建表格(使用uitable)
t = uitable('Data', model_data{:,:}, ...
'ColumnName', model_data.Properties.VariableNames, ...
'RowName', model_data.Properties.RowNames, ...
'Position', [table_x, table_y, table_width, table_height], ...
'FontSize', 11, ...
'BackgroundColor', [1, 1, 1; 0.95, 0.95, 0.95], ... % 交替行颜色
'ColumnWidth', num2cell(column_widths), ... % 设置每列宽度
'RowStriping', 'on'); % 启用行条纹
hold off;
让迭代过程中的MAPE全程不要低于最终MAPE