dykes
dykes

Reputation: 103

Plotting two fitted curves in the same plot (MATLAB)

I am trying to plot two fitted curves using two-term gauss model on Matlab. While plotting the fitted curves individually, the plots show the entire fitted curve but when I combine both the fitted curve in a single plot only a part of the curves is being displayed the rest is being cut off.

code:

% 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); 

Upvotes: 0

Views: 1357

Answers (1)

Florian
Florian

Reputation: 1824

Replace

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

by

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

What is happening? The hold on command already fixes the axis of the plot. It seems the fit plot then uses this axis to determine its plot range. If you plot before the hold, the fit plot determines its plot range automatically.

Upvotes: 4

Related Questions