%% Problem 0
% copy the variables from the file we just created.

% repeat the calculation
n_student = n_matlab + n_python + n_beginner;
matpy_ratio = n_matlab / n_python;

% now let's try a bit more complicated calculations
% 1-placing students on a row of 4 chairs, how many rows do we have?
n_row  = (n_student+2) / 4
n_row_int = ceil((n_student+2)/4) %you can also try round, floor
% 2-how many students are left on the last row?
n_lastrow = mod((n_student+2),4)

% 3-placing students onto a right triangle?
a = n_python;
b = n_beginner;
csqr = a^2 + b^2;
c = sqrt(csqr);

% 4-placing students onto a circle with each person ocupying 1 meter, how
% big the diameter would be?
% remember: perimeter = pi * diameter
diam = n_student/pi;
%% Problem 0, vectors
vec_psy = [n_psy_mat,n_psy_py,n_psy_bgn]; %row vector
vec_neu = [n_neu_mat,n_neu_py,n_neu_bgn];
vec_other = [n_other_mat,n_other_py,n_other_bgn];

% add up vectors 
vec_psyneu = vec_psy + vec_neu; % what's the shape of the result?

% what's the total number of psychology students?
n_psy = vec_psy(1)+vec_psy(2)+vec_psy(3) % accesing each number
% alternatively
n_psy2 = sum(vec_psy); % sum is a function on vector

% can you use inner product to make the sum operation happen?
% example of inner product:
dot([1,0],[1,0])
dot([1,0],[0,1])

n_psy3 = dot(vec_psy,[1,1,1]) % yes, vectors can be a operator!
% always check your results
n_psy == n_psy2; % == checks equivalence

%% Problem 0, matrices
mat_psyneu = [vec_psy;vec_neu]
size(mat_psyneu)
% what is the shape if we use comma instead of semicolon?
% try: temp = [vec_psy,vec_neu]

% what's the total number of psychology and neuro students?
% way 1: use sum function, apply on the matrix
% sum(mat_psyneu), this won't work because of the dimension priority of
% matlab.
sum(mat_psyneu,2)
% or transpose the matrix
sum(mat_psyneu')

% way 2: use inner product
% this will not work: sumvec = [1,1,1]; % row vector
sumvec = [1;1;1]; % column vector
mat_psyneu * sumvec

% bonus problem: if we want to get the difference of number of matlab and 
% python programmer in psych and neuro, what vector to use?

%% Problem 1
clear % clear the workspace (which should be empty now!)

% a) copy the line below into command window
sqrt(1^2+1^2) 
% alternative: (1^2+1^2)^?
% you can also try this and see why it doesn't work: (1^2+1^2)^1/2 

% b) copy the line below into command window
ans*pi
% ans is automatically generated by Matlab and is always changing
% pi is a constant that matlab provides.
% check your "Workspace" window and see what's there!

% c) copy the line below into command window, or simply press the up arrow
% key to get back the command then add a semicolon.
ans*pi;
% did the "ans" variable change?
%%
% d) name the variables
a = 1;
b = 1;
c = sqrt(a^2+b^2); % hypotenuse of the right angle
disp(c) % for your homework, if we ask for numerical results please display them.
perimeter = c * pi; % perimeter of circle
disp(perimeter)

% open a new window to save these in to a new script.
% then run the script.

%% Problem 2
clear % clear the workspace (which should be empty now!)

% a)
vec_a = [1,0]; % use brackets to represent a vector (or matrix as in problem 3).
vec_b = [0 1]; % use either blank space or comma to delineate entries.
vec_c = vec_a  - vec_b; 
len_vc = sqrt(vec_c(1)^2 + vec_c(2)^2);
disp(len_vc)
disp(c == len_vc) % check if it's the same 

% b) other ways of calculating vector length?
len_1 = sqrt(sum(vec_c.^2)); 
len_2 = sqrt(sum(vec_c.*vec_c))
%len_3 = sqrt(dot(vec_c,vec_c))
%len_4 = norm(vec_c)
len_5 = sqrt(vec_c*vec_c') 
% display vec_c' to see what does ' do?

% c)
costheta = dot(vec_c,vec_a)/len_vc;
theta = acos(costheta); % check the help function if you are not sure what acos does.
uni_c = vec_c * (1/len_vc);
disp(norm(uni_c))

univec_c = [cos(theta),sin(theta)];
univec_c1 = vec_c/len_vc;
% you may try acosd() function to show the result in degree
vec_d = len_vc * [cos(theta*2),sin(theta*2)];
vec_e = len_vc * [cos(theta*3),sin(theta*3)];

% d) plot!
h=figure; % this starts a new figure window.
plot([0,vec_a(1)],[0,vec_a(2)])
hold on
plot([vec_a(1),vec_a(1)-vec_b(1)],[vec_a(2),vec_a(2)-vec_b(2)])
hold on
plot([0,vec_c(1)],[0,vec_c(2)])
%plotv([vec_c',vec_d',vec_e'])
% please plot the last two vectors


% e) adjust
xlim([0,1.5])
ylim([-1.5,0.5])
title('lovely vectors')
xlabel('x')
ylabel('y')
legend(['a';'b';'c'])

% f)
saveas(h,'lovely_vector')
saveas(h,'lovely_vector.png') % if don't specify the format it will be saved as .fig which can only be used by Matlab

%% Problem 3
clear % clear the workspace (which should be empty now!)

% a)
income = 1500 * ones(12,1);

% b)
rent = 1200; % welcome to use your own estimation! This is mine.
grocery = 500;
clothing = 300;
fun = 500;
expense  = repmat([rent,grocery,clothing,fun],12,1);

% c)
income(7) = ?

% d)
saving = ?

% e) 
disc = linspace(1,0.8,12)';
expense(2:3,:) = expense(2:3,:).* repmat(disc,?);

% f)
saving = ?
figure;
bar(saving)
ylabel('saving')
title('monthly saving when you know how to live with budget in nyc')

% g)
fun_noise = 100*randn(12,1);
expense(:,end) = expense(:,end)+fun_noise;
figure;
saving = ?
bar(saving)
ylabel('saving')
title('monthly saving when the fun expense has random noise')

figure;
hist(fun_noise)
% if it doesn't look like gaussian, try increase sample number, like
% hist(100*randn(1200,1))

% h)
credit_rew = [0,.01,.02,.03; 0,.02,.02,.01;0.001,.02,.01,.01];
my_rew = ?
% what is the size of my_rew matrix?
matsize = ?

[max6,best6] = max(sum(my_rew(1:6),1));
[maxall,bestall] = max(sum(my_rew,1));

sprintf('the best card for the first six month is card %d',best6)
sprintf('the best card overall is card %d',bestall)












