clear all; close all
load census
s = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);
f2 = fittype('poly1');
pop2 = log(pop);
[c2,gof2] = fit(cdate,pop,f,'problem',2);
[c3,gof3] = fit(cdate,pop2,f2);
low_lim = min(cdate);
hi_lim = max(cdate);
x = linspace(low_lim, hi_lim, 100);
y1 = feval(c2, x);
y2 = feval(c3, x);
figure(1); clf
hl1 = plot(cdate, pop, 'ro');
hold on;
hl2 = plot(x, y1, 'c');
legend(hl2, 'fitted curve')
ax1 = gca;
set(ax1,'XColor','r','YColor','r');
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
gca = ax2;
hold on;
hl3 = plot(cdate, pop2, 'bs');
hold on;
hl4 = plot(x, y2, 'b-');
legend([hl2 hl4], 'fitted curve', 'fitted curve')
axis(ax2, [low_lim hi_lim min(y2) max(y2)])
set(ax2, 'xlim', get(ax1, 'xlim'));
Best Answer