CS 100: Programming Assignment P6

Solution


% Script File: P6Demo.
% Plots Ithaca temperature data over the span of a year. Displayed are the record high, the average 
% high, the average low, and the record low.

D = ithacaData;
mStr = '     Jan    Feb   Mar    Apr    May   Jun    Jul    Aug    Sep   Oct    Nov    Dec';
m = [1 32 60 91 121 152 182 213 244 274 305 335 366];
close all                            % Closes all open figure window-a good habit
plot(D')
axis([1 365 -40 120])                % Horizontal range from 1 to 365 and vertical range -40 to 120
title('Ithaca Weather Data')         % Put a title on top.
ylabel('Temperature (F^{o})')        % Label the y-axis
set(gca,'Xtick',m,'XtickLabel',[])   % Put tick marks at the xvalues named by m but don't label
text(-10,-50,mStr)                   % Display the string mStr along the bottom edge
grid on                              % Show the grid lines



% Script File P6A.
% Displays the hot and cold days

close all
D = ithacaData;
mStr = '     Jan    Feb   Mar    Apr    May   Jun    Jul    Aug    Sep   Oct    Nov    Dec';
m = [1 32 60 91 121 152 182 213 244 274 305 335 366];
plot(D')
axis([1 365 -40 120])
title('1998 Hot and Cold Days')
ylabel('Temperature (F^{o})')
set(gca,'Xtick',m,'XtickLabel',[])
text(-10,-50,mStr)
grid on
LastYear = D98;
hotDays  = find(LastYear(1,:)> =(D(1,:)+D(2,:))/2);
coldDays = find(LastYear(2,:)< =(D(3,:)+D(4,:))/2);
hold on
plot(hotDays,LastYear(1,hotDays),'or',coldDays,LastYear(2,coldDays),'ob')
hold off


  function y = modelHigh(t)
% Yields the approximate average high temperature to be expected on day t.
% Days are indexed from 1 (= Jan 1) to 365 (= December 31)
y = 55.8301 - 25.1551*sin( (t+70.0800)*(2*pi/365));

  function y = modelLow(t)
% Yields the approximate average low temperature to be expected on day t.
% Days are indexed from 1 (= Jan 1) to 365 (= December 31)
y = 36.3452 + 21.7682*sin( (t-115.4738)*(2*pi/365));















% Script File P6B
% Displays the error in two models that predict the average high and average low.
close all
D = ithacaData;
mStr = '     Jan    Feb   Mar    Apr    May   Jun    Jul    Aug    Sep   Oct    Nov    Dec';
m = [1 32 60 91 121 152 182 213 244 274 305 335 366];

err = D(2,:) - modelHigh(1:365);
plot(err)
axis([1 365 -3 3])
title('Error in Daily High Model')
ylabel('Degrees (F^{o})')
set(gca,'Xtick',m,'XtickLabel',[])
text(-7,-3.3,mStr)
grid on

figure
err = D(3,:) - modelLow(1:365);
plot(err)
axis([1 365 -5 5])
title('Error in Daily Low Model')
ylabel('Degrees (F^{o})')
set(gca,'Xtick',m,'XtickLabel',[])
text(-7,-5.5,mStr)
grid on




% Script File P6C
% Bar plot showing the monthly average highs and lows.

close all
D = ithacaData;
m = [1 32 60 91 121 152 182 213 244 274 305 335 366];
highs = zeros(1,12);
lows = zeros(1,12);
for k=1:12
   H = D(2,m(k):m(k+1)-1);
   highs(k) = sum(H)/length(H);
   L = D(3,m(k):m(k+1)-1);
   lows(k) = sum(L)/length(L);
end
bar(highs,'r')
hold on
bar(lows,'b')
hold off
axis([0 13 0 90])
title('Average High and Low Temperature')
ylabel('Temperature (F^{o})')
set(gca,'XTickLabel','Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec')