Transcript
Page 1: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

Solutions for Geog 210C Lab Assignment #1

STUDENT NAME: Antonio Medrano

DATE COMPLETED: 4/10/08

Contents

Instructions

Page #1: Preliminaries

1/1: Just displaying stuff...

1/2: Now it's your turn...

1/3: Booleans

1/4: For loops and basic statistics

1/5: Sorting

1/6: Median

1/7: Logic

1/8: More logic and logs

2/1: Histograms

2/2: Density Histograpms

2/3: Gaussian Comparison

2/4: Cumulative Distribution Function (CDF)

2/5: Easy CDF plotting

2/6: Gaussian CDF comparison

2/7: Interpolation

2/8: Quantiles

Section 3 prep

3/1: Stem plots

3/2: Annotations

3/3: Histogram Comparison

3/4: Box Plots

3/5: QQ-Plots

3/6: more QQ-plots

3/7: Gaussian comparison

3/8: Printing graphs to files

Instructions

Copy this M-file into your working directory C:\Workspace, rename it as, say, lab01.m, and

use it as a template, i.e., append the appropriate Matlab commands, to complete each Lab

assignment.

From Matlab, open this file using the Matlab Editor. DO NOT open this file by double

clicking it with your mouse outside Matlab.

Remember to enable cell mode in the Matlab Editor. A cell is a group of Matlab commands,

whose start is designated with 2 percent signs %% You can highlight each cell by clicking on

it with the mouse, and then evaluate/execute ONLY that cell (Control + Enter) as you

proceed in completing your Lab assignment.

Use a single percent sign % to include comments/answers in your M-file. Anything after (to

the right of) % is treated as a comment, i.e., not considered a Matlab command.

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

1 of 31 4/16/2009 2:23 PM

Page 2: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

You could try to answer each Lab question in the Matlab command window. Once you have

decided on the correct Matlab syntax, copy that command in your M-file for the

corresponding Lab.

Publishing to html:

The completed M-file can be converted into an html file containing all your Matlab

commands, comments, command output, and figures, using Matlab's publishing utility. This

can be done using:

File -> Publish to HTML

and automatically creates a folder called html in your Matlab working directory. This folder

contains the html file, as well as a suite of associated image files (.png files), if your M-file

contains commands that produce graphical output, such as command plot. Note that you can

also publish your M-file into other formats, such as pdf or Powerpoint; see File ->

Publish To

You can insert a variety of text formats in your M-file, using:

Cell -> Insert Text Markup

the differences between the above text formats are only visible in the published html file.

For example, you can have a bulleted list with:

boldface text

italisized text

monospaced text; this is typically reserved for Matlab commands

What to turn in:

For each Lab assignment, send an e-mail to the READER [email protected] with:

(i) a M-file containig the appropriate Matlab commands

(ii) a .zip archive of the above html folder published via Matlab

Collaboration is encouraged, but each student needs to submit his or her own M-file and

.zip archive to the Reader

Good luck, and remember to put your name at the top of this file...

Phaedon C. Kyriakidis, April 2009.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Page #1: Preliminaries 114.5/120

Load the data file ithaca.dat with weather-related from the working directory

load ithaca.dat

% List variables in workspace

who

% More detailed listing of variables in workspace

whos

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

2 of 31 4/16/2009 2:23 PM

Page 3: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

Your variables are:

Fx fx meanTmax precip stdTmax

FxG i medT propW tmax

Gval2040 indN medTmax q2040 tmaxS

ans indNW minT q2040Gauss tmin

area indW minTmax qCGauss val

binWidth10 ithaca minV qp2040 val2040

boolW j myLabels qp2040Quant val2040Gauss

c10 madP n10 quantG varP

c15 madPrecip n15 relFreq10 varPrecip

c20 maxT n20 relFreq15 x

canandaigua maxTmax nDays relFreq20 x20

covP maxV nVars sortTmax x40

covPrecip meanCtmax numW stdCtmax xS

d meanP p stdP ymax

