1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| clear;clc year = 1790:10:2000; population = [3.9,5.3,7.2,9.6,12.9,17.1,23.2,31.4,38.6,50.2,62.9,76.0,92.0,106.5,123.2,131.7,150.7,179.3,204.0,226.5,251.4,281.4]; cftool
[fitresult, gof] = createFit(year, population) t = 2001:2030; xm = 342.4; r = 0.02735; predictions = xm./(1+(xm./3.9-1).*exp(-r.*(t-1790))); figure(2) plot(year,population,'o',t,predictions,'.') disp(predictions)
function [fitresult, gof] = createFit(year, population)
[xData, yData] = prepareCurveData( year, population );
ft = fittype( 'xm/(1+(xm/3.9-1)*exp(-r*(t-1790)))', 'independent', 't', 'dependent', 'x' ); opts = fitoptions( 'Method', 'NonlinearLeastSquares' ); opts.Display = 'Off'; opts.StartPoint = [0.2 500];
[fitresult, gof] = fit( xData, yData, ft, opts );
figure( 'Name', 'untitled fit 1' ); h = plot( fitresult, xData, yData ); legend( h, 'population vs. year', 'untitled fit 1', 'Location', 'NorthEast' );
xlabel year ylabel population grid on
|