# channelflow.org

### Site Tools

gibson:teaching:fall-2014:math445:lecture8-diary

# Differences

This shows you the differences between two versions of the page.

 gibson:teaching:fall-2014:math445:lecture8-diary [2014/10/09 16:59]gibson [Example: if-elseif-else and multiple args and return val] gibson:teaching:fall-2014:math445:lecture8-diary [2014/10/09 17:01] (current)gibson [Example: if-elseif-else and multiple args and return val] Both sides previous revision Previous revision 2014/10/09 17:01 gibson [Example: if-elseif-else and multiple args and return val] 2014/10/09 16:59 gibson [Example: if-elseif-else and multiple args and return val] 2014/10/08 12:50 gibson [if-elseif-else statement] 2014/10/08 12:35 gibson [switch statement] 2014/10/08 12:34 gibson [switch statement] 2014/10/08 12:34 gibson [switch statement] 2014/10/08 12:34 gibson [switch statement] 2014/10/08 12:33 gibson 2014/10/08 12:29 gibson 2014/10/08 12:12 gibson 2014/10/08 11:43 gibson 2014/10/08 11:43 gibson created 2014/10/09 17:01 gibson [Example: if-elseif-else and multiple args and return val] 2014/10/09 16:59 gibson [Example: if-elseif-else and multiple args and return val] 2014/10/08 12:50 gibson [if-elseif-else statement] 2014/10/08 12:35 gibson [switch statement] 2014/10/08 12:34 gibson [switch statement] 2014/10/08 12:34 gibson [switch statement] 2014/10/08 12:34 gibson [switch statement] 2014/10/08 12:33 gibson 2014/10/08 12:29 gibson 2014/10/08 12:12 gibson 2014/10/08 11:43 gibson 2014/10/08 11:43 gibson created Line 34: Line 34: Write a Matlab function ''​temp2kcf''​ converts a temperature ''​t''​ in any one of the three units Kelvin, Celsius, or Farenheit, and returns the temperature in all three units. The function should have two arguments, the temperature ''​t''​ and a character ''​units''​ which specifies the units as either K, C, or F and three return values, ''​tK,​ tC, tF''​. ​ Write a Matlab function ''​temp2kcf''​ converts a temperature ''​t''​ in any one of the three units Kelvin, Celsius, or Farenheit, and returns the temperature in all three units. The function should have two arguments, the temperature ''​t''​ and a character ''​units''​ which specifies the units as either K, C, or F and three return values, ''​tK,​ tC, tF''​. ​ + Here's a decent solution to the problem using an ''​if-elseif-else''​ statement. function [tK, tC, tF] = temp2kcf(t, units); function [tK, tC, tF] = temp2kcf(t, units); Line 55: Line 56: ​ ​ end end + ​ ====switch statement==== ====switch statement==== Line 69: Line 71: otherwise otherwise action4 action4 + end + ​ + + Here's a solution to the problem using a ''​switch''​ statement. + + function [tK, tC, tF] = temp2kcf(t, units); + % convert temperature t in units '​C',​ '​F',​ or '​K'​ to all three of those units + + % convert input temp to Kelvin + switch units + case '​F'​ + tK = 5/9*(t-32) + 273.15; ​     ​ + case '​C'​ + tK = t + 273.15; + case '​K'​ + tK = t;  ​ + otherwise + fprintf('​error:​ unknown units %c, returning absolute zero\n',​ units); + tK = 0; + end + ​ + % convert Kelvin to output temps + tC = tK - 273.15; + tF = 9/5*tC + 32; + ​ end end