day meanPrecip p2040 stdPrecip

days meanPrecipC p2040Gauss stdPrecipC

densFreq10 meanPrecipI pq2040Gauss stdPrecipI

Name Size Bytes Class Attributes

Fx 18x1 144 double

FxG 1x501 4008 double

Gval2040 1x2 16 double

ans 1x1 8 double

area 1x1 8 double

binWidth10 1x1 8 double

boolW 31x1 31 logical

c10 1x10 80 double

c15 1x15 120 double

c20 1x20 160 double

canandaigua 31x4 992 double

covP 1x1 8 double

covPrecip 1x1 8 double

d 1x31 248 double

day 1x1 8 double

days 1x31 248 double

densFreq10 1x10 80 double

fx 1x501 4008 double

i 1x1 8 double

indN 7x1 56 double

indNW 5x1 40 double

indW 15x1 120 double

ithaca 31x4 992 double

j 1x1 8 double

madP 1x1 8 double

madPrecip 1x1 8 double

maxT 1x1 8 double

maxTmax 1x1 8 double

maxV 1x1 8 double

meanCtmax 1x1 8 double

meanP 1x1 8 double

meanPrecip 1x1 8 double

meanPrecipC 1x1 8 double

meanPrecipI 1x1 8 double

meanTmax 1x1 8 double

medT 1x1 8 double

medTmax 1x1 8 double

minT 1x1 8 double

minTmax 1x1 8 double

minV 1x1 8 double

myLabels 1x2 144 cell

n10 1x10 80 double

n15 1x15 120 double

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

3 of 31 4/16/2009 2:23 PM

Page 4: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

n20 1x20 160 double

nDays 1x1 8 double

nVars 1x1 8 double

numW 1x1 8 double

p 1x31 248 double

p2040 1x2 16 double

p2040Gauss 1x2 16 double

pq2040Gauss 1x2 16 double

precip 31x1 248 double

propW 1x1 8 double

q2040 1x2 16 double

q2040Gauss 1x2 16 double

qCGauss 1x31 248 double

qp2040 1x2 16 double

qp2040Quant 1x2 16 double

quantG 1x31 248 double

relFreq10 1x10 80 double

relFreq15 1x15 120 double

relFreq20 1x20 160 double

sortTmax 31x1 248 double

stdCtmax 1x1 8 double

stdP 1x1 8 double

stdPrecip 1x1 8 double

stdPrecipC 1x1 8 double

stdPrecipI 1x1 8 double

stdTmax 1x1 8 double

tmax 31x1 248 double

tmaxS 31x1 248 double

tmin 31x1 248 double

val 1x1 8 double

val2040 1x2 16 double

val2040Gauss 1x2 16 double

varP 1x1 8 double

varPrecip 1x1 8 double

x 1x501 4008 double

x20 1x1 8 double

x40 1x1 8 double

xS 18x1 144 double

ymax 1x1 8 double

Note that, in the html file all command outputs are displayed at the end of each cell of the

M-file, and any comments that follow a Matlab command appear with a percent sign % in

front. That's fine for your assignments.

If, for whatever reason, you want to see command outputs right after each command, use a

mock-cell, i.e., start a cell with no title using 2 percent signs %% in a single line of code:

List of variables in workspace

who

Your variables are:

Fx fx meanTmax precip stdTmax

FxG i medT propW tmax

Gval2040 indN medTmax q2040 tmaxS

ans indNW minT q2040Gauss tmin

area indW minTmax qCGauss val

binWidth10 ithaca minV qp2040 val2040

boolW j myLabels qp2040Quant val2040Gauss

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

4 of 31 4/16/2009 2:23 PM

Page 5: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

c10 madP n10 quantG varP

c15 madPrecip n15 relFreq10 varPrecip

c20 maxT n20 relFreq15 x

canandaigua maxTmax nDays relFreq20 x20

covP maxV nVars sortTmax x40

covPrecip meanCtmax numW stdCtmax xS

d meanP p stdP ymax

day meanPrecip p2040 stdPrecip

