function [cf_,gof,output,MeanTest,warnstr,errstr,convmsg,exitflag]=createFitLonS1S2_0(X,Y,W,p)
%Subfunction of LonFit3_long and LonFit3_long_2. Fits bare S1 and S2
%CREATEFIT Create plot of data sets if p=1 and fits
%   CREATEFIT(X,Y,W)
%   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:  1
%   Number of fits:  1

% Data from data set "test":
%     X = X:
%     Y = Y:
%     Weights = W:

% Auto-generated by MATLAB on 08-Feb-2012 14:45:01
if p==1

 %Set up figure to receive data sets and fits%
%f_ = clf;
f_=figure('NumberTitle','off','units','centimeters','Position',[0 0 10 8],...                    % setter str på figuren du vil se på skjermen. VIKIT Gat units kommer FØR Position
    'PaperUnits','centimeters','PaperPosition', [0 0 10 8],...% setter str på figuren du vil lagre (uavhengig av format) VIKITG at units kommer FØR Position
    'DefaultAxesFontSize',12,'DefaultAxesFontWeight','b');
figure(f_);
set(f_,'Units','Pixels','Position',[658 329 688 485]);
% 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;
end
% --- Plot data that was originally in data set "test"
X = X(:);
Y = Y(:);
W = W(:);
if p==1
h_ = line(X,Y,'Parent',ax_,'Color',[0.333333 0 0.666667],...
    'LineStyle','none', 'LineWidth',1,...
    'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(X));
xlim_(2) = max(xlim_(2),max(X));
legh_(end+1) = h_;
legt_{end+1} = 'test';

% 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.95000000000931328, 95.950000000940634]);
end
end
% --- Create fit "fit 2" 
%optimset('TolPCG',0.01)
optimset('PrecondBandWidth',inf);
fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[0     -inf      0     -inf   ],'Upper',[100    inf   100    inf]);
ok_ = isfinite(X) & isfinite(Y) & isfinite(W); 
if ~all( ok_ )
    warning( 'GenerateMFile:IgnoringNansAndInfs',...
        'Ignoring NaNs and Infs in data.' );
end
MeanTest=mean(Y);
%st_ = [ 10 0.5 mean(Y) 10 0.5 ];
st_ = [ 10 0.5 10 0.5 ];
set(fo_,'Startpoint',st_);
%set(fo_,'Weight',W(ok_));
set(fo_,'TolFun',1e-020);
set(fo_,'TolX',1e-06);
optimset('PrecondBandWidth',inf);
set(fo_,'MaxIter',1000);
set(fo_,'MaxFunEvals',3000);
%set(fo_,'DiffMaxChange',0.0000001);
% ft_ = fittype('S2*sin(2*pi*(lon/180+c2))+S1*sin(2*pi*(lon/360+c1))+S0',...
%     'dependent',{'y'},'independent',{'lon'},...
%     'coefficients',{ 'S2','c2','S1', 'c1','S0'});

ft_ = fittype('S2*sin(2*pi*(lon/180+c2))+S1*sin(2*pi*(lon/360+c1))',...
     'dependent',{'y'},'independent',{'lon'},...
     'coefficients',{ 'S2','c2','S1', 'c1'});


% Fit this model using new data
[cf_,gof,output,warnstr,errstr,convmsg] = fit(X(ok_),Y(ok_),ft_,fo_);
%cf_
output.iterations;
output.funcCount;
output.message;
%if (output.exitflag~=2)
%    pause
%end
if p==1
% Plot this fit
h_ = plot(cf_,'fit',0.95);
set(h_(1),'Color',[1 0 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
% 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';

% --- Finished fitting and plotting data. Clean up.
hold off;
% Display legend
leginfo_ = {'Orientation', 'vertical', 'Location', 'NorthEast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:});
set(h_,'Interpreter','none');
% Remove labels from x- and y-axes.
xlabel(ax_,'');
ylabel(ax_,'');
end
end