12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

We change variables:p ′ = C + Bx.Now we simply do a least-squares fit using this equation; that is, astraight line. We could use the backslash notation that we used to fitthe parabola but, for variety, let’s use the polyfit function. A straightline is a polynomial <strong>of</strong> degree 1. The following code takes the logarithm<strong>of</strong> the population data <strong>and</strong> fits a straight line to it:>> logp = log(P);>> c = polyfit(year,logp,1)c =0.0430 -68.2191The vector c contains B <strong>and</strong> C, in that order. We use the polynomialevaluation function polyval to calculate the fitted population over a fineyear grid:>> year_fine = (year(1):0.5:year(length(year)))’;>> logpfit = polyval(c,year_fine);And we display the results on linear <strong>and</strong> logarithmic y-scales:subplot(221)plot(year,P,’:o’,year_fine,exp(logpfit))subplot(222)semilogy(year,P,’:o’,year_fine,exp(logpfit))The single straight line cannot fit all the data. The right h<strong>and</strong> plotindicates that there were two growth factors, B: one prior to 1870 <strong>and</strong>one after. Let’s do another fit using only the data after 1870:ind = find(year>1870);logp = log(P(ind));c = polyfit(year(ind),logp,1);logpfit = polyval(c,year_fine);clf subplot(221)plot(year,P,’:o’,year_fine,exp(logpfit))subplot(222)semilogy(year,P,’:o’,year_fine,exp(logpfit))c○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!