% ---------------------------------------- 
% EPS 131: Intro to Phys. Ocean. & Climate
% Laure Zanna, Feb 7, 2008
% Intro to Matlab, Part I
%-----------------------------------------

% Download MATLAB from:
% http://downloads.fas.harvard.edu/download

% Tutorials:
% http://www.mathworks.com/academia/student_center/tutorials/launchpad.html

% Some definitions 
% 1. To start MATLAB: a) double-click on the MATLAB icon or b) from the Start
% menu. This will open the main window called the Desktop
% 2. Command prompt: >>
% 3. To clear the command window: clc
% 4. To quit matlab: type at the prompt exit or quit
% 5. Manual/Help: at the prompt  help [command name] ; 
% or visit : http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml

% -----------------------------------------------
%  Using matlab with basic arithmetic operations
% +, -, *, /, ^ 
% -----------------------------------------------

% At the prompt >>, type 
% a. 
>> 4+10

% The answer will appear ("ans" stands for answer)
ans 
   = 14

% b.
>> 5 *10 + 6

ans =

    56

% c.
>> (6 + 6) / 3

ans =

     4

% d.     
>> 9^2

ans =

    81
 
% before we continue, clear the memory
>> clear

% ---------------------------------------
% Defining Your Own Variables
% ---------------------------------------
% a. Define a = 10, b = 20, c = 30
>> a = 10 
a =

    10

>> b = 20

b =

    20

>> c = 30

c =

    30

% b. Take the average of the a,b,c => avg = (a + b + c) / 3

>> avg= (a + b + c) / 3
avg =

    20

% c. Divide multiply variables 

>> d=a/b 

d =

    0.5000


>> e=a*b

e =

   200

% d. Check the list of your variables in memory

>> whos

  Name      Size                   Bytes  Class

  a         1x1                        8  double array
  avg       1x1                        8  double array
  b         1x1                        8  double array
  c         1x1                        8  double array
  d         1x1                        8  double array
  e         1x1                        8  double array

Grand total is 6 elements using 48 bytes

% e. Clear variables from memory 
>> clear a b c % to clear only variable a b c 
>> whos

  Name      Size                   Bytes  Class

  avg       1x1                        8  double array
  d         1x1                        8  double array
  e         1x1                        8  double array

Grand total is 3 elements using 24 bytes

>> clear       % to clear all variables

% f. Suppressing Results with Semicolons 
>> a = 10;
>> b = 20;

% -------------------------
% Construct Vectors
% -------------------------
% 1. Row vector (comma)
>> vector_1=[1, 2, 3] 

vector_1 =

     1     2     3

% 2. Column vector (semi-colon)
>> vector_2=[1; 2; 3]  

vector_2 =

     1
     2
     3

% 3. Construct a matrix : 4 (rows) x 3 (columns)      
>> matrix = [8, 12, 19; 7, 3, 2; 8, 1, 1; 2, -1, 0]

matrix =

     8    12    19
     7     3     2
     8     1     1
     2    -1     0
      
% and its transpose of a matrix: 
>> matrix'

ans =

     8     7     8     2
    12     3     1    -1
    19     2     1     0
     
% 4. Check for the size of a vector (or matrix) 
% command size gives: number of rows and number of columns  
% comand lengh: give the max of row or columns
     
>> size(vector_1)

ans =

     1     3


>> size(vector_2)

ans =

     3     1

>> length(vector_2)

ans = 
    
     3
     
% 5. Get single elements of a matrix/vector
     
>> vector_1(2)

ans =

   2
    
>> matrix(3,2) 

ans = 
   
   1
   
% clear all the variables
>> clear 
 
% 6. Element-By-Element Operations:  multiplication: ".*"
% division: "./".,  addition: "+", subtraction: "-", exponentiation: ".^"
% Define 2 vectors a and b
>> a= [ 1 2 3]

a =

     1     2     3

>> b= [4 5 6]

b =

     4     5     6

>> a*b

??? Error using ==> *
Inner matrix dimensions must agree.

>> a.*b

ans =

     4    10    18

>> clear
