%% Week 1 -- Computational Tools for Macroeconomics using MATLAB
%% In class examples

%% Housekeeping
clc; clear; close all;  % Clear command window, workspace, and close figures

% Parameters and simple computation
alpha = 0.33;
k = (1:10)';                 % capital grid column vector
y = k.^alpha;                % production function y = k^alpha

plot(k, y); xlabel('k'); ylabel('y');
title('Production Function y = k^{\alpha}');

%%%%%%%%%
clc; clear; close all;  % Clear command window, workspace, and close figures


a = 5;                  % Scalar
v = [1, 2, 3];          % Row vector
u = [1; 2; 3];          % Column vector
M = [1 2 3; 4 5 6];     % 2x3 matrix

%%%%%%%%%
clc; clear; close all;  % Clear command window, workspace, and close figures

v = [10, 20, 30, 40, 50];
v(1)        % first element -> 10
v(end)      % last element  -> 50
v(2:4)      % slice -> [20 30 40]

M = [1 2 3; 4 5 6; 7 8 9];
M(2,3)      % element at row 2, col 3 -> 6
M(:,2)      % entire 2nd column -> [2; 5; 8]
M(1:2,:)    % first 2 rows, all columns

%%%%%%
clc; clear; close all;  % Clear command window, workspace, and close figures
tic;
for i = 1:1000
    x(i) = i^2;
end
toc

tic;
x = zeros(1,1000);
for i = 1:1000
    x(i) = i^2;
end
toc


%%%%%%
clc; clear; close all;  % Clear command window, workspace, and close figures
A = [1 2; 3 4];
B = [5 6; 7 8];

A * B     % matrix multiplication (2x2 * 2x2)
A .* B    % elementwise multiplication
A.^2      % elementwise square


%%%%%%
clc; clear; close all;  % Clear command window, workspace, and close figures
v = [1, 2, 3, 4, 5];

mean(v)     % average -> 3
sum(v)      % sum -> 15
max(v)      % maximum -> 5
std(v)      % standard deviation
plot(v)     % quick plot



%%%%%%
clc; clear; close all;  % Clear command window, workspace, and close figures
x = 0:0.1:10;
y = sin(x);

plot(x, y)
xlabel('x-axis')
ylabel('sin(x)')
title('Sine function')



%%%%%%
clc; clear; close all;  % Clear command window, workspace, and close figures
t = 2000:2010;
GDP = [1.5 1.7 2.0 2.3 2.5 2.7 2.9 3.1 3.4 3.6 3.8];
C   = [1.2 1.3 1.5 1.6 1.8 1.9 2.0 2.2 2.3 2.5 2.6];

plot(t, GDP, '-o', t, C, '--*')
xlabel('Year')
ylabel('Trillion USD')
title('GDP and Consumption')
legend('GDP','Consumption')