%% Lecture 1 - Welcome to 310
%% Matlab environment
% [Take a tour - still confused? Extra training here: https://matlabacademy.mathworks.com/ ]
% Windows, ribbon bar
%% Matlab help
% Shift+F1
% Mathworks File Exchange (advanced)
% Your teammates
%% Making an 'M-file'
%   m-file is the file matlab saves (*.m extension), they come in two broad flavors:
%% Script vs. function (main and sub)

% This is a script.  I require no input.  I do what I am told and print to
% the screen.
A = 12;
B = 5;
disp('A + B =')
c = A+B

% A function is in the form:
% function [outputArg1,outputArg2] = untitled6(inputArg1,inputArg2)
% %UNTITLED6 Summary of this function goes here
% %   Detailed explanation goes here
% outputArg1 = inputArg1;
% outputArg2 = inputArg2;
% end
% Function programming must be shown out of the 'live editor' due to requirement of ALL functions being at the end of script files.
% MATLAB has thousands of built in functions! Use help menus to see how to use a function (there are many input and output options.
%% Format
% ALWAYS insert a header: 1) Name of program, 2) Who wrote it and when, 3) Details on the program.
%% Saving and Running
%
%% Matlab basics
% Use of the mighty semicolon ;
% Variables must be named with letters first
% Always put new variable first in a line of code, only way matlab can read.
% Types of variables

S = 12; %scalar
Vr = [1 2 3 4 5]; %row vector
Vc = [1; 2; 3; 4]; %column vector
% Transpose row vector to column vector
Vc2 = Vr';
M = [1 2 3; 4 5 6; 7 8 9];
string = 'Hello World!'

% Indentation can be messy (use them to help you)
%% Mathematical Operations

% Adding, subtraction, multiplication, and division
a = 4; b = 5; c = 12;
d1 = a+b;
d2 = a-b;
d3 = a*b;
d4 = a/b; %NOTE, this is not '\' becuse we use this for matrix math later
%
% Parantheses
d5 = (a+b)-c;
% Powers
d6 = 2^a;
% Natural log
d7 = log(100);
% Log base 10;
d8 = log10(100);
% Scientific notation
d9 = 10e2;
% Exponential
d10 = exp(2);

% There are many built in variables (avoid writing over these as well as using function names as variable names.

pi

% A note on imaginary number 'i' - some purists don't like assigning this as a variable name.  You will see that I OFTEN use this as the index count for loops.  If I need to use the imaginiary number, I define it as '1i'.  As ChemEs this is rare.
% Note, everything shown above is for scalars.  You can also do opperations on vectors and matrices, however you need to know about 'element-wise notation.'

% Try this
a1 = Vr/Vr
a2 = Vr./Vr
% If you want each element of the matrix or vector to be multiplied or
% divided by each elemet of the corresponding matrix or vector you need to
% put a '.' before the / or *

%% Input, Disp, fprintf
% In your script you might want to get input from your user, or you might want to display text.  Here's how to do it:

% InputScript
% Coded by Nigel F. Reuel on Aug 22, 2017
% Demonstrates first script and how to run, save, etc.
A = input('Tell me what A is?')
B = input('Give me B?')
C = input('C?!!')
disp('Your answer is:')
D = A/B/C

% You can format your displayed text using 'fprintf' - pg.
%% Clearing screen
% Often you will see me start a program with these commands, they clear all variables and close all figures respectively

clc
close all

%% Loading data
% Typing in all your data is a drag. You'll get used to many functions to import data, here are a few favorites.  Note: your program will look in the folder in which the program is running UNLESS you give it extra file directory information in the file name.' (so in our case they will throw an error).

D1 = csvread('Mydata.csv');
D2 = xlsread('MyData.xlsx');
D3 = dlmread('MyData.txt');
I1 = imread('test.png');

%% Debug methods in editor
% <-- See the numbers to the left?  These count the lines of code in your program.  You can get your program to pause at any line, by adding a 'break.'  Show this on your program.
%%