days meanPrecipC p2040Gauss stdPrecipC

densFreq10 meanPrecipI pq2040Gauss stdPrecipI

More detailed list

whos

Name Size Bytes Class Attributes

Fx 18x1 144 double

FxG 1x501 4008 double

Gval2040 1x2 16 double

ans 1x1 8 double

area 1x1 8 double

binWidth10 1x1 8 double

boolW 31x1 31 logical

c10 1x10 80 double

c15 1x15 120 double

c20 1x20 160 double

canandaigua 31x4 992 double

covP 1x1 8 double

covPrecip 1x1 8 double

d 1x31 248 double

day 1x1 8 double

days 1x31 248 double

densFreq10 1x10 80 double

fx 1x501 4008 double

i 1x1 8 double

indN 7x1 56 double

indNW 5x1 40 double

indW 15x1 120 double

ithaca 31x4 992 double

j 1x1 8 double

madP 1x1 8 double

madPrecip 1x1 8 double

maxT 1x1 8 double

maxTmax 1x1 8 double

maxV 1x1 8 double

meanCtmax 1x1 8 double

meanP 1x1 8 double

meanPrecip 1x1 8 double

meanPrecipC 1x1 8 double

meanPrecipI 1x1 8 double

meanTmax 1x1 8 double

medT 1x1 8 double

medTmax 1x1 8 double

minT 1x1 8 double

minTmax 1x1 8 double

minV 1x1 8 double

myLabels 1x2 144 cell

n10 1x10 80 double

n15 1x15 120 double

n20 1x20 160 double

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

5 of 31 4/16/2009 2:23 PM

Page 6: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

nDays 1x1 8 double

nVars 1x1 8 double

numW 1x1 8 double

p 1x31 248 double

p2040 1x2 16 double

p2040Gauss 1x2 16 double

pq2040Gauss 1x2 16 double

precip 31x1 248 double

propW 1x1 8 double

q2040 1x2 16 double

q2040Gauss 1x2 16 double

qCGauss 1x31 248 double

qp2040 1x2 16 double

qp2040Quant 1x2 16 double

quantG 1x31 248 double

relFreq10 1x10 80 double

relFreq15 1x15 120 double

relFreq20 1x20 160 double

sortTmax 31x1 248 double

stdCtmax 1x1 8 double

stdP 1x1 8 double

stdPrecip 1x1 8 double

stdPrecipC 1x1 8 double

stdPrecipI 1x1 8 double

stdTmax 1x1 8 double

tmax 31x1 248 double

tmaxS 31x1 248 double

tmin 31x1 248 double

val 1x1 8 double

val2040 1x2 16 double

val2040Gauss 1x2 16 double

varP 1x1 8 double

varPrecip 1x1 8 double

x 1x501 4008 double

x20 1x1 8 double

x40 1x1 8 double

xS 18x1 144 double

ymax 1x1 8 double

this approach, however, destroys text continuity in the actual M-file and is not

recommended.

1/1: Just displaying stuff... 5/5

Determine dimensions of ithaca array

size(ithaca)

% Number of columns (attributes) = 4

% Number of rows (days) = 31

% Alternative answer:

[nDays,nVars] = size(ithaca); % The semi-colon supresses output

disp([nDays nVars]);

ans =

31 4

31 4

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

6 of 31 4/16/2009 2:23 PM

Page 7: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

1/2: Now it's your turn... 5/5

Append here your commands, and continue with the remaining questions...

% You need to add meaninful titles to each requisite in this file, once

% you complete your assignment; this will help you consolidate what you

% have learned from each requisite.

% column vector of only the precipitation values of the dataset

precip = ithaca(:,2);

% display elements of precip for Ja1, Jan 11, and Jan 10-15

precip(1)

precip(11)

precip(10:15)

% min and max values of the last 5 days of precip

minV = min(precip(end-4:end));

maxV = max(precip(end-4:end));

% min and max values of the first 5 days of precip

minV = min(precip(1:5));

