%% Custom Nonlinear Census Fitting
% This example shows how to fit a custom equation to census data, specifying bounds, coefficients, and a problem-dependent parameter.
% Load and plot the data in census.mat:

load census
plot(cdate,pop,'o')
hold on

%
% Create a fit options structure and a fittype object for the custom nonlinear model y = a(x-b)n, where a and b are coefficients and n is a problem-dependent parameter. See the fittype function page for more details on problem-dependent parameters.

s = fitoptions('Method','NonlinearLeastSquares',...
               'Lower',[0,0],...
               'Upper',[Inf,max(cdate)],...
               'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);

%
% Fit the data using the fit options and a value of n = 2:

[c2,gof2] = fit(cdate,pop,f,'problem',2)

%
% Fit the data using the fit options and a value of n = 3:

[c3,gof3] = fit(cdate,pop,f,'problem',3)

%
% Plot the fit results and the data:

plot(c2,'m')
plot(c3,'c')
legend( 'fit with n=2', 'fit with n=3' )

% Copyright 2015 The MathWorks, Inc.