%% 1

% (a) Create the vector x starting on the next line:
x = -2:0.25:3;

% (b) Compute and plot the function starting on the next line:
y = x.^4 - 2*x.^3 - 3*x.^2;

% (c)
plot(x, y);




%% 2

% (a) Create variables A, a, b
A = 2;
a = 0.246;
b = 0.806;

% (b) Create the vector x starting on the next line:
x = linspace(0, 20, 75);

% (c) Compute and plot the function starting on the next line:
y = A * exp(-a * x) .* sin(b * x);

% (d)
plot(x, y);



%% 3

% (a) Create the vector x starting on the next line:
x = linspace(-10, 10, 120);

% (b) Compute and plot the function starting on the next line:
y = sin(pi * x) ./ (pi * x);

% (c)
plot(x, y);


%% 4
x = randi([-5 5], 1);
y = 0;

if x <= 5
    y = 1;
end


%% 5
x = randi([-5 15], 1);
y = 0;

if x >= 0 && x <= 10
    y = 1;
end



%% 6
x = randi([-5 15], 1);
y = 0;

if x < 0 || x > 10
    y = 1;
end


%% 7
x = randi([-5 35], 1);
y = 0;

if (x > 0 && x < 10) || (x > 20 && x < 30)
    y = 1;
end


%% 8
x = randi([0 15], 1);
y = 0;

if rem(x, 3) == 0 || rem(x, 5) == 0
    y = 1;
end


%% 9
x = randi([-5 15], 1);
y = 0;

% in this particular question it does not matter which condition
% you check first because they are all mutually exclusive
if x < 0 || x > 10
    y = 1;
elseif x == 0
    y = 2;
elseif x == 10
    y = 3;
end


%% 10
x = randi([0 15], 1);
y = 0;

if rem(x, 15) == 0
    y = 3;
elseif rem(x, 3) == 0
    y = 1;
elseif rem(x, 5) == 0
    y = 2;
end


%% 11
x = randi([-5 15], 1);

% mathematically you can negate the two logical conditions and
% switch || to && 
if ~(x < 0) && ~(x > 5)
    disp('do something')
end

% but most programmers would probably change the comparison 
% operators and switch the || to &&
if x >= 0 && x <= 5
    disp('do something')
end


%% 12

% starting with tf equal to false
tf = false;
if x ^ 2 + y ^ 2 < 1
    tf = true;
end

% OR
% starting with tf equal to true
tf = true;
if x ^ 2 + y ^ 2 >= 1
    tf = false;
end


% OR
% using an if-else statement
if x ^ 2 + y ^ 2 >= 1
    tf = false;
else
    tf = true;
end


%% 13

% starting with tf equal to false
tf = false;
if (x - xc) ^ 2 + (y - yc) ^ 2 < R ^ 2
    tf = true;
end

% OR
% starting with tf equal to true
tf = true;
if (x - xc) ^ 2 + (y - yc) ^ 2 >= R ^ 2
    tf = false;
end


% OR
% using an if-else statement
if (x - xc) ^ 2 + (y - yc) ^ 2 >= R ^ 2
    tf = false;
else
    tf = true;
end


%% 14

% make some random values
nOutlier = randi([0 5], 1);
nUnusual = randi([0 5], 1);
meas = randi([-6 6], 1);

% (a)
if meas < -3 || meas > 3
    nOutlier = nOutlier + 1;
end

% (b) the easiest way to solve this problem is to check for the
%     outlier cases first
if meas < -3 || meas > 3
    nOutlier = nOutlier + 1;
elseif meas < -2 || meas > 2
    nUnusual = nUnusual + 1;
end

% if you check for the unusual cases first then you require
% different conditions
if (meas < -2 && meas > -3) || (meas > 2 && meas < 3)
    nUnusual = nUnusual + 1;
elseif meas < -3 || meas > 3
    nOutlier = nOutlier + 1;
end


%% 15
x = zeros(1, 10);
for i = 1:10
    x(i) = i;
end


%% 16

% the values change going down the columns (row by row) so it is
% easiest to put the row loop inside the column loop
y = zeros(4, 3);
x = 100;
[rows, cols] = size(y);
for c = 1:cols
    for r = 1:rows
        y(r, c) = x;
        x = x + 1;
    end
end


%% 17

y = [8 1 6; 3 5 7; 4 9 2];
z = zeros(numel(y), 1);
[rows, cols] = size(y);

% the values are taken going down the columns (row by row) so it is
% easiest to put the row loop inside the column loop

i = 1;  % index for z
for c = 1:cols
    for r = 1:rows
        z(i) = y(r, c);
        i = i + 1;
    end
end


%% 18
z = randi([1 5], 1, 5);
n = numel(z);

% accumulate the sum into zrms and then compute the correct rms value
zrms = 0;
for i = 1:n
    zrms = zrms + z(i) ^ 2;
end
zrms = sqrt(zrms / n);


%% 19
z = randi([1 5], 1, 5);
n = numel(z);

% accumulate the sum into zh and then compute the correct harmonic mean
zh = 0;
for i = 1:n
    zh = zh + 1 / z(i);
end
zrms = n / zh;


%% 20

% almost identical to Q19
r = randi([1 5], 1, 5);
n = numel(z);

% accumulate the sum into rtotal and then compute the correct resistance
rtotal = 0;
for i = 1:n
    rtotal = rtotal + 1 / r(i);
end
rtotal = 1 / rtotal;


%% 21
y = randi(100, [1 20]);

yMax = -Inf;  % or any value smaller than the minimum value in y
for i = 1:numel(y)
    yi = y(i);
    if yi > yMax
        yMax = yi;
    end
end

% OR assume the first element of y is the greatest value
yMax = y(1);
for i = 2:numel(y)  % now start with the second element
    yi = y(i);
    if yi > yMax
        yMax = yi;
    end
end



%% 22
y = randi(100, [1 20]);

yMin = Inf;  % or any value greater than the maximum value in y
for i = 1:numel(y)
    yi = y(i);
    if yi < yMin
        yMin = yi;
    end
end

% OR assume the first element of y is the smallest value
yMin = y(1);
for i = 2:numel(y)  % now start with the second element
    yi = y(i);
    if yi < yMin
        yMin = yi;
    end
end



%% 23
y = randi(100, [1 20]);

yMax = y(1);
iMax = 1;
for i = 2:numel(y)
    yi = y(i);
    if yi >= yMax    % >= is needed to get the last occurrence of the max value
        yMax = yi;
        iMax = i;
    end
end


%% 24
y = randi(100, [1 20]);

yMax = y(1);
iMax = 1;
for i = 2:numel(y)
    yi = y(i);
    if yi > yMax    % > is needed to get the first occurrence of the max value
        yMax = yi;
        iMax = i;
    end
end
















