This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 10/6/08 11:47 AM C:\Documents and Settings\gtj223\Desktop\Matlab...\NMHW5num328.m %Homework 5
%Problem 1 Chapter 3 Number 28 %This is the script file. It will have a function that opens
%the file and outputs the information including the column
%vector that contains the years and the giant matrix that
%contains the rain data. %Another function that will calculate the yearly preciptation,
%the average, lowest, and highest precipitation. %The last function will plot the yearly data. %Opens the file and creates the column vector years and the
%matrix precip.
(years, precip] = corvRain %Calculates the totals per year, the average per year, the highest %year and the lowest year. [yearTotal, yearAverage, yearLow, yearHigh] = corvRainCalculate(precip) pause;
%Plots the annual precipitation
plotRain(years, yearTotal) 1 of 1 10/6/08 11:48 AM C:\Documents and Settings\gtj223\Desktop\Matlab Pr...\corvRain.m 1 of 1
W function [years, precip] = corvRain
%corvRain Gets the years column vector and the % precipitation matrix %Synopsis: {years, precip] = corvRain' %Input NONE. You could input something like % Rainfile but it isn't necessary.
%Output: years = The column vector that will % contain all the years of the data. % % precip = Short for precipitation. It % will contain the rain values for every
% month for every year in hundredths of
% an inch. It will be a matrix that is % 105 x 12. The 105 is the number of years
% and the 12 is the number of months. o\° %Opens file %Gets headings %Gets second heading
%Reads in the data
%into one column vector
%Close the file fid = fopen('CorvRain.dat');
headingl = fgetl(fid);
heading2 = fgetl(fid);
d = fscanf(fid, '%f'); fclose(fid);
Info = reshape(d, 13, length(d)/13)'; %Reshapes it into a 105 x 13 %Outputs
years = Info(:,1);
precip = Info(:,2:13); %years becomes the lst column
%precip becomes columns 2:13 %NOTE: The precip matrix will still be in hundredths of an inch. 10/6/08 11:48 AM. C:\Documents and Settings\gtj223\Desktop\...\corvRainCalculate.m W function [yearTotal, yearAverage, yearLow, yearHigh] = corvRainCalculate(precip)
%corvRainCalculate Calculates all the information we are seeking do %Synopsis: [yearTotal, yearAverage, yearLow, yearHigh] % = corvRainCalculate(precip) %Input precip = The 105 x 12 matrix that contains the rain for
every month of every year in hundredths of an inch. Output: I yearTotal = The total rain for a given year in inches. yearAverage = The average rainfall for a given year.
yearLow = The lowest rainfall in any given year.
yearHigh The highest rainfall in any given year. o\° o\° a\° o\°v o\° a\° c\° %I used the sum function built in to Matlab. The ,2 means sum the rows. The
%Matlab default is sum the columns which you could write sum(precip) or %sum(precip,1). yearTotal = sum(precip,2)/100; A = size(yearTotal); %This is the equivalent of the length function K %but for a matrix. yearAverage = sum(yearTotal)/A(1,l); yearLow = min(yearTotal); %Minimum function
keyboard; ' yearHigh = max(yearTota1); %Maximum function %You could have done it like this: {D = size(precip);
sumYears = zeros(1,A(1,l));
irow = O; jcol = O; for irow = 1:A(l,1)
for jcol = 1:12
sumYears(irow) = sumYears(irow) + precip(irow,jcol);
end
end yearTotal = sumYears/loo; keyboard; yearAverage = sum(yearTotal)/A(1,l);
yearLow = min(yearTotal); yearHigh = max(yearTotal); o\° o\° o\° a\° o\° o\° o\° o\° o\° o\° o\° n\° o\° o\° o\° %This is the exact same thing. The for loops cycle through the matrix
%row at a time. %yearTotal divided by the number of years. 1 of 1 10/6/08 11:48 AM C:\Documents and Settings\gtj223\Desktop\Matlab Pr...\plotRain.m 1 of 1 MW function plotRain(years, yearTotal) % plotRain Creates a plot with the years and their rainfall : Synopsis: plotRain(year, yearTotal) : Input: years = Column vector with all the years % yearTotal = Total rainfall for a given year in inches
: Output None The plot will automatically output. o\° plot(years,yearTotal);
x1abel('Years');
ylabel('Inches of Rain (in)');
title('Annual Precipitation'); 2000 1 980 0 6 9 1
n
.m
t
a
ulln
.m. c 0 e 4 r 9 P 1
a
U
n
n
A 0 2 9 1 0 0 9 1 0 8 0 5 0 5 0 5 0 5 0w 6 5 ‘ 5 4 4 3 3 2 2 Ev 5mm 6 85:. Years 10/6/08 11:49 AM MATLAB Command Window 1 of 3 K>> yearLow
yearLow = 22.9900 K>> yearAverage
yearAverage = E
40.2804 I g
i I [ K>> yearHigh
??? Error using ==> keyboard
Undefined function or variable 'yearHigh'. K>> return
yearTotal = 34.8200
44.8900 .
30.8800‘ I
43.0400 V L
46.6900
43.7400 1
57.7600
43.6100
35.0100
49.6900
38.4900
41.3600
54.0200
34.1300
56.2100
34.3900
45.7100
50.2300
33.2900
50.3100
38.5200
38.4600
45.9500
35.7600
44.1300
36.8200
46.6500
39.1300
33.6200 g
50.5700 E
37.1200 %
E f f E 41.0500 MATLAB Command Window 2 of 3 10/6/08 11:49 AM
W 38.4500 ’
37.0700 ‘ E
37.8700 2
34.0700 V
40.0400 5
47.8700
39.2700
24.4500
23.6800
39.1300
36.9400
42.5900
35.4200 5
26.3500 g
32.1100 §
58.0600 .
32.0400 1
26.3300 I
40.3600 [
32.9500 é } é f 39.2000
31.5300
22.9900
37.7900
33.4200
33.9100
40.1400
30.4600 I
48.5800 E
38.3000 3
27.6800
50.2100
45.7300
47.4100 ’ . 4
40.5900 E
39.2900 1
42.7200
32.8400 g
42.9800
44.1700 ;
34.5800 I
38.5400
44.7000
37.1200 L
39.9900 1 35.5400 ,
58.7400 1
43.5000 !
53.0800 '
57.1500 1
47.0600 E
51.4400 4 g 10/6/08 11:49 AM
W 50.
38.
29.
41.
37.
42.
42.
47.
46.
55.
48.
27.
.7500
37.
37.
29.
38.
34.
.4200
35.
37. 43 33 yearAverage 40. 7600
9700
7300
9100
6400
0600
1300
0800
7500
5300
5700
1500 7100
5200
6400
8100
0000 9900
9300 2804 yearLow = 22. 9900 yearHigh = 58. 7400 MATLAB Command Window 3 of 3 10/6/08 11:51 AM C:\Documents and Settings\gtj223\Desktop\Matlab ...\NMHW5num45.m 1 of 2 W %Homework 5
%Problem 2: Chapter 4 number 5 %Take a column vector of data and convert it into traffic for
%for a given hour. fid = fopen(‘traffic.dat');
Info = fscanf(fid, '%f');
fclose(fid); %Now we have the column vector called Info that contains the '
%times for every car passing a given point on this street. %So we want to return the number of cars per hour. %We need to break up the giant column vector into cars prior to
%a given hour. i=0;
m=O;
TotalCars = zeros(1,24); %Cars is a row vector with 24 columns %The outer for loop goes for each hour. So we will check the entire
%column vector Info for values that match each hour with the inside
%for loop. In our case, the data is already sequentially ordered but
%that is not always the case. So in the instance of data that is
%scambled, we will check the entire column vector for each given hour. %If you wanted to truncate your search assuming the data is already
%ordered, you could add an if statement with a break. a
a 1 for i = 1:24; %For every hour
for m = l:length(Info) %From the first value til the last 9
if Info(m)<i %If the value is less than the hour: TotalCars(1,i) = TotalCars(1,i) + 1; g
%Adds a new increment to TotalCars. For every car that
%passed before a given hour (i), TotalCars increments %by l. ' E end E
% end
end %So we just created a vector TotalCars that gives the cummulative
%cars that have passed up to that point. In other words, TotalCars(l,10)
%will be the total cars that have passed prior to hour 10. %will subtract all values that aren't in a given hour. Cars = zeros(1,24); I
i
E
i
f
%So to get the cars per hour, we have to write something that %
Cars(1,1) = TotalCars(l,l); g I 10/6/08 11:51 AM C:\Documents and Settings\gtj223\Desktop\Matlab ...\NMHW5num45.m 2 of 2
M %We have to establish Cars(1,1) because TotalCars(1,i—1) for i = 1
%is out of bounds (i.e. TotalCars(l,O) doesn't exist). for i = 2:24
Cars(1,i) = TotalCars(1,i)Tota1Cars(1,i—l);
end %So the for loop takes the Total Cars prior to a given hour and
%subtracts the value of the TotalCars for the previous hour.
%In other words, if 100 cars have passed prior to 6AM and 300
%cars have passed prior to 7AM, the cars that passed in the 7th
%hour is equal to the total (300) minus the prior (100). Cars a::::=:=‘:::::::::::::::z:22::2::::::=:::::::::=:::::::::::::::::::::::::: %If you wanted to do it all at one time, you could have used an if—else
%statement inside your outside for loop. a\° Cars = zeros(l,24); °\° o\° % for Condition % for Condition '% Statements % end % % if i == 1 % Cars(1,i) = TotalCars(1,i);
% else % Cars(1,i) = TotalCars(1,i)—TotalCars(1,il);
% end % end o\° Cars 10/6/08 11:51 AM MATLAB Command Window 1 Of 1
W Cars = Columns 1 through 16 3 3 0 l 3 12 123 348 328 127 94 169 172 100 127!
230 Columns 17 through 24 355 384 280 129 67 54 33 13 >> ...
View
Full Document
 Spring '06
 Klaus
 Dot Product, Covariance and contravariance of vectors, Column vector, yearAverage, TotalCars

Click to edit the document details