Demo entry 6381874

matlab

   

Submitted by anonymous on May 23, 2017 at 16:17
Language: Matlab. Code size: 5.6 kB.

 function createFit(x1,y1,x2,y2)
%CREATEFIT Create plot of data sets and fits
%   CREATEFIT(X1,Y1,X2,Y2)
%   Creates a plot, similar to the plot in the main Curve Fitting Tool,
%   using the data that you provide as input.  You can
%   use this function with the same data you used with CFTOOL
%   or with different data.  You may want to edit the function to
%   customize the code and this help message.
%
%   Number of data sets:  2
%   Number of fits:  2

% Data from data set "y1 vs. x1":
%     X = x1:
%     Y = y1:
%     Unweighted

% Data from data set "y2 vs. x2":
%     X = x2:
%     Y = y2:
%     Unweighted

% Auto-generated by MATLAB on 20-May-2017 22:28:55

% Set up figure to receive data sets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[1 41 1536 699.2]);
% Line handles and text for the legend.
legh_ = [];
legt_ = {};
% Limits of the x-axis.
xlim_ = [Inf -Inf];
% Axes for the plot.
ax_ = axes;
set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
set(ax_,'Box','on');
axes(ax_);
hold on;

% --- Plot data that was originally in data set "y1 vs. x1"
x1 = x1(:);
y1 = y1(:);
h_ = line(x1,y1,'Parent',ax_,'Color',[0.333333 0 0.666667],...
    'LineStyle','none', 'LineWidth',1,...
    'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(x1));
xlim_(2) = max(xlim_(2),max(x1));
legh_(end+1) = h_;
legt_{end+1} = 'y1 vs. x1';
x=[x1(1),x1(1)];
y=[y1(1),y2(1)];
h1=plot(x,y);
set(h1(1),'Color',[0 0 1],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',2);
x=[x1(1),x2(1)];
y=[y2(1),y2(1)];
h2=plot(x,y);
set(h2(1),'Color',[0 0 1],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',2);


% --- Plot data that was originally in data set "y2 vs. x2"
x2 = x2(:);
y2 = y2(:);
h_ = line(x2,y2,'Parent',ax_,'Color',[0.333333 0.666667 0],...
    'LineStyle','none', 'LineWidth',1,...
    'Marker','x', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(x2));
xlim_(2) = max(xlim_(2),max(x2));
legh_(end+1) = h_;
legt_{end+1} = 'y2 vs. x2';

% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
    xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
    set(ax_,'XLim',xlim_)
else
    set(ax_, 'XLim',[0.96100000000000008, 15.138999999999999]);
end

% --- Create fit "fit 1"
fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[-Inf -Inf    0 -Inf -Inf    0 -Inf -Inf    0]);
ok_ = isfinite(x1) & isfinite(y1);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
st_ = [20.199571850000002 1.1000000000000001 0.61836909343853708 7.4290516483210904 2 1.0059952660108773 4.1481389746617925 4 1.2933061247023687 ];
set(fo_,'Startpoint',st_);
ft_ = fittype('gauss3');

% Fit this model using new data
cf_ = fit(x1(ok_),y1(ok_),ft_,fo_);
% Alternatively uncomment the following lines to use coefficients from the
% original fit. You can use this choice to plot the original fit against new
% data.
%    cv_ = { 5932.7413380231646, -4.5659510631555804, 2.1514006795838228, 4513.9047779925622, -17.091325128486403, 7.3637032943075615, 355840419116365760, -641.22673292139109, 102.92124254929544};
%    cf_ = cfit(ft_,cv_{:});

% Plot this fit
x = linspace(x1(1),x1(end));
y= cf_(x);
% Plot this fit
h_ = plot(x,y,'*');
set(h_(1),'Color',[1 0 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',2);
% Turn off legend created by plot method.
legend off;
% Store line handle and fit name for legend.
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 1';

% --- Create fit "fit 2"
fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[-Inf -Inf    0 -Inf -Inf    0 -Inf -Inf    0]);
ok_ = isfinite(x2) & isfinite(y2);
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
st_ = [7.6455703379999997 1.1000000000000001 0.55189427145074121 2.835447395995593 2 0.87547275757119802 1.2887044517751005 4 1.2306814390647618 ];
set(fo_,'Startpoint',st_);
ft_ = fittype('gauss3');

% Fit this model using new data
cf_1 = fit(x2(ok_),y2(ok_),ft_,fo_);

% Alternatively uncomment the following lines to use coefficients from the
% original fit. You can use this choice to plot the original fit against new
% data.
%    cv_ = { 6.974478283834582, 0.16328370972790576, 0.83008150070405529, 93.664622184363395, -6.9234807055230032, 4.5382613025080412, 1412207875817312, -441.51692917677303, 75.427483614682217};
%    cf_ = cfit(ft_,cv_{:});
x = linspace(x2(1),x2(end));
y= cf_1(x);
% Plot this fit
h_ = plot(x,y,'*');
set(h_(1),'Color',[0 0 1],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',2);
% Turn off legend created by plot method.
legend off;
% Store line handle and fit name for legend.
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 2';
x=[x1(end),x2(end)];
y=[cf_(x1(end)),cf_1(x2(end))];
h2=plot(x,y);
set(h2(1),'Color',[1 0 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',2);
% --- Finished fitting and plotting data. Clean up.
hold off;
% Display legend
leginfo_ = {'Orientation', 'vertical', 'FontSize', 9, 'Location', 'northeast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:});
set(h_,'Interpreter','none');

[I1,n]=quad(cf_,x2(1),x1(end));
[I2,n1]=quad(cf_1,x2(1),x2(end));
I3=(x2(1)-x1(1))*y2(1);
[I4,n1]=quad(cf_,x1(1),x2(1));
S=abs(I1-I2)+I3-I4;
st=sprintf('面积S= %.2f',S); 
disp(st);
% Remove labels from x- and y-axes.
xlabel(ax_,'');
ylabel(ax_,'');

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).