%% Lab 3
% Table of Contents
% Uppgift 1 - Systems of equations as sparse matricies
% Uppgift 2 - Least-Squares
% Uppgift 3 - Multiple Regression (Lay pg. 389)
%% Uppgift 1 - Systems of equations as sparse matricies
% Equilibrium problems with boundary conditions lend themselves well to systems of linear equations using sparse().
% In this exercise, we determine the temperature at each node, where the temperature is the average of the adjacent nodes.
% We find that the equations form a regular diagonal pattern when arranged in a matrix and divided into n*n blocks.
% Matlab solves A\b many orders of magnitude faster when using sparse.

clear all
close all

n = 30;
n2 = n.^2;

e = ones(n2, 1);

A = spdiags([-e, -e, 4*e, -e, -e], [-n, -1, 0, 1, n], n2, n2)

spy(A)

% Fix irregularities.
for k = n:n:(n-1)*n
    A(k+1, k) = 0;
    A(k, k+1) = 0;
end
spy(A)

% Create b, depending on boundary values.
b1 = 20;
b2 = 100;
b3 = 40;
b4 = 80;

% Left side
bL = [b1*ones(n,1); zeros(n2-2*n,1); b3*ones(n,1)];
bR = [b4; zeros(n-2, 1); b2]; % One block
bR = repmat(bR, n, 1); % Repeat block n times

b = bL + bR;

x = A\b;

U = reshape(x, n, n)';
U = [b4, b1*ones(1,n), b2;
     b4*ones(n,1), U, b2*ones(n,1);
     b4, b3*ones(1,n), b2]

L = 1;
h = L/(n+1);
x = 0:h:L;
y = 0:h:L;

surf(x,y,U)
contourf(x,y,U,20), colorbar
axis equal, axis tight

%
%% Uppgift 2 - Least-Squares
% We want to use least-squares approximation to fit the function y\left(t\right)=c_1 +c_2 \cos \left(\omega t\right)+c_3 \sin \left(\omega t\right) to the given data.

close all;

M = (1:12)'; % Month 
T = [-0.9, -0.9, 2, 6, 11.6, 15.5, 16.6, 16.2, 12.8, 9.1, 4.4, 1]'; % Temperature

y = @(t, omega, c1, c2, c3) c1 + c2.*cos(omega.*t) + c3.*sin(omega.*t);
close all
plot(M, T, 'bo-')

% We must choose appropriate omega.
f = 1/(12); % Guesswork based on graph.
w = 2*pi*f;
A = [ones(size(M)), cos(w.*M), sin(w.*M)]
xhat = A\T;

hold on
plot(M, y(M, w, xhat(1), xhat(2), xhat(3)))

e = norm(A*xhat - T)/sqrt(length(M))
text(6,6,['\epsilon = ', num2str(e)])
fplot(@(t) y(t, w, xhat(1), xhat(2), xhat(3)))

%
%% Uppgift 3 - Multiple Regression (Lay pg. 389)
% We have two independent variables, (v,t) and a dependent variable y that we want to "fit" to y=c_1 +c_2 t+c_3 v.
% No change in the method is required: we use the same least-squares algorithm. We construct A to contain every possible combination of t,v, with y containing the data points in the correct order. Then we solve as usual.
% The problem with Matlab is that it's tricky to construct the column vectors of A.

close all

tab = load('avkylningseffekt')
ET = tab.ET
v = tab.v;
t = tab.t;

% Linear function to fit.
y = @(t,v, c1, c2, c3) c1 + c2.*t + c3.*v;

[T,V] = meshgrid(t,v);
contourf(T,V,ET), colorbar     
xlabel('Temp'); ylabel('Wind velocity'); title('Effective Temperature')


ETv = reshape(ET, 45,1); % Reshape as column vector

n = numel(ET)

At = [] % Middle vector of A. t values.
for k = 1:length(t)
    At = [At; ones(length(v),1).*t(k)];
end

Av = repmat(v, n/length(v),1);

A = [ones(n, 1), At, Av]

x = A\ETv

c1 = x(1);
c2 = x(2);
c3 = x(3);

solv = zeros(n, 1);
for k = 1:numel(V)
    solv(k) = y(T(k), V(k), c1,c2,c3);
end
solv

sol = reshape(solv, 5,9) % Shape back into original matrix.
subplot(2,2,1)
contourf(T,V,ET);
colorbar
caxis([-70, 10])

subplot(2,2,2)
contourf(T,V,sol)
colorbar
caxis([-70, 10])

subplot(223)
surf(T,V,ET)

subplot(2,2,4)
surf(T,V,sol)