Plot multi curve in one figure

2 views (last 30 days)
Tesla
Tesla on 25 May 2022
Commented: Voss on 28 May 2022
I am trying to plot many curve in one figure with different color and size.
But when I am trying to do that I got strange error.
Here my code
function [fitresult, gof] = createFits1(x, y)
%% Initialization.
x = [0,0.1, 0.2, 0.3, 0.4, 0.46, 0.55, 0.6];
%y = [0.001, 0.00111499, 0.0011926, 0.0013699, 0.00161633, 0.00192075, 0.00274991, 0.00357156]; % for 100% 0.64
% x = [0,0.1, 0.2, 0.3, 0.4, 0.46, 0.55, 0.6]';
% y = [0.001, 0.001161, 0.00141484, 0.0021995, 0.00408401, 0.00454675, 0.0067192, 0.00987622]'; % 10% 0.54 + 90% 0.99
y = [0.001, 0.00117728, 0.00146081, 0.00215119, 0.00307794, 0.00398234, 0.00619084, 0.00696612];
% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 10, 1 );
gof = struct( 'sse', cell( 10, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
%% Fit: 'Mooney (1951)'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( '0.001*(exp((b*x)/(1-x/a)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [0 0];
opts.StartPoint = [1 1];
opts.Upper = [1 100];
% Fit model to data.
[fitresult{1}, gof(1)] = fit( xData, yData, ft, opts );
plot( fitresult{1}, xData, yData );
legend('y vs. x', 'Mooney (1951)', 'Location', 'NorthEast', 'Interpreter', 'none' );
hold on
%% Fit: 'Krieger and Dougherty (1959)'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( '0.001*(1-x/a)^(-b*a)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [0 0];
opts.StartPoint = [1 1];
opts.Upper = [1 100];
% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft, opts );
plot( fitresult{2} ,xData, yData,'LineWidth',10);
legend('y vs. x', 'Krieger and Dougherty (1959)', 'Location', 'NorthEast', 'Interpreter', 'none' );
hold on
  2 Comments
Torsten
Torsten on 25 May 2022
It would be interesting to know the error message and location.
Tesla
Tesla on 25 May 2022
The error is:
Error in cfit/plot (line 50)
outliers = curvefit.ensureLogical( outliers, numel( ydata ) );
Error in testfit (line 45)
plot( fitresult{2} ,xData, yData,'LineWidth',10);
it gone when I remove
'LineWidth',10

Sign in to comment.

Accepted Answer

Voss
Voss on 28 May 2022
The function plot from the Curve Fitting Toolbox does not have the same set of options as the function plot from base MATLAB.
To set any property of lines created with Curve Fitting Toolbox plot, you can store the line handle and then set the property after the plot call.
createFits1();
function [fitresult, gof] = createFits1()%x, y)
%% Initialization.
x = [0,0.1, 0.2, 0.3, 0.4, 0.46, 0.55, 0.6];
%y = [0.001, 0.00111499, 0.0011926, 0.0013699, 0.00161633, 0.00192075, 0.00274991, 0.00357156]; % for 100% 0.64
% x = [0,0.1, 0.2, 0.3, 0.4, 0.46, 0.55, 0.6]';
% y = [0.001, 0.001161, 0.00141484, 0.0021995, 0.00408401, 0.00454675, 0.0067192, 0.00987622]'; % 10% 0.54 + 90% 0.99
y = [0.001, 0.00117728, 0.00146081, 0.00215119, 0.00307794, 0.00398234, 0.00619084, 0.00696612];
% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 10, 1 );
gof = struct( 'sse', cell( 10, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
%% Fit: 'Mooney (1951)'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( '0.001*(exp((b*x)/(1-x/a)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [0 0];
opts.StartPoint = [1 1];
opts.Upper = [1 100];
% Fit model to data.
[fitresult{1}, gof(1)] = fit( xData, yData, ft, opts );
plot( fitresult{1}, xData, yData );
legend('y vs. x', 'Mooney (1951)', 'Location', 'NorthEast', 'Interpreter', 'none' );
hold on
%% Fit: 'Krieger and Dougherty (1959)'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( '0.001*(1-x/a)^(-b*a)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [0 0];
opts.StartPoint = [1 1];
opts.Upper = [1 100];
% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft, opts );
h = plot( fitresult{2} ,xData, yData);%,'LineWidth',10);
set(h,'LineWidth',10);
legend('y vs. x', 'Krieger and Dougherty (1959)', 'Location', 'NorthEast', 'Interpreter', 'none' );
hold on
end
  2 Comments
Tesla
Tesla on 28 May 2022
Thank you very much it works!

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!