% =========================================================================
% READ ME FIRST
% =========================================================================
% (1) Be sure to enter your name, as a string, in the space below.
%     Do this now, before you forget.
% =========================================================================
% \/\/\/ ENTER YOUR NAME \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
my_name = '';
% /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
% =========================================================================
% (2) Most of the exam reqires answers (as MATLAB statements/code)
%     to be written in the spaces provided in this file.
%     If you like -- test your code at the command line,
%     and/or in a seperate m-file, before copying-pasting into this file
%     as your 'answer'.
%     The space provided in this file for the code to be inserted 
%     looks like:
% *** ANSWER  *************************************************************

% *************************************************************************
% =========================================================================
% (3) Where specified in a question, use the variable name requested.
%     i.e. a hypothetical question asking you to 
%     multiple 2 and 3 together and assign the answer to the variable A0 --
%     make sure you do use the variable A0 (exactly as specified), e.g.
% *** ANSWER  *************************************************************
A0 = 2*3;
% *************************************************************************
% =========================================================================
% (4) Some of the exam requires that an m-file is created and saved.
%     Ensure that you use the name for the m-fie requested in the question.
% =========================================================================
% (5) Some of the exam (the GUI questions) may require BOTH .m AND .fig
%     files to be created and saved.
%     Ensure that you use the name for the GUI requested in the question.
%     Ensure you save and upload BOTH files!
% =========================================================================
% (6) For the longer questions (and longer code), the marks are 
%     structured so that some marks are possible for a partial or
%     incomplete answer, and that many of the marks will be available for
%     'relatively' simple first steps and tasks.
% =========================================================================
% (7) There are FOUR (4) sections in this exam:
%     (A) single line MATLAB statements -- 10 questions -- 20 marks total
%     (B) multiple (code) line answers  -- 3 questions  -- 30 marks total
%     (C) creating short functions      -- 3 questions  -- 30 marks total
%     (D) creating Apps/GUIs            -- 2 questions  -- 20 marks total
% =========================================================================
% (8) Up to 5 BONUS MARKS will be awarded for *consistent* and
%     sufficient code commenting and formatting (e.g. indenting).
%     SO COMMENT AND FORMAT YOUR CODE WELL (throughout) FOR FREE MARKS
%     AND ... including the 10 bonus marks offered in Sections B and D, 
%     the potential maximum exam mark is ***115%***!!!
%     i.e. *DON'T PANIC* if you find you cannot do every single question
%          (it is still possible to get 100%!)
% =========================================================================
% (9) Provided on iLearn are the following files:
%     * exam_geo111_20191213.m [this file]
%     * etheridge_etal_1996.txt -- historical CO2 data file
%     * SPECMAPStack.tab -- glacial-interglacial d18O data file
%     Copies will also appear on my teaching/course webpage.
% =========================================================================
% (10) The exam is THREE (3) hours in duration and will finish at 5.00 pm.
%     Any and all paper (including books), and electronic, notes and files
%     ARE allowed.
%     Pages or searches of any internet domain ARE allowed, with the
%     exception of you cannot post questions to discussion boards.
%     (Try and avoid searching material outside of the Mathworks domains 
%     becasue it will not likely be the most productive use 
%     of your time.)
%     **NO** social media, 'phone, email, txt-ing are allowed.
%     Similarly, ALL verbal and electronic communication is NOT allowed.
%     Phones must be PUT AWAY.
% =========================================================================
% (x) UPLOAD YOUR ANSWERS, INCLUDING *THIS* COMPLETED FILE, TO iLEARN
%     In an emergency ... email me the files:
%     andy@seao2.org
%     (If you email -- I must have recieved the files by 5 pm.)
% =========================================================================
%
%
%
% *************************************************************************
% *** SECTION A ***********************************************************
% *************************************************************************
% This section requires SINGLE LINE answers (as MATLAB code).
% EXAMPLE:
% 'Write down the line of code that divides 10 by 5 and assign the result
% to the variable A0'
% would have a code answer of:
% *** ANSWER  *************************************************************
A0 = 10/5;
% *************************************************************************
% NOTE: In this section -- it does not matter whether you include the ';' 
%       at the end of the line or not.
% [20 marks total for this section]
% *************************************************************************
%
% *************************************************************************
%
% *** QUESTION 1 **********************************************************
% Write down the line of code that creates a ROW vector of (integer) values
% from 0 to 500, in steps of 5.
% Assign the resulting vector to the variable A1.
% [2 marks]
% *** ANSWER  *************************************************************

% *************************************************************************
%
% *** QUESTION 2 **********************************************************
% Write down the line of code that determines the number of elements in
% vector A1.
% Assign the resulting scalar value to the variable A2.
% [2 marks]
%
% *** ANSWER  *************************************************************

% *************************************************************************
%
% *** QUESTION 3 **********************************************************
% Write down the line of code that gives the MEAN of the values in 
% variable A1.
% Assign the resulting scalar value to the variable A3.
% [2 marks]
%
% *** ANSWER  *************************************************************

% *** QUESTION 4 **********************************************************
% Write down the line of code that removes (sets empty) all the elements in
% variable A1 that are greater than or equal to 250.
% Assign the resulting vector to the variable A4.
% [2 marks]
%
% *** ANSWER  *************************************************************

% *** QUESTION 5 **********************************************************
% Assuming that the values in A1 are in degrees, write down 
% the line of code that calculates the sine of all values in the vector A1.
% Assign the resulting vector of values to the variable A5.
% HINT: remember to convert degrees to radians ...
% [2 marks]
%
% *** ANSWER  *************************************************************

% *************************************************************************
%
% *** QUESTION 6 **********************************************************
% Write down the line of code that creates a 20x100 (20 rows, 100 columns)
% matrix, with each element having a value of zero (0).
% Assign the resulting array to the variable A6.
% HINT: zeros
% [2 marks]
%
% *** ANSWER  *************************************************************

% *** QUESTION 7 **********************************************************
% Write down the line of code that creates a 20x100 (20 rows, 100 columns)
% matrix, with each element having a value of pi.
% Assign the resulting array to the variable A7.
% HINT: you can add pi to the matrix values of A6 ...
% [2 marks]
%
% *** ANSWER  *************************************************************

% *** QUESTION 8 **********************************************************
% Write down the line of code that calculates the sum of  
% ALL of the elements in matrix A7.
% Assign the resulting scalar to the variable A8.
% [2 marks]
%
% *** ANSWER  *************************************************************

% *** QUESTION 9 **********************************************************
% Write down the line of code that takes the matrix stored in variable A7,
% and divides it (all elements) by the total sum (scalar variable A8).
% Assign the resulting array to the variable A9.
% [2 marks]
%
% *** ANSWER  *************************************************************

% *** QUESTION 10 *********************************************************
% Write down the line of code that takes the LAST COLUMN of 
% array variable A7 and calculates cosine of those elements.
% Assign the resulting (column) vector to the variable A10.
% [2 marks]
%
% *** ANSWER  *************************************************************

% *************************************************************************
% *************************************************************************
% 
%
%
% *************************************************************************
% *** SECTION B ***********************************************************
% *************************************************************************
% This section requires you to write MULTIPLE LINES (as MATLAB code)
% as the answer in the space provided.
% NOTE: A single blank line is shown, but this is not an indication of
%       how many lines are required for the answer -- use as many lines
%       as you need/want.
% HINT: Test your code answer in an m-file before 
%       copying-pasting into the space provided.
% HINT: Some (or even most!) marks can be gained even if the code 
%       does not fully work as intended.
% DONT FORGET COMMENTS AND FORMATTING!
% [30 marks total for this section]
% *************************************************************************
%
% *** QUESTION 1 **********************************************************
% Write down code that will achieve the following:
% (i)   Load in the historical atmospheric CO2 data (ppm) vs. year, 
%       contained in the file: 'etheridge_etal_1996.txt'. [2 marks]
% (ii)  Convert the absolute CO2 values into a fractional value
%       *relative* to 278 ppm
%       i.e. 278.0 (if it existed in the dataset) would become 1.0
%       i.e. 556.0 (if it existed in the dataset) would become 2.0
%       [2 marks]
% (iii) Open a new figure window, and 
%       plot the relative CO2 value (y-axis) vs. year (x-axis).
%       Plot, using *both* point symbols (your choice of shape and color),
%       *and* a blue line. [3 marks]
% (iv)  Set plot scale limits to: 1.0 to 1.2 (relative CO2 value), 
%       and 1850 to 2000 (time) [1 mark]
% (v)   Add a horizontal dashed red line, at a relative CO2 (y-axis) value 
%       equal to the MEAN of all the values [2 marks]
% (x)   For 2 BONUS marks, add a grid to the plot.
% NOTE: Appropriate / sufficient figure annotation must be present. 
% [10+2 marks total]
%
% *** ANSWER  *************************************************************

% *************************************************************************
%
% *** QUESTION 2 **********************************************************
% Write down code that will achieve the following:
% (i)   Load in the glacial-interglacial 
%       oxygen isotope data (age (kyr), d18O value (o/oo))
%       contained in the file: 'SPECMAPStack.tab'. [2 marks]
% (ii)  Plot the data as symbols only (no lines) [2 marks]
% (iii) Plot values >= 0.0 in red and < 0.0 in blue [4 marks]
% (vi)  Add a horizontal dashed black line at a d18O (y-axis) value 
%       equal to the maximum value occurring in the dataset [2 marks]
% (x)   For 2 BONUS marks, make the outlines (edges) of the symbols yellow.
% NOTE: Appropriate / sufficient figure annotation must be present. 
% [10+2 marks total]
%
% *** ANSWER  *************************************************************

% *************************************************************************
%
% *** QUESTION 3 **********************************************************
% Write down code that will achieve the following:
% (i)   Creates a figure window with no visible axes, and with 
%       axes that completely fill the window. [2 marks]
% (ii)  Draws a 4x4 grid of squares (of even size, filling the window).
%       [4 marks]
% (iii) Color the *complete rows* of squares in alternating colors:
%       (A) a light blue color
%       (B) an orange color
%       (i.e. you should end up with horizontal bands of alternating color)
%       [4 marks]
% (x)   For 2 BONUS marks, make the outlines of the squares red and thick
%       (or at least, thicker than the default)
% HINT: If you cannot get the axes to compeltely fill the figure window,
%       just make sure there are no tick marks or labels visible.
% NOTE: Appropriate / sufficient figure annotation must be present. 
% [10+2 marks total]
%
% *** ANSWER  *************************************************************

% *************************************************************************
% *************************************************************************
%
%
%
% *************************************************************************
% *** SECTION C ***********************************************************
% *************************************************************************
% This section requires you to create an m-file function
% and save this as your 'answer'.
% NOTE: DO NOT paste the code into *this* file.
% HINT: Some (or even most!) marks can be gained even if the function 
%       does not fully work as intended.
% [30 marks total for this section]
% *************************************************************************
%
% *** QUESTION 1 **********************************************************
% Create and save a function, called 'C1', with the following properties:
% (i)   The function takes as input, a single number. [1 mark]
% (ii)  The function returns a single number. [1 mark]
% (iii) The number returned by the function is equal to the 
%       reciprocal of that number [4 marks]
%       UNLESS, the input number is equal to zero, when a NaN is returned
%       instead [4 marks]
%       e.g. at the command line, you should get:
%       >> C1(2)
%          ans = 
%                5.000000000000000e-01
%       >> C1(0)
%          ans = 
%                NaN
% [10 marks total]
%
% *** QUESTION 2 **********************************************************
% Create and save a function, called 'C2', with the following properties:
% (i)   The function takes as input, a single variable 
%       (assumed to be a vector of numbers). [1 mark]
% (ii)  The function returns a single variable (assumed to be a scalar).
%       [1 mark]
% (iii) The value returned by the function is equal to largest value
%       in the input vector, divided by the second (or equal) largest
%       e.g. if the input is [1 2 3 4], then the output should be equal to:
%       4/3
%       and at the command line:
%       >> C3([4 2 3 1])
%          ans = 
%                1.333333333333333e+00
%       [8 marks]
% HINT: first determine the largest value
%       then remove that value from the vector ...
%       and determine the new largest value of the smaller now vector
% NOTE: If you just return the largest value of the input vector, 
%       you'll get half marks (and full marks for a complete answer)
% [10 marks total]
%
% *** QUESTION 3 **********************************************************
% Create and save a function, called 'C3', with the following properties:
% (i)   The function takes as input, a single variable 
%       (assumed to be a vector of numbers). [1 mark]
% (ii)  The function returns a single variable (assumed to be a scalar).
%       [1 mark]
% (iii) The value returned by the function is equal to the 
%       sum of the reciprocal of all numbers in the vector. [8 marks]
%       e.g. if the input is [1 2 3 4], then the output should be equal to:
%       1/1 + 1/2 + 1/3 + 1/4
%       and at the command line:
%       >> C3([1 2 3 4])
%          ans = 
%                2.083333333333333e+00
% HINT: One way to do this and structure the code (there are other ways):
%       (1) determine the number of elements in the vector
%       (2) loop through the number of elements
%       (3) calculate the reciprocal and add to a running total 
%       NOTE: if you do it this way, you will need to define a default 
%             value of the answer variable BEFORE the loop starts and
%             you start to multiply it with numbers from the vector
% NOTE: Any shorter and more compact answer that works is also acceptable
% NOTE: I'll give you half marks for just returning the simple 
%       sum of the numbers in the input vector.
% [10 marks total]
%
% *************************************************************************
% *************************************************************************
%
%
%
% *************************************************************************
% *** SECTION D ***********************************************************
% *************************************************************************
% This section requires you to create (using the MATLAB GUIDE GUI App) 
% a series of Apps (GUIs) (the required specifications of which, 
% are listed).
% Save BOTH the resulting m-file AND the .fig file as your answer 
% (using the specified filename).
% NOTE: DO NOT paste the code into this *file*.
% HINT: Some (or even most!) marks can be gained even if the App does not 
%       fully work as intended.
%       (If pushed for time: design it first .... but not wasting time 
%       on making it look nice, then worry about teh code after.)
% [20 marks total for this section]
% *************************************************************************
%
% *** QUESTION 1 **********************************************************
% Create an App, called 'D1', with the following properties:
% (i)   A single *white* colored Push Button, labelled 'CLICK ME'. 
%       [2 marks]
% (ii)  A program (function) action, that when you click on the push button 
%       with the mouse, the button text should:
%       (a) turn red (any shade of red) [4 marks]
%       (b) change its text to 'Thanks for clicking me!' [4 marks]
% (x)   For 2 BONUS marks, when clicking on the push button,
%       after it has changed color and text, add a delay of a few seconds
%       and then close the program
% [10+2 marks total]
%
% *** QUESTION 2 **********************************************************
% Create an App, called 'D2', with the following properties:
% (i)   A single Slider, with a range 0-10. [1 mark]
% (ii)  A single, Static Text Box, initially labelled '0' (zero). [1 mark]
% (iii) A single, Static Text Box, initially with no label. [1 mark]
% (iv)  A program (function) action, that when you move the slider: 
%       (a) the new value is displayed in the first Static Text Box
%           [4 marks]
%       (b) if the value is greater than 5,
%           the second Static Text Box displays 'WARNING: over-range!'
%           [3 marks]
% (x)   For 2 BONUS marks, ensure no text is displayed in the 2nd
%       Static Text Box for values <= 5. i.e. for values <=5,
%       set an empty string as the Static Text Box property
% [10+2 marks total]
%
% *************************************************************************
% *************************************************************************
%
%
%
% =========================================================================
% =========================================================================
% END OF EXAM --- READ ME LAST
% =========================================================================
% CHECK that you have entered your name (string) at the top of this file.
% CHECK that for the questions requiring an m-file, all changes are saved.
% CHECK that you have uploaded all answers to iLearn.
% !!!DOUBLE CHECK!!! everything before leaving the room ...
% =========================================================================
% =========================================================================
