%% Sample solutions for test 1 practice questions

%% 1.29
r = 5;
theta = 30;
x = r * cosd(30)
y = r * sind(30)

%% 1.31
N = 2;
wt = 10;
minWt = (1 - N / 100) * wt
maxWt = (1 + N / 100) * wt

%% 1.32
r = 10;
C = 32430 / r + 428 * pi * r

%% 1.33
A = 100;
x = 1000;
C = A / x * sqrt(2 / (pi * exp(1)))

%% 1.34
nthroot(1.15 * 1.5 * 1.3, 3)

%% 2.6
[-1:0.5:1]'

%% 2.7
xEvenLen = 1:10
xEvenLen(1:2:end)

xOddLen = 1:9
xOddLen(1:2:end)

%% 2.8
mat = [7:10; 12:-2:6]
mat(1, 3)
mat(2, :)
mat(:, 1:2)

%% 2.9
mat = ones(2, 4);
nElements = numel(mat);
prodOfDim = size(mat, 1) * size(mat, 2);
nElements == prodOfDim

%% 2.10
mat = ones(2, 4);
mat(1, :) = 1:4
mat(:, 3) = [0; 0]

%% 2.13
myc = clock;
today = myc(1:3)
now = fix(myc(4:6))

%% 2.14
mat = pascal(5);
last = mat(end, end)
last = mat(size(mat, 1), size(mat, 2))
last = mat(numel(mat))
last = mat(size(mat, 1) * size(mat, 2))

%% 2.15
vec = 1:10;
last = vec(end)
last = vec(length(vec))
last = vec(max(size(vec)))
last = vec(numel(vec))

%% 2.24
n = 8;
denom = 1:n;
sum(1 ./ denom)

%% 2.25
n = 5;
denom = 2 .^ [0:(n-1)];
sum(1 ./ denom)

%% 2.26
numer = 3:2:9;
denom = 1:4;
sum(numer ./ denom)


%% 2.27
mat = magic(4);
colProd = prod(mat)
rowProd = prod(mat')


%% 2.33
evals = [5 3 2 5 5 4 1 2];
rev = 6 - evals


%% 2.34
v = [33 10.5 40 18 20 7.5];
hours = v(1:2:end-1);
wages = v(2:2:end);
total = hours .* wages


%% 2.40
v = 1:10;
diffV = v(2:end) - v(1:end - 1)


%% 2.43
A = [1 -3 2 4; 2 5 0 1; -2 1 5 -3; -1 3 1 2];
A11 = A(1:2, 1:2);
A12 = A(1:2, 3:4);
A21 = A(3:4, 1:2);
A22 = A(3:4, 3:4);

B = [2 1 -3 0; 1 4 2 -1; 0 -1 5 -2; 1 0 3 2];
B11 = B(1:2, 1:2);
B12 = B(1:2, 3:4);
B21 = B(3:4, 1:2);
B22 = B(3:4, 3:4);

% addition
[(A11 + B11)  (A12 + B12); (A21 + B21)  (A22 + B22)] == A + B

% subtraction
[(A11 - B11)  (A12 - B12); (A21 - B21)  (A22 - B22)] == A - B

% scalar multiplication
s = 5;
[(s * A11)  (s * A12);  (s * A21)  (s * A22)] == s * A
[(s * B11)  (s * B12);  (s * B21)  (s * B22)] == s * B


%% 2.44
[(A11 * B11 + A12 * B21)  (A11 * B12 + A12 * B22);
 (A21 * B11 + A22 * B21)  (A21 * B12 + A22 * B22)] == A * B
 

%% 3.16
X = load('q3_16.dat');
altitude = X(:, 1);
temp = X(:, 2);
plot(altitude, temp, 'bo-');
xlabel('altitude (meters)');
ylabel('temperatuer (kelvin)');
title('temperature vs altitude');


%% 3.21
X = load('floatnums.dat');
X = round(X')
save intnums.dat X -ascii


%% 3.23
X = load('hightemp.dat');
yr = 1900 + X(:, 1);
X(:, 1) = yr
save y2ktemp.dat X -ascii

%% 3.27
% see mph2mps.m

%% 3.28
%
% -all variables created in a script appear in the workspace; none of
%  of the variables in a function appear in the workspace
%
% -the results of running a script are stored in variables with names
%  defined in the script; the results of running a function can be
%  stored in whatever variable names the user chooses
%
% -you must edit and save a script to modify the input parameter values
%  used in the script (or ask the user for input); the input parameters
%  to a function can be changed everytime the function is called


%% 3.29
% see fluidvel.m


%% 3.30
% see fives.m


%% Written questions

%% W1
%
% -must begin with a letter
% -may contain letters, numbers, and underscores


%% W2
%
% * is used for scalar-array multiplication and matrix-matrix
% multiplication
%
% .* is used for element-by-element array multiplication
%
% -there is no .+ operator because matrix-matrix addition is identical
%  to element-by-element addition

%% W3
%
% -using a finite quantity of bits means that only a finite quantity of
%  integers can be represented (i.e., there must be both a minimum and
%  maximum integer value that can be represented)
%
% -any calculation that results in a value outside of the range of integers
%  that can be represented will fail in some way


%% W4
% 
% -saturation arithmetic refers to what occurs when an integer
%  calculation results in a value outside of the range of integers
%
% -if the calculation exceeds the maximum integer value then the result
%  is the maximum integer value (similarly for the minimum integer value)


%% W5
%
% -the student has created a variable named size which is also the
%  name for a (widely used) MATLAB function
%
% -the variable named size takes precedence over the function named
%  size, so the expression size(A) means use the values in A to index
%  into the variable named size instead of calling the function named
%  size to find the size of A


%% W6
%
% -because the results of two floating-point calculations that are
%  mathematically identical will rarely be exactly equal
%
% -this is caused by the fact that only a finite number of bits are
%  used to represent numbers on a computer and thus there are an
%  infinite number of real values that cannot be exactly represented
%  on a computer


%% W7
%
% -good programming style is important because it makes your code easier
%  to read, and thus, understand


%% More sample programming questions

%% 1

x = linspace(-3, 3, 100);
phi = exp(-(x .^ 2) / 2) / sqrt(2 * pi);
plot(x, phi);

% you should label the graph

%% 2

x = linspace(-10, 10, 500);
sigma1 = 1;
mu1 = 0;
phi1 = exp(-(x - mu1) .^ 2 / (2 * sigma1 ^ 2)) / (sigma1 * sqrt(2 * pi));

sigma2 = 2;
mu2 = -5;
phi2 = exp(-(x - mu2) .^ 2 / (2 * sigma2 ^ 2)) / (sigma2 * sqrt(2 * pi));

sigma3 = 0.5;
mu3 = 7;
phi3 = exp(-(x - mu3) .^ 2 / (2 * sigma3 ^ 2)) / (sigma3 * sqrt(2 * pi));

plot(x, phi1, 'b', x, phi2, 'r', x, phi3, 'g');

% you should label the graph and perhaps add a legend


%% 3

k = 0:20;
lambda1 = 1;
f1 = (lambda1 .^ k) * exp(-lambda1) ./ factorial(k);

lambda2 = 4;
f2 = (lambda2 .^ k) * exp(-lambda2) ./ factorial(k);

lambda3 = 10;
f3 = (lambda3 .^ k) * exp(-lambda3) ./ factorial(k);

plot(k, f1, 'b', k, f2, 'r', k, f3, 'g');

% you should label the graph and perhaps add a legend
