% Lesson 1: Introduction to the MATLAB Environmnet
% Part 2: Built-in MATLAB Functions,
% Author: Your name here
% Last Updated: Monday 1/14/2019
%
% In-class announcement:
%     - Jeff updated the ENGR 11 Tentative Calendar (see course homepage)
%     -Friday Februar 8, 2019 : Jeff will not be in lab that morning
%
% In this lesson, we will start by exploring built-in functions calls. When coding in any lanuage, syntax matters greatly. We can think of syntax as the grammer, spelling, format, organization, and structure of a programming lanuage. All of these features matter a lot. An example of this is that MATLAB is case-sensitive (upper and lower cases are not the same thing in MATLAB). With this in mind, let's consider the general syntax for a built-in function call:
%  y = function_name( x1, x2, .... )
%
% EXAMPLE 1: HOW TO USE BUILT-IN ARITHEMTIC FUNCTIONS
% For our first example, let's use the famous Ideal Gas Law formulat given by
%     P * V = n * R * T
% Notice this is all multiplication. Recall:

%Example 1: Inspired by Ann :)
format short

%Volume of container that holds gas 
V = 1.00 %Liters cubed

%Number of moles
n = 1 %mole

%Ideal-Gas Constant 
R = 8.314 %Units: Joules/ ( K * Mole )

%Temperature of gas
T = 273.15 %Kelvin

% Using Ideal gas law, let's calculate the pressure.
% This calculation uses purely arithmetic operator notation that
% we would expect from math.
P1 = (n * R * T) / ( V )

%Let's recalculate pressure using built-it mathematics functions
P2 = rdivide(times(times(n, R),T), V)

% Let's use the absolute error to test the two different outputs.
% If the variable test below is zero, we know both formulas produce
% identical outputs. We will use this trick (combined with relative
% error calculations to test for accuracy of our algorithms)
test = abs(P2 - P1)

%
% STUDY HOW TO USE FUNCTION INPUTS AND OUTPUTS
% Note that we can use function outputs really in various contexts.

% Output of the inner function becomes input of outer function
z = log(exp(1)) 

% Function outputs can also be used in arithmetic operations
easy_root = 8/sqrt(4)

%We can do arithmetic within function arguments
fun_root = sqrt(((64 + 36)*16)/(9))

%
% DEFINE SCALAR VARIABLES
% Let's note that there is a major difference between math words and programming words. As we will see, overloaded words are very common in programming.  Our first example of such a scenario will be the word variable.
%
% -In math, the word variable usually means a symbol for an unknown (or known) value in a mathematical statement (expression or equation).
%
% -In MATLAB programming, the word variable represents a tool that has three special attributes, including:
% a specific storage location: something about an address and data
% a user-defined symbolic name: a set of characters that are used to represent the variable within our programming environment. In MATLAB, there are six rules that we will discuss for making these names.
% an assigned value: a value that has been specially encoded, using some binary encoding scheme, that can be stored and manipulated within our environment.
% When we declare a variable, MATLAB does some interesting work internally.
% The first thing MATLAB does is to allocate memory in our computer's "long term memory." By default, MATLAB's scalar-valued variables are stored as double precision floating point format (from the IEEE 754 standard... Don't stress: we will study this in Lessons 6 & 7). The short side of this story is that for each scalar stored memory, MATLAB's default is to use a 64-bit encoding (64 different voltage values of either 0 or 1).
%
% The second thing that happens is that MATLAB encodes the value of the variable in a useful binary data scheme. In this class, we will focus on many such encoding including:
% binary representation of unsigned (nonnegative) integers
% twos complement representation of signed integers
% IEEE 754 floating-point Representation of floating-point numbers (quarter, half, single, and double precision)
% ASCII: American standard code for information interchange (8-bits: 256 symbol)
% Unicode: ASCII plus a lot of extra features (64-bit: 2^(64) different symbols)
% The third thing that MATLAB does is to associate the address of our memory slot with the symbolic name that we've chosen for that variable. In other words, scalar-valued variables are data structures. Let's look at a specific example:

variable_name = 3

% Let's look at the general syntax for declaring a scalar-valued variable
% variable_name = (computable expression or numerical scalar  value)
% For giggles (heeheehee), let's do some non-numerical variables

elliot_giggles = 'a'
elliot_laughs = 'this is a much longer string of characters'
integer = uint8(4)

%
% RULES ABOUT VARIABLE NAMES;
% We have six rules for defining variable names in MATLAB. When we define variables in MATLAB, we need to follow the rules outlined below. MATLAB variables:
% Must begin with a letter (upper or lower case)
% Can be up to 63 characters long
% Can contain uppercase letters, lowercase letters, decimal digits (0, 1, 2, ..., 9) and the underscore character
% Cannot contain punctuation characters like periods, commas, semicolon, exclamation marks, questions marks, ampersand, etc.
% Cannot contain spaces between characters (suggestion: use underscore instead of space)
% Cannot be defined as MATLAB's keywords that are reserved for special purposes in MATLAB (note: to check the list of 20 keywords in MATLAB, we can use the iskeyword command seen below)

iskeyword

% In addition to these six rules, we have some general guidelines for defining variables that are useful to remember.
%
% GUIDELINES FOR VARIABLE NAMES
% As we will see, defining variable names strategically is an art! Here are some guidelines I suggest you use:
% A. Avoid using the names of built-in function (i.e. don't use 'sin' as a variable name). Note that in MATLAB variable names take precedence over function names. In other words, if there is any confusion, MATLAB assumes variables rather than function. This can be a big problem if you want to use both. To check whether a desired variable name exists in MATLAB's environment using the following command:

exist sin

% B. Avoid using names of predefined variable in MATLAB. In particular, MATLAB maintains a list of a bunch of pre-defined variables including all of the following:

pi      %This stores a IEEE 754 double precision floating point number to approximate the famous 
        %mathematical constant pi

i, j    %Store the sqrt(-1)

inf     %Stands for infinity: something about IEEE 754 format (see lessons 6 & 7)

NaN      %Not a Number: something about IEEE 754 format (see lessons 6 & 7)

clock    %1 x 6 vector: Time stamp for the exact time [ year, month, day, minute, second ]

date     %String representing today's date

eps     %Machine epsilon: this the distance between 1 and the next largest double-precision number

ans     %answer: the default variable used to store output that has been unassigned

% C. MATLAB is case sensitive: uppercase and lower case variables are not the same. The following two variables are different:

a = 4;
A = 2;

% USEFUL COMMAND FOR MANAGING VARIABLES
% We may find it very helpful to be able to manage and view our Workspace from the Command Window. To do so, we can use at least four basic commands, including:

clear     %Deletes all variables stored in the entire Workspace

%Precision deletion of variables
a = 4; A = 2; b = 'whos tired'; c = 'your teacher';
clear a c

who     %Displays a (short) list of variables currenly stored in the workspace

whos    %Displays a (longer and more detailed) list of variables currenly stored in the workspace along
        %with information about each variable