====== Differences ====== This shows you the differences between two versions of the page.
gibson:teaching:fall-2013:math445:lecture2 [2013/09/04 18:47] gibson created |
gibson:teaching:fall-2013:math445:lecture2 [2013/09/04 18:47] (current) gibson |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | <code> | + | <code>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
- | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | + | % How to evaluate wind chill formula in Matlab |
- | % Linear algebra | + | |
- | % Assign matrix A | + | % Approach 1: Set variables T and V, then evaluate formula |
- | A = [ 1 3 ; 5 -2] | + | T = 45 |
- | A = | + | T = |
- | 1 3 | + | 45 |
- | 5 -2 | + | V = 10 |
- | % Assign vector b | + | V = |
- | b = [3 ; 32] | + | 10 |
- | b = | + | 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 | 3 | ||
- | 32 | + | randi(4) |
+ | ans = | ||
+ | 1 | ||
- | % Solve Ax=b for x | + | % so 100 + randi(10) |
- | x = A\b; | + | 100 + randi(11) % gives rnadom numbers btwn 101 and 110 |
- | x = A\b | + | ans = |
- | x = | + | 107 |
- | 6.0000 | + | 100 + randi(11) % gives rnadom numbers btwn 101 and 111 |
- | -1.0000 | + | 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 | ||
- | % Verify that Ax equals b | + | % to evaluate this expression, Matlab casts 'b' and 'c' to their |
- | A*x | + | % integer values and then evaluates the integer equation |
+ | 'b' >= 'c' + 1 | ||
ans = | ans = | ||
- | 3.0000 | + | 0 |
- | 32.0000 | + | |
- | A*x-b | + | 'b' >= 'c' - 1 |
ans = | ans = | ||
- | -8.8818e-16 | + | 1 |
- | 0.0000e+00 | + | |
- | % pretty darn close | ||
</code> | </code> |