maxV = max(precip(1:5));

% find the day of the maximum falue of precip

[val,day]=max(precip);

day

%find the max value in days 1-5 and the index where that occurs

max(precip(1:5))

find(precip==max(precip(1:5)))

%this is a better way to find the max value and index

[ymax,i]=max(precip(1:5))

ans =

0

ans =

0.3400

ans =

0.0500

0.3400

0.0600

0.1800

0.0200

0.0200

day =

3

ans =

1.1100

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

7 of 31 4/16/2009 2:23 PM

Page 8: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

ans =

3

ymax =

1.1100

i =

3

1/3: Booleans 5/5

%boolean of rainy days

boolW = precip > 0

% which days are rainy?

indW = find(precip>0)

% number of rainy days

numW = sum(boolW)

% proportion of wet days

propW = mean(boolW)

boolW =

0

1

1

0

0

0

0

1

1

1

1

1

1

1

1

0

0

0

0

1

0

0

1

0

0

0

0

0

1

1

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

8 of 31 4/16/2009 2:23 PM

Page 9: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

1

indW =

2

3

8

9

10

11

12

13

14

15

20

23

29

30

31

numW =

15

propW =

0.4839

1/4: For loops and basic statistics 5/5

%calculate the mean two ways

j = 0;

for i=1:length(precip)

j = j+precip(i);

end

meanPrecip = j/i

meanP = mean(precip)

% calculate the variance two ways

j = 0;

for i=1:length(precip)

j = j + (precip(i)-meanP)^2;

end

varPrecip = j/(i-1)

varP = var(precip)

% calculate the standard deviation two ways

j = 0;

for i=1:length(precip)

j = j + (precip(i)-meanP)^2;

end

stdPrecip = sqrt(j/(i-1))

stdP = std(precip)

% calculate the coefficient of variation two ways

covPrecip = meanPrecip / stdPrecip

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

9 of 31 4/16/2009 2:23 PM

Page 10: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

covP = meanP / stdP

% calculate the mean absolute deviation from the mean two ways

j = 0;

for i=1:length(precip)

j = j + abs(precip(i)-meanP)

end

madPrecip = j/i

madP = mad(precip)

meanPrecip =

0.1016

meanP =

0.1016

varPrecip =

0.0590

varP =

0.0590

stdPrecip =

0.2429

stdP =

0.2429

covPrecip =

0.4184

covP =

0.4184

j =

0.1016

j =

0.1332

j =

1.1416

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

10 of 31 4/16/2009 2:23 PM

Page 11: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

j =

1.2432

j =

1.3448

j =

1.4465

j =

1.5481

j =

1.6097

j =

1.6913

j =

1.7429

j =

1.9813

j =

2.0229

j =

2.1013

j =

2.1829

j =

2.2645

j =

2.3661

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

11 of 31 4/16/2009 2:23 PM

Page 12: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

j =

2.4677

j =

2.5694

j =

2.6710

j =

3.0194

j =

3.1210

j =

3.2226

j =

3.8210

j =

3.9226

j =

4.0242

j =

4.1258

j =

4.2274

j =

4.3290

j =

4.4206

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

12 of 31 4/16/2009 2:23 PM

Page 13: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

j =

4.4923

j =

4.5439

madPrecip =

0.1466

madP =

0.1466

1/5: Sorting 5/5

% assign last column of ithaca to a new variable

tmax = ithaca(:,end);

% calculate min and max through sorting

sortTmax = sort(tmax);

minTmax = sortTmax(1)

maxTmax = sortTmax(end)

minT = min(tmax)

maxT = max(tmax)

minTmax =

9

maxTmax =

53

minT =

9

maxT =

53

1/6: Median 5/5

% calculate the median two ways

medTmax = sortTmax(round(0.5*length(sortTmax)))

medT = median(tmax)

medTmax =

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

13 of 31 4/16/2009 2:23 PM

Page 14: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

30

medT =

30

1/7: Logic 5/5

% assign second to last column of ithaca to a new variable

