plotting multiple fitting graphs in a single graph

2 views (last 30 days)
Hi ,
I want to plot 2 fitting graphs on the same plot, i tried to use hold on , but it doesn't work
here is the code the one that ( with large scale of noise =5 on the same figure with large scale of noise =10)
% with large scale of noise = 10
xData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]
xData = 1×21
10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
yData = [99 99 99 99 99 99 98 92 85 70 48 31 7 3 1 0 0 0 0 0 0]
yData = 1×21
99 99 99 99 99 99 98 92 85 70 48 31 7 3 1 0 0 0 0 0 0
x= xData'
x = 21×1
10 9 8 7 6 5 4 3 2 1
y=yData'
y = 21×1
99 99 99 99 99 99 98 92 85 70
% Set up fittype and options.
ft = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult, gof] = fit( x, y, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
hold on
legend( h, 'y vs. x', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on
saveas(gcf,'myfigure.pdf')
hold on
% with large scale of noise = 5
tData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]
tData = 1×21
10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
bData = [99 99 99 99 99 99 99 99 98 84 47 8 1 0 0 0 0 0 0 0 0]
bData = 1×21
99 99 99 99 99 99 99 99 98 84 47 8 1 0 0 0 0 0 0 0 0
x= tData'
x = 21×1
10 9 8 7 6 5 4 3 2 1
y=bData'
y = 21×1
99 99 99 99 99 99 99 99 98 84
% Set up fittype and options.
ft = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult, gof] = fit( x, y, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, tData, bData );

Answers (2)

Cris LaPierre
Cris LaPierre on 16 Nov 2022
By including the figure command, you are telling MATLAB to create a new figure window. Removing that and making some minor cosmetic changes, here is how I would do it.
% with large scale of noise = 10
xData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
yData = [99 99 99 99 99 99 98 92 85 70 48 31 7 3 1 0 0 0 0 0 0]';
% Set up fittype and options.
ft1 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts1 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts1.Display = 'Off';
opts1.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult1, gof1] = fit( xData, yData, ft1, opts1 );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
plot( fitresult1, xData, yData );
legend('y vs. x', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on
% with large scale of noise = 5
tData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]';
bData = [99 99 99 99 99 99 99 99 98 84 47 8 1 0 0 0 0 0 0 0 0]';
% Set up fittype and options.
ft2 = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts2 = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts2.Display = 'Off';
opts2.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult2, gof2] = fit( tData, bData, ft2, opts2 );
% Plot fit with data.
hold on
plot( fitresult2, tData, bData,'c.' );
hold off

Star Strider
Star Strider on 16 Nov 2022
The code works as posted.
If you want to plot both results on the same axes, that is straightforward. See the third figure —
% with large scale of noise = 10
xData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10];
yData = [99 99 99 99 99 99 98 92 85 70 48 31 7 3 1 0 0 0 0 0 0];
x= xData(:);
y=yData(:);
% Set up fittype and options.
ft = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult, gof] = fit( x, y, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h1 = plot( fitresult, xData, yData )
h1 =
2×1 Line array: Line (data) Line (fitted curve)
hold on
legend( h1, 'y vs. x', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on
saveas(gcf,'myfigure.pdf')
hold on
% with large scale of noise = 5
tData = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10];
bData = [99 99 99 99 99 99 99 99 98 84 47 8 1 0 0 0 0 0 0 0 0];
x= tData(:);
y=bData(:);
% Set up fittype and options.
ft = fittype( 'a/(1+exp(-b*x))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.957166948242946 0.485375648722841];
% Fit model to data.
[fitresult, gof] = fit( x, y, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h2 = plot( fitresult, tData, bData )
h2 =
2×1 Line array: Line (data) Line (fitted curve)
figure
plot(h1(1).XData, h1(1).YData, 'sb', 'DisplayName','Data 1')
hold on
plot(h1(2).XData, h1(2).YData, '-r', 'DisplayName','Curve 1')
plot(h2(1).XData, h2(1).YData, 'db', 'DisplayName','Data 2')
plot(h2(2).XData, h2(2).YData, '-g', 'DisplayName','Curve 2')
hold off
grid
legend('Location','best')
Make appropriate changes to get the desired result.
.

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!