User Tools

Site Tools


gibson:teaching:fall-2014:math445:lecture1-diary
% =========================================================
% Basic matlab usage: 
% type in expression, matlab computes, and returns answer

7 + 8 * 6

ans =
    55

(7 + 8) * 6

ans =
    90

% arithmetic operations are
% + - * / ^ 

% here's how to compute 3 squared, i.e. 3 to the 2

3^2 

ans =
     9

% =========================================================
% Variables 

% assign a number into a variable

x = 5 % assign the value 5 into the variable x

x =
     5

x

x =
     5

2+x

ans =
     7

% Assign 9 + 2*4 (that is, 17) into the variable x
x = 9 + 2*4 

x =
    17

% Note: this is not an equation! It's an assignment of a value into a variable.
x = 9 + 2*4 

x =
    17


% Note: Matlab can be 'cute'

% "who" tells what variables matlab knows at any time
who 

Your variables are:

ans  x    

% "whos" is a more detailed version of "who", tells type (class) as well
whos
  Name      Size            Bytes  Class     Attributes

  ans       1x1                 8  double              
  x         1x1                 8  double              


% convert x to an integer and store in n. "uint32" is unsigned 32-bit integer
n = uint32(x); 

whos
  Name      Size            Bytes  Class     Attributes

  ans       1x1                 8  double              
  n         1x1                 4  uint32              
  x         1x1                 8  double              

% The default type (class) of any variable is double (double precision 
% floating point number)


y = exp(23) % compute e^23

y =
   9.7448e+09

% this is standard text notation for 9.7448 x 10^9

y = exp(-23) % compute e^23

y =
   1.0262e-10

% this is standard text notation for 1.0262 x 10^-10

whos
  Name      Size            Bytes  Class     Attributes

  ans       1x1                 8  double              
  n         1x1                 4  uint32              
  x         1x1                 8  double              
  y         1x1                 8  double              

% class(variable) tells you the class (type) of a variable
class(x)

ans =
double

% ===============================================================
% logical expressions

% 3 > 1 is a logical expression, just like 3 + 1 is an arithmetic expression

3 > 1 

ans =
     1

% in matlab, true is 1 and false is 0

% this is a logical expression, just like 3 + 1 is an arithmetic expression
3 < 1 

ans =
     0

% evaluate 3 > 2 to true (1) and assign to variable b
b = 3 > 2 

b =
     1

class(b)

ans =
logical

whos
  Name      Size            Bytes  Class      Attributes

  ans       1x7                14  char                 
  b         1x1                 1  logical              
  n         1x1                 4  uint32               
  x         1x1                 8  double               
  y         1x1                 8  double               

% illustrate && (and) and || (or)


3 > 2 && 4 > 1        % this means 3 > 2 AND 4> 1

ans =
     1

3 > 2 && 4 < 1        % this means 3 > 2 AND 4 < 1

ans =
     0

% Implicit type conversion, will convert logical true (1) to 1 to add to 18

(3 > 2 && 4 > 1) + 18 

ans =
    19


3 > 2 || 4 > 1        % this means 3 > 2 OR  4 > 1

ans =

     1

3 > 2 || 4 < 1        % this means 3 > 2 OR  4 < 1

ans =

     1

3 < 2 || 4 < 1        % this means 3  < 2 OR  4 < 1

ans =
     0



% ~ means NOT, logical inversion

~1       % not true, ie false

ans =
     0

~0       % not false, ie true

ans =
     1

3 > 2 

ans =
     1

~(3 > 2)

ans =
     0

~(3 < 2 || 4 < 1)        % this means NOT (3  < 2 OR  4 < 1)

ans =
     1
gibson/teaching/fall-2014/math445/lecture1-diary.txt · Last modified: 2014/09/02 07:03 by gibson