%% ЧщсйкЮ РбсемвплЮ ме ЕрйцЬнейет ФЬузт
% *ЦбЯдщн КхсйddбкЯдзт*
%
% ФмЮмб ГещгсбцЯбт РбнерйуфзмЯпх БйгбЯпх

%% ЕйубгщгЮ дедпмЭнщн вспчьрфщузт уе уфбимпэт кбй рлзспцпсЯет
load bayAreaData.mat
who

%% 
x = bayprecip(:,2);
y = bayprecip(:,3);
z = bayprecip(:,4);
N = numel(z)

%%
stem3(x,y,z,'filled');
xlabel('x'); ylabel('y'); zlabel('precip');
hold on
plot(baycoast(:,1),baycoast(:,2),'k-'); 
axis square
hold off

%% 
X = [ones(N,1) x y];
bHat = regress(z,X)
zHat = X*bHat;

%%
stem3(x,y,z,'filled');
xlabel('x'); ylabel('y'); zlabel('precip');
hold on
scatter3(x,y,zHat,'filled','MarkerFaceColor','r');
plot(baycoast(:,1),baycoast(:,2),'k-'); 
axis square
hold off

%% 
X2 = [ones(N,1) x y x.*y x.^2 y.^2];
bHat2 = regress(z,X2)
bHat2(2:end)
zHat2 = X2*bHat2;

%%
stem3(x,y,z,'filled');
xlabel('x'); ylabel('y'); zlabel('precip');
hold on
scatter3(x,y,zHat2,'filled','MarkerFaceColor','r');
plot(baycoast(:,1),baycoast(:,2),'k-'); 
axis square
hold off

%%
GridSpecsFine = [300/6 -123.5 0.0083*6; 360/6 36 0.0083*6];
xyFine = makegrid(GridSpecsFine);
xFine = xyFine(:,1); 
yFine = xyFine(:,2);
Nfine = numel(xFine)

%%
stem3(x,y,z,'filled');
xlabel('x'); ylabel('y'); zlabel('precip');
hold on
plot(xFine,yFine,'k.');
plot(baycoast(:,1),baycoast(:,2),'r-'); 
axis square
hold off

%% 
Xfine = [ones(Nfine,1) xFine yFine];
zHatFine = Xfine*bHat;

%%
stem3(x,y,z,'filled');
xlabel('x'); ylabel('y'); zlabel('precip');
hold on
plot3(xFine,yFine,zHatFine,'r.');
plot(baycoast(:,1),baycoast(:,2),'k-'); 
axis square
hold off

%% 
Xfine2 = [ones(Nfine,1) xFine yFine xFine.*yFine xFine.^2 yFine.^2];
zHatFine2 = Xfine2*bHat2;

%%d
stem3(x,y,z,'filled');
xlabel('x'); ylabel('y'); zlabel('precip');
hold on
plot3(xFine,yFine,zHatFine2,'r.');
plot(baycoast(:,1),baycoast(:,2),'k-'); 
axis square
hold off