Lecture 20 precision input and output switch Number

Lecture 20 precision, input and output, switch

Number Formats: Floating Point • Scientific notation in computer languages – 2. 9979 x 108 meters per second is speed of light – To write it in Matlab (or most any other language) • 2. 9979 e 8 (no spaces allowed) • Use ‘e’ for exponent in base 10 • It’s not the irrational number e – Negative exponents are fine • 43. 67 X 10 -26 • 43. 67 e-26 (c) 2007 -08 Brian Funt, Simon Fraser University 2

Numeric Display Format • • format short 4 decimal digits format long 14 decimal digits format short e 4 decimal digits format long e 14 decimal digits 3. 14159265358979 e+000 • format bank 2 decimal digits 3. 1416 3. 14159265358979 3. 1416 e+000 3. 14

Numeric Display Format • • format short 4 decimal digits format long 14 decimal digits format short e 4 decimal digits format long e 14 decimal digits 3. 14159265358979 e+000 • format bank 2 decimal digits 3. 1416 3. 14159265358979 3. 1416 e+000 3. 14 >> A = 5 returns A = 5 >> A = 5. 1 returns A = 5. 1000 >> format long >> A = 5. 1 returns A = 5. 10000000 >> format short >> A = 5. 1 returns A = 5. 1000

logspace %Generate vector of values logarithmically spaced over range 101 through 104 b=logspace(1, 4 , 4) >> b b= 10 10000 c=logspace(1, 4, 3) %Now generate only 3 values c= 1. 0 e+004 * (note here the constant multiplier) 0. 0010 0. 0316 1. 0000 log 10(c) ans = (i. e, this is 10, 316, 1000) %This is log base 10. Natural log would be log(c) 1. 0000 2. 5000 4. 0000 (i. e. , they’re evenly spaced in log space) (c) 2007 -08 Brian Funt, Simon Fraser University 5

Formatted Printing using fprintf • Want complete control over how everything is printed – Should numbers be printed as integers or reals – Controlling the number of significant digits – Controlling spacing between items – Controlling line spacing • fprintf(formattingstring, value 1, value 2, …) • fprintf is common to Fortran, C, Java, etc. (c) 2007 Brian Funt, Simon Fraser University 6

fprintf formattingstring • Use % sign to indicate a formatting item – %f means print fixed point • With a decimal point, but no exponent students = 25 >> fprintf('There are %f students in CMPT 102', students); There are 25. 000000 students in CMPT 102>> (c) 2007 Brian Funt, Simon Fraser University 7

%f %e and %g • %f for fixed point without exponent • %e decimal with exponent • %g prints whatever is shortest, including without a decimal point >> fprintf('There are %e students in CMPT 102', students); There are 5. 200000 e+001 students in CMPT 102>> >> fprintf('There are %g students in CMPT 102', students); There are 52 students in CMPT 102>> (c) 2007 Brian Funt, Simon Fraser University 8

Printing on the next line • fprintf doesn’t move to the next line unless specifically told to do so – To move to the next line use n • Like %, is a special formatting character The command prompt is on the same line >> fprintf('There are %e students in CMPT 102', students); There are 5. 200000 e+001 students in CMPT 102>> >> fprintf('There are %e students in CMPT 102 n', students); There are 5. 200000 e+001 students in CMPT 102 (c) 2007 Brian Funt, Simon Fraser University 9

Backslash “” Uses • Note that “” is not the same “/” • n linefeed – linefeed means go to next line • r “carriage return” from old typewriters – same as linefeed • t tab • b backspace – likely don’t need with modern character font sets (c) 2007 Brian Funt, Simon Fraser University 10

Width and Precision Controls • %f format is generalized to – %w. pf • Forces the number to be w spaces wide – Fixed width is useful for tables • Forces there to be p places after the decimal • The “f” is required, but could be “e” or “g” as well >> fprintf('There are %15. 2 f students in CMPT 102 n', students); There are 31. 00 students in CMPT 102 >> >> fprintf('There are %15. 2 e students in CMPT 102 n', students); There are 3. 10 e+001 students in CMPT 102 (c) 2007 Brian Funt, Simon Fraser University 11

Formatted Printing of Matrices • Numbers printed in column-major order m=[1 2 3; 4 5 6] m= 1 2 3 4 5 6 fprintf('%5. 1 f', m); 1. 0 4. 0 2. 0 5. 0 3. 0 6. 0>> m(: ) ans = 1 4 2 5 3 6 >> fprintf('%5. 1 f', m(: )); 1. 0 4. 0 2. 0 5. 0 3. 0 6. 0>> (c) 2007 Brian Funt, Simon Fraser University 12