tmin = ithaca(:,end-1);

% in which days is tmin negative

indN = find(tmin < 0)

% in which days is tmin positive and precip more than 0.1

indNW = find(tmin > 0 & precip > 0.1)

indN =

4

24

25

26

27

28

29

indNW =

3

11

13

20

23

1/8: More logic and logs 5/5

% create a boolean of wet days

boolW = precip > 0

% take the logarithm (base 10) of only the wet days

precip(boolW) = log(precip(boolW))

boolW =

0

1

1

0

0

0

0

1

1

1

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

14 of 31 4/16/2009 2:23 PM

Page 15: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

1

1

1

1

1

0

0

0

0

1

0

0

1

0

0

0

0

0

1

1

1

precip =

0

-2.6593

0.1044

0

0

0

0

-3.2189

-3.9120

-2.9957

-1.0788

-2.8134

-1.7148

-3.9120

-3.9120

0

0

0

0

-0.7985

0

0

-0.3567

0

0

0

0

0

-4.6052

-3.5066

-2.9957

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2/1: Histograms 5/5

% plot the tmax data as three histograms with 10, 15, and 20 bins

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

15 of 31 4/16/2009 2:23 PM

Page 16: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

figure(1)

subplot(3,1,1);

hist(tmax,10);

[n10, c10] = hist(tmax,10);

subplot(3,1,2);

hist(tmax,15);

[n15, c15] = hist(tmax,15);

subplot(3,1,3);

hist(tmax,20);

[n20, c20] = hist(tmax,20);

% all three graphs have the same general shape, but with finer resolution

% as you increase the number of bins

% plot the histograms as relative frequencies

figure(2)

subplot(3,1,1);

relFreq10 = n10/nDays;

bar(c10,relFreq10,1,'w');

subplot(3,1,2);

relFreq15 = n15/nDays;

bar(c15,relFreq15,1,'w');

subplot(3,1,3);

relFreq20 = n20/nDays;

bar(c20,relFreq20,1,'w');

% in bar(), the third argument is a normalized width parameter. n=1 means

% it's a full width bar graph (no gaps). the fourth argument is a filling

% color argument. In this case, 'w' for white was used.

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

16 of 31 4/16/2009 2:23 PM

Page 17: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

close all;

2/2: Density Histograpms 2.5/5

% create the density histogram for the 10 bin instance

binWidth10 = c10(2)-c10(1);display results

densFreq10 = relFreq10/binWidth10;display results

bar(c10,densFreq10,1,'w');

% ensure the total area is equal to 1

area = sum(densFreq10)*binWidth10

% it is!

ecdf function ??

area =

1

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

17 of 31 4/16/2009 2:23 PM

Page 18: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

2/3: Gaussian Comparison 5/5

% plot the Gaussian over the density histogram

hold on;

meanTmax = mean(tmax); display results

stdTmax = std(tmax);display results

x = [5:.1:55];

fx = normpdf(x,meanTmax,stdTmax);

plot(x,fx);

legend('tmax density histogram','Gaussian distribution')

hold off;

% this comparison is good to do to see if your data can be approximated as

% a standard Gaussian distribution (thus allowing you to use Gaussian

% statistics to analyze the data).

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

18 of 31 4/16/2009 2:23 PM

Page 19: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

2/4: Cumulative Distribution Function (CDF) 5/5

% plot the CDF for tmax

tmaxS = sort(tmax);

p = (1:nDays)/nDays;

plot(tmaxS,p,'*');

% the CDF is the integral of the discrete sample density histogram

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

19 of 31 4/16/2009 2:23 PM

Page 20: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

2/5: Easy CDF plotting 5/5

subplot(2,1,1);

cdfplot(tmax);

subplot(2,1,2);

[Fx,xS] = ecdf(tmax);

stairs(xS,Fx);

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

20 of 31 4/16/2009 2:23 PM

Page 21: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

2/6: Gaussian CDF comparison 5/5

% plot the Gaussian CDF over the tmax CDF

close all;

