在同一图中绘制两条拟合曲线 (MATLAB)

Plotting two fitted curves in the same plot (MATLAB)

我正在尝试在 Matlab 上使用两项高斯模型绘制两条拟合曲线。在分别绘制拟合曲线时,这些图显示了整个拟合曲线,但是当我将两条拟合曲线组合在一个图中时,仅显示了一部分曲线,其余曲线被截断。

代码:

% Data     
X1 = [0 1 2 3 4 5 6 7 8 9 10]';
Y1 = [0.004 0.010 0.025 0.036 0.057 0.061 0.044 0.032 0.039 0.024 0.020]';
X2 = X1;
Y2 = [0.012 0.018 0.032 0.046 0.067 0.071 0.054 0.042 0.025 0.019 0.011]';

[p1,p2,p3] = deal(0.04, 3.5, 1.6);
[q1,q2,q3] = deal(0.03, 5.8, 4.02);

% Options
opt = fitoptions('method','NonlinearLeastSquares','Lower',[-Inf -Inf 0]);
ind1 = isfinite(X1) & isfinite(Y1);
ind2 = isfinite(X2) & isfinite(Y2);

set(opt,'Startpoint',[p1 p2 p3 q1 q2 q3]);
typ = fittype('gauss2');

% Fit 
cf1 = fit(X1(ind1),Y1(ind1),typ,opt);
cf2 = fit(X2(ind2),Y2(ind2),typ,opt);

figure(1)
plot(cf1,'fit',0.95);

figure(2)
plot(cf2,'fit',0.95);

% Problem lies here while combining the two plots 
figure(3)
hold on
plot(cf1,'fit',0.95);
plot(cf2,'fit',0.95); 

替换

figure(3)
hold on
plot(cf1,'fit',0.95);
plot(cf2,'fit',0.95); 

来自

figure(3)
plot(cf1,'fit',0.95);
hold on
plot(cf2,'fit',0.95); 

这是怎么回事? hold on 命令已经固定了绘图的轴。似乎拟合图然后使用此轴来确定其绘图范围。如果您在保持之前绘图,拟合图会自动确定其绘图范围。