Change Order with Transpose >> fprintf('%5. 1 f n', m); 1. 0 4. 0 2. 0 5. 0 3. 0 6. 0 >> >> fprintf('%5. 1 f n', m'); 1. 0 2. 0 3. 0 4. 0 5. 0 6. 0 >> (c) 2007 Brian Funt, Simon Fraser University 13

To Print as 2 x 3 table >> m = 1 2 4 5 3 6 >> fprintf('%5. 1 f n', m'); 1. 0 2. 0 3. 0 4. 0 5. 0 6. 0 >> >> fprintf('%8. 1 f %8. 1 e %8. 1 g n', m'); 1. 0 2. 0 e+000 3 4. 0 5. 0 e+000 6 >> Varied formatting (c) 2007 Brian Funt, Simon Fraser University 14

Evaluating the weather • Hot – Over 30 • Warm – Over 20 to 30 • Cool – Over 5 to 20 • Cold – 5 and under (c) 2007 Brian Funt, Simon Fraser University 15

% weather(temperature) evaluates the weather in terms of it % being hot, warm, cool, or cold. function e = weather(t) if (t>30) e = 'hot'; elseif (t>20) e ='warm'; %Note it’s not t<=30 & t>20 elseif (t>5) e='cool'; else e='cold'; end; >> weather(20) ans = cool >> weather(20. 1) ans = warm >> weather(30. 1) ans = hot (c) 2007 Brian Funt, Simon Fraser University 16

Switch-Case switch variable case option 1 statement 2 …. case option 2 statement 1 statement 2 …. otherwise statement 1 …. end Consider estimating temperature as a function of words like hot, warm, cool, cold. %Guess temperature given a description of the weather. function [] = temperature(weather) switch weather case 'hot' disp('I guess it''s about 33 degrees. ') case 'warm' disp('I guess it''s about 24 degrees. ') case 'cool' disp('I guess it''s about 16 degrees. ') case 'cold' disp('I guess it''s about 5 degrees. ') otherwise disp('Could you explain further please? ') end (c) 2007 Brian Funt, Simon Fraser University 17

menu with switch • in=menu(‘message’, ‘button 1’, ‘button 2’, …) – Very simple way to create menu-based user interaction – The message is displayed – Buttons are displayed with corresponding labels • menu returns the number of the button clicked on – Convenient to use switch to deal with the button cases (c) 2007 Brian Funt, Simon Fraser University 18

menu-based temperature guessing %Print a guess as to the temperature given a description of the weather. function [] = temperature_menu() weather=menu('How would you describe the weather? ', 'hot', 'warm', 'cool', 'cold'); switch weather case 1 %Selection is by button number, not button label. disp('I guess it''s about 33 degrees. ') case 2 disp('I guess it''s about 24 degrees. ') case 3 disp('I guess it''s about 16 degrees. ') case 4 disp('I guess it''s about 5 degrees. ') otherwise disp('Error in button reading') end (c) 2007 Brian Funt, Simon Fraser University 19

To Try Using a menu-based approach, write a function cost_dinner that asks the user to rate dinner at some restaurant as either expensive, moderate, inexpensive, or very cheap. Output a guess as to the cost of the dinner in dollars. (c) 2007 -08 Brian Funt, Simon Fraser University 20

Computing Factorial with a for loop factorial of n is n! = 1*2*3* …*n % Computing factorial with a for loop function fact = factorial 1(n) if n<1 %Error checking section. disp('Factorial 1 requires argument >= 1'); fact=[]; else fact = 1; %Have to initialize before entering the loop for i = 1: n fact = fact * i; end (c) 2007 Brian Funt, Simon Fraser University 21

Factorial with Implicit Looping % Computing factorial without explicit looping function fact = factorial 2(n) if n<1 %Error checking section. disp('Factorial requires argument >= 1'); fact=[]; else fact = prod(1: n); end >> tic; factorial(10000); toc Elapsed time is 0. 016227 seconds. >> tic; factorial 2(10000); toc Elapsed time is 0. 003786 seconds. >> (c) 2007 Brian Funt, Simon Fraser University 22

To Try (1) Using explicit looping, write a function sumn(n) that returns the sum of the numbers 1 to n. [Note that looping isn’t a very intelligent way to compute the sum since we have the formula 1+ 2+. . . + n = n(n+1) / 2, but this is for practice with loops. ] (2) Using explicit looping, write a function cummulative(V) that returns a vector of cummulative sums of the elements of V. Do not use Matlab’s cumsum function. >> cummulative([3 5 6 2]) ans = 3 8 14 16 (c) 2007 -08 Brian Funt, Simon Fraser University 23

Factorial using Factorial % Computing factorial recursively function fact = factorial 3(n) if n<1 %Error checking section. disp('Factorial requires argument >= 1'); fact=[]; else if n == 1 fact=1; %Base case else fact = n * factorial 3(n-1); %Recursive call to factorial 3 end factorial 3(1) %Testing base case ans = 1 >> factorial 3(5) ans = 120 (c) 2007 Brian Funt, Simon Fraser University 24
- Slides: 24