cdfplot(tmax);

hold on;

FxG = normcdf(x,meanTmax,stdTmax);

plot(x,FxG);

hold off;

% if the Gaussian is a good approximation of the data, then you can use

% that function to determine quantiles, rather than interpolating between

% CDF values

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

21 of 31 4/16/2009 2:23 PM

Page 22: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

2/7: Interpolation 5/5

% find the CDF values for 20 and 40 degrees for the tmax data

xS(1)=xS(1)-0.001;

val2040 = interp1(xS,Fx,[20 40])

% find the CDF values for 20 and 40 degrees for the associated Gaussian

x20 = find(x == 20);

x40 = find(x == 40);

val2040Gauss = FxG([x20 x40])

val2040 =

0.0839 0.9476

val2040Gauss =

0.1004 0.9054

2/8: Quantiles 5/5

% find the quantiles for the values in 2/7 (do the inverse calculation)

q2040 = interp1(Fx,xS,val2040)

% find the quantiles using the quantile function

q2040 = quantile(tmax,val2040)

% the ansewr is not 20, 40. At first I thought maybe it was QUANTILE might

% use some other interpolation method rather than linear interpolation, but

% this is not the case. At this point, I'm at a loss for explanation.

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

22 of 31 4/16/2009 2:23 PM

Page 23: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

% find the quantiles for the associated Gaussian

q2040Gauss = norminv(val2040,meanTmax,stdTmax)

q2040 =

20 40

q2040 =

22.2000 44.0000

q2040Gauss =

19.2286 42.3829

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Section 3 prep

load canandaigua.dat

3/1: Stem plots 5/5

% plot ithaca

days = 1:31;

subplot(2,3,1);

stem(days,precip,'*')

xlim([0 32]);

subplot(2,3,2);

stem(days,tmin,'*')

xlim([0 32]);

subplot(2,3,3);

stem(days,tmax,'*')

xlim([0 32]);

% plot canandaigua

subplot(2,3,4);

stem(days,canandaigua(:,2),'r')

xlim([0 32]);

subplot(2,3,5);

stem(days,canandaigua(:,3),'r')

xlim([0 32]);

subplot(2,3,6);

stem(days,canandaigua(:,4),'r')

xlim([0 32]);

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

23 of 31 4/16/2009 2:23 PM

Page 24: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

3/2: Annotations 5/5 check the solution for shorter process

% annotate the above graphs

subplot(2,3,1);

xlabel('day');

ylabel('precip');

title('Ithaca');

ylim([0 1.5]);

subplot(2,3,2);

xlabel('day');

ylabel('tmin');

title('Ithaca');

ylim([-20 40]);

subplot(2,3,3);

xlabel('day');

ylabel('tmax');

title('Ithaca');

ylim([0 60]);

subplot(2,3,4);

xlabel('day');

ylabel('precip');

title('Canandaigua');

ylim([0 1.5]);

subplot(2,3,5);

xlabel('day');

ylabel('tmin');

title('Canandaigua');

ylim([-20 40]);

subplot(2,3,6);

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

24 of 31 4/16/2009 2:23 PM

Page 25: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

xlabel('day');

ylabel('tmax');

title('Canandaigua');

ylim([0 60]);

text(-5,-16,'created by Antonio Medrano')

3/3: Histogram Comparison 5/5

% compare histograms of the two data sets

% Histograms of the 2 precipitation data sets

subplot(1,2,1);

hist(ithaca(:,2),20);

axis([0 1.5 0 30]);

title('Ithaca precip histogram');

subplot(1,2,2);

hist(canandaigua(:,2),20);

axis([0 1.5 0 30]);

title('Canandaigua precip histogram')

% calculate the mean and std deviation for both sets

meanPrecipI = mean(ithaca(:,2))

meanPrecipC = mean(canandaigua(:,2))

stdPrecipI = std(ithaca(:,2))

stdPrecipC = std(canandaigua(:,2))

% Despite having more "no rain" days, Ithaca has a higher mean

% precipitation amount than Canandaigua. This is also reflected in the

% standard deviations, where Ithaca's is much higher than Canandaigua's,

% meaning it has a greater spread of values.

meanPrecipI =

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

25 of 31 4/16/2009 2:23 PM

Page 26: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

0.1016

meanPrecipC =

0.0774

stdPrecipI =

0.2429

stdPrecipC =

0.1676

3/4: Box Plots 5/5

% make box plots for the various variables

subplot(3,1,1);

boxplot([ithaca(:,2) canandaigua(:,2)]);

title('Precip Boxplot');

axis square;

subplot(3,1,2);

boxplot([ithaca(:,3) canandaigua(:,3)]);

title('Tmin Boxplot');

axis square;

subplot(3,1,3);

boxplot([ithaca(:,4) canandaigua(:,4)]);

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

26 of 31 4/16/2009 2:23 PM

Page 27: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

title('Tmax Boxplot');

axis square;

% box plots graphically show the quartiles, extents, and outliers of data

% in an easy to read way. This allows for quick comparison of the

% characteristics of two sets of the same type of data. I'm not sure what

% criteria is used to find the whisker though.

3/5: QQ-Plots 4/5

% plot the QQ-plot for tmax values for the two data sets

figure(1);

qqplot(canandaigua(:,4),ithaca(:,4)); bigger plot was required for comparison, boxplots not required

axis square;

xlabel('Canandaigua Quantiles');

ylabel('Ithaca Quantiles');

% make the same plot manually

figure(2);

plot(sort(canandaigua(:,4)), sort(ithaca(:,4)), '+');

axis square;

xlabel('Canandaigua Quantiles');

ylabel('Ithaca Quantiles');

% using qqplot is nicer because it's easier to implement, and it also

% includes the linear best fit line of the data as part of the output. This

% is possible manually too, but it'd be a pain in the neck to compute and

% add to the scatter plot

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

27 of 31 4/16/2009 2:23 PM

Page 28: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

close all;

3/6: more QQ-plots 3/5, please go through the solution

% display a QQ-plot of the Canandaigua tmax vs. a Gaussian of it

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

28 of 31 4/16/2009 2:23 PM

Page 29: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

d = ((1:nDays)-0.5)/nDays;

meanCtmax = mean(canandaigua(:,4));

stdCtmax = std(canandaigua(:,4));

qCGauss = norminv(d,meanCtmax,stdCtmax);

qqplot(qCGauss, canandaigua(:,4));

axis square;

ylabel('Canandaigua Quantiles');

xlabel('Gaussian Quantiles');

3/7: Gaussian comparison 5/5

% display two normal probabiliy plots

subplot(1,2,1);

normplot(canandaigua(:,4));

axis square;

subplot(1,2,2);

probplot('normal',canandaigua(:,4));

axis square;

% these plots compare the data to a gaussian distribution, where if the

% plots are a straight line, then you can say that the data fits the

% distribution. PROBPLOT is more general, because it allows you to check

% against other types of distributions as well.

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

29 of 31 4/16/2009 2:23 PM

Page 30: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

3/8: Printing graphs to files

close all;

% recreate 3/6 plot

qqplot(qCGauss, canandaigua(:,4));

axis square;

ylabel('Canandaigua Quantiles');

xlabel('Gaussian Quantiles');

% Print to a color eps file

print -depsc 'myFigure.eps'

% Print to jpeg file

print -djpeg 'myFigure.jpg'

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

30 of 31 4/16/2009 2:23 PM

Page 31: Solutions for Geog 210C Labchris/Medrano_GEO 210C/GEO 210C - Spatial...Solutions for Geog 210C Lab Assignment #1 STUDENT NAME: Antonio Medrano DATE COMPLETED: 4/10/08 Contents Instructions

Published with MATLAB® 7.4

Solutions for Geog 210C Lab Assignment #1 file:///C:/Documents%20and%20Settings/Micah/My%20Documents/Doc...

31 of 31 4/16/2009 2:23 PM