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.

display the resulting fit to the data by calculating the parabola. Weuse matrix multiplication to calculate the polynomial over a fine set <strong>of</strong>points separated by half a year:year_fine = (year(1):0.5:year(length(year)))’;Pfine = [ones(size(year_fine)) year_fine year_fine.^2]*C;plot(year,P,’o’,...year_fine,Pfine)This technique can be used to fit any function that is linear in itsparameters. (matlab provides the functions polyfit <strong>and</strong> polyval aseasy interfaces to the functionality that we have just illustrated usingmatrix multiplication <strong>and</strong> division.)Exercise 4 Use this technique to fit an exponential curve to thepopulation data. Hint: Take logs. (Answer on page 183.)15 Missing DataReal-world measurements are <strong>of</strong>ten taken at regular intervals; for example,the position <strong>of</strong> a comet in the sky measured each night, or the depth<strong>of</strong> the sea along a line at 1 metre increments. Environmental effects orequipment failure (a cloudy night or a failed depth meter) sometimesresult in a set <strong>of</strong> data that has missing values. In matlab these can berepresented by NaN, which st<strong>and</strong>s for “not-a-number”. NaN is also givenby matlab as the result <strong>of</strong> undefined calculations such as 0/0. matlabh<strong>and</strong>les NaNs by setting the result <strong>of</strong> any calculation that involves NaNsto NaN. Let us look at an example:y = [1:4 NaN 6:14 NaN 16:20];plot(y,’o’)grid;boxIn everyday language we would say that the fifth <strong>and</strong> the fifteenth values<strong>of</strong> the y-vector are missing. matlab’s graphics functions usually h<strong>and</strong>lec○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!