%% MATLAB Live Scripts and Live Editor
%% Introduction
% The document you are currently reading is a MATLAB Live Script. Live scripts are interactive documents that combine MATLAB code with formatted text, equations, and images in a single interactive environment called the MATLAB Live Editor. In this context you can see your results together with the code that produced them.
% In the following sections you will learn
% How to run, edit and create code with the Live Editor,
% How to use common operators for basic calculations with numbers,
% How to define variables to store numbers and perform calculations.
%
%% 1. Getting started: Running code within sections
% Each Live Script is divided in sections that may contain code. Each section can be run individually.
% To run the code from each section, click into the code section and then click on the Run Section button (from the toolstrip) or click on the blue stripe on left side of that section as shown below:
%
% Remark: Run the code of each section from top to bottom, otherwise you may get an error.
% - The end of a section is indicated with a thin line, like the next one -
%
%% 1.1 MATLAB code and Output display options
% In the Live Script the MATLAB code is inside a grey square as the example below which shows the use of the operators +, -, /, * , ^, () for basic calculations with numbers:

5.5 + 3           % Addition
2 - 1.3           % Subtraction        
8 / 2             % Division
2 * 5             % Multiplication 
2^3               % Exponentiation
(3 * 2 - 2) * 2   % Brackets

% Note: The text after the percent " % " symbol indicates a comment line in the code.
% When you run this section, you will notice that the output appears together with the code inside the live editor. Try it, run this section!
% If you click on an output value, the cursor shows you which line of code produced that output as shown image below:
%
% You can also display your output inline:
%
% To change the output displays, use the icons on the right-side window of the Live Editor:
%
%
%% 1.2 Errors
% Errors in the code will appear next to the line where they occur:
%
% The following code has three errors. Run this section to find them. Then fix the errors in the code and re-run it.

a = pi;              % Constant pi
b = sqrt(2);         % Square root of 2
c = sin(0);          % Trig function sin evaluated at 0
op = cos(a / 2) + b + c

% Remark: Every time you modify your code a hatch blue bar appears on the left side. This tells you that you have modified the code. To run the latest version just click on this bar.
%
%
%% 1.3 Clearing outputs
% Finally, if you want to clear all the Outputs, right click in any section to open the help menu. Then select the option
%
%
%% 2. Hands on Practice
% Let's practice what we just learned.
%% 2.1 Operations with numbers
% In section 1.1 we introduced the operators +, -, /, *, ^, () for simple calculations with numbers. In section 1.2 we used the built-in constant pi and the functions sqrt(), sin() and cos().
%
%% Activity 1:
% Use these operators and functions to calculate the following:
% {1\ldotp 2}^5 -14\cdot 7
% \frac{7}{3}\cdot \left(3^7 +10\ldotp 62\right)
% \frac{\sqrt{1\ldotp 25-1/2}}{3\ldotp 46-8^4 }
% \frac{\cos \left(2\pi \right)}{\sqrt{2}}-\sin \left(\frac{\pi \;}{4}+3\ldotp 5\right)
% Write your code here and run it:





%
%% 2.2 Variables to store numbers and calculations
% We can define variables to store numbers and operate with them. Variable names consist of a letter followed by any number of letters, digits or underscore. For example:

x = 3;
y_0 = 2;
y_1 = 5;
initial_value = 0;
z0 = x + y_0 * y_1 + initial_value / 2

% Remark: Notice that at the beginning of the previous code, the semicolon ";" was added at the end. In general, a semicolon indicates end of statement. However, if you want to suppress and hide the MATLAB output for an expression, you must add a semicolon after the expression. Modify the code to suppress on the output the variables y_0, y_1 and initial_value. Then re-run the code.
%
%% Activity 2: Terminal velocity of falling objects
% A model for the velocity of a falling object after time t is given by
% v(t)=\sqrt{\frac{m\cdot g}{k}}\,\tanh\left(\sqrt{\frac{g \cdot k}{m}}\, t\right)
% where m is the mass of the object in kg, g=9.8\,\text{m}/\text{s}^2 is the acceleration due to gravity, k is a constant, t is measured in seconds, and v in \text{m}/\text{s}.
% If a person falls from a building, the value of the constant k depends on his or her position. For a "belly-to-earth" position, k=0.515\text{kg}/\text{s}, but for a "feet-first" position, k=0.067\text{kg}/\text{s}.
% Problem 1: What is the velocity of a 60-kg person falling in belly-to-earth position after 10 seconds and after 30 seconds? What if he/she falls in feet-first position?
% Write your code here:

% define variabes


% belly-to-earth velocity


% feet-first velocity


% Hint: You can use other built-in function tanh().
%
% The terminal velocity of a falling object is determined by the limit
% \lim_{t\rightarrow \infty} \sqrt{\frac{m\cdot g}{k}}\,\tanh\left(\sqrt{\frac{g \cdot k}{m}}\, t\right)
% Problem 2: Calculate this limit on your notebook and then use MATLAB to compute the terminal velocity of a 60-kg person falling in belly-to-earth and feet-first position.
% Write your code here:

% belly-to-earth terminal velocity


% feet-first terminal velocity


% Hint: Recall that \lim_{x \rightarrow \infty} \tanh (x) = 1.
% Analyse your results and compare them with the results from Problem 1. What do you notice? What do you wonder?
%
%% References
% Long, L, N. & Weiss, H. (2006). How terminal is terminal velocity? The American Mathematical Monthly. (113): 752-755.