User Tools

Site Tools


gibson:teaching:fall-2013:math445:lecture2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% How to evaluate wind chill formula in Matlab

% Approach 1: Set variables T and V, then evaluate formula
T = 45
T =
    45
V = 10
V =
    10
WCF = 35.7 + 0.6*T - 35.7*V^0.16 + 0.43*T*V^0.16
WCF =
   39.0671

% Approach 2: evaluate formula with embedded values of T,V
WCF = 35.7 + 0.6*45- 35.7*10^0.16 + 0.43*45*10^0.16
WCF =
   39.0671

% Approach 1 is better because you can easily modify T,V, e.g. 
T=45; V=0; WCF = 35.7 + 0.6*T - 35.7*V^0.16 + 0.43*T*V^0.16
WCF =
   62.7000

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Random numbers

% The rand function produces a random real number between 0 and 1
rand 
ans =
    0.9649

% So 20*rand produces a random real number between 0 and 20
20*rand
ans =
    3.1523

20*rand
ans =
   19.4119

% and 20 + 30*rand produces and random real between 20 and 50
20+30*rand
ans =
   42.7322

20+30*rand
ans =
   31.7668

20+30*rand
ans =
   20.9550

% randi(N) produces a random integer between 1 and N
randi(4)
ans =
     3
randi(4) 
ans =
     1

% so 100 + randi(10) 
100 + randi(11) % gives rnadom numbers btwn 101 and 110
ans =
   107
100 + randi(11) % gives rnadom numbers btwn 101 and 111
ans =
   106

% round(10*rand) will produce random integers between 0 and 10,
% but it's not as efficient as using the randi() function
round(10*rand)
ans =
     8
round(10*rand)
ans =
     4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Boolean expressions, also known as logical expressions

% double equals signs means evaluate whether both sides are equal
% return true or false accordingly

3 == 2 + 1 % remember 1 == true ; 0 == false
ans =
     1

3 == 2 - 1 % remember 1 == true ; 0 == false
ans =
     0

% here (3 == 2) evaluates to 0, which is then added to 1
(3 == 2) + 1
ans =
     1

% variables b and c are undefined
b > c + 1
Undefined function or variable 'b'.

% but 'b' is the character b
'b'
ans =
b

'c'
ans =
c

class('b')
ans =
char

% cast char 'b' to an integer
uint32
uint32('b')
ans =
      98

% cast char 'c' to an integer
uint32('c')
ans =
      99

% to evaluate this expression, Matlab casts 'b' and 'c' to their 
% integer values and then evaluates the integer equation
'b' >= 'c' + 1
ans =
     0

'b' >= 'c' - 1
ans =
     1
gibson/teaching/fall-2013/math445/lecture2.txt · Last modified: 2013/09/04 18:47 by gibson