How can I plot 2 fit curves in the same plot using cftool ?

2 views (last 30 days)
Hello to all!
I hope you are doing well. As the question says, I just would like to know if it is possible to plot 2 previosuly fit curves in the same plot using cftool, so far I havent found anything about this.
My matlab version is 2019.
Thanks in advance!
Angela
  2 Comments
Angelavtc
Angelavtc on 15 Feb 2020
Yes, like for example put together the following 2 fits.
Fit 1.-
function [fitresult, gof] = createFit(x_dec_12_s, Price_dec_12_s)
%CREATEFIT(X_DEC_12_S,PRICE_DEC_12_S)
% Create a fit.
%
% Data for 'december 2012' fit:
% X Input : x_dec_12_s
% Y Output: Price_dec_12_s
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 15-Feb-2020 16:58:00
%% Fit: 'december 2012'.
[xData, yData] = prepareCurveData( x_dec_12_s, Price_dec_12_s );
% Set up fittype and options.
ft = fittype( 'poly6' );
excludedPoints = excludedata( xData, yData, 'Indices', [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 30 44 46 47 48 54 676 712 780 858 865 877 889 892 903 912 913 914 915 916 917 923 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 949] );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Exclude = excludedPoints;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'december 2012' );
plot( fitresult, xData, yData, excludedPoints );
% Label axes
xlabel( 'x_dec_12_s', 'Interpreter', 'none' );
ylabel( 'Price_dec_12_s', 'Interpreter', 'none' );
grid on
Fit 2.-
function [fitresult, gof] = createFits(x_dec_12_s, Price_dec_12_s, x_dec_13_s, Price_dec_13_s)
%CREATEFITS(X_DEC_12_S,PRICE_DEC_12_S,X_DEC_13_S,PRICE_DEC_13_S)
% Create fits.
%
% Data for 'december 2012' fit:
% X Input : x_dec_12_s
% Y Output: Price_dec_12_s
% Data for 'december 2013' fit:
% X Input : x_dec_13_s
% Y Output: Price_dec_13_s
% Output:
% fitresult : a cell-array of fit objects representing the fits.
% gof : structure array with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 15-Feb-2020 17:05:00
%% Initialization.
% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 2, 1 );
gof = struct( 'sse', cell( 2, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
%% Fit: 'december 2012'.
[xData, yData] = prepareCurveData( x_dec_12_s, Price_dec_12_s );
% Set up fittype and options.
ft = fittype( 'poly6' );
excludedPoints = excludedata( xData, yData, 'Indices', [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 30 44 46 47 48 54 676 712 780 858 865 877 889 892 903 912 913 914 915 916 917 923 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 949] );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Exclude = excludedPoints;
% Fit model to data.
[fitresult{1}, gof(1)] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'december 2012' );
plot( fitresult{1}, xData, yData, excludedPoints );
% Label axes
xlabel( 'x_dec_12_s', 'Interpreter', 'none' );
ylabel( 'Price_dec_12_s', 'Interpreter', 'none' );
grid on
%% Fit: 'december 2013'.
[xData, yData] = prepareCurveData( x_dec_13_s, Price_dec_13_s );
% Set up fittype and options.
ft = fittype( 'poly6' );
excludedPoints = excludedata( xData, yData, 'Indices', [3 4 7] );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Exclude = excludedPoints;
% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'december 2013' );
plot( fitresult{2}, xData, yData, excludedPoints );
% Label axes
xlabel( 'x_dec_13_s', 'Interpreter', 'none' );
ylabel( 'Price_dec_13_s', 'Interpreter', 'none' );
grid on

Sign in to comment.

Accepted Answer

darova
darova on 15 Feb 2020
Try this in second fit

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!