====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:fall-2014:math445:lecture8-diary [2014/10/08 12:34] gibson [switch statement] |
gibson:teaching:fall-2014:math445:lecture8-diary [2014/10/09 17:01] (current) gibson [Example: if-elseif-else and multiple args and return val] |
||
---|---|---|---|
Line 17: | Line 17: | ||
The general form of an ''if-elseif-else'' statement is | The general form of an ''if-elseif-else'' statement is | ||
- | <code> | + | <code matlab> |
if condition1 | if condition1 | ||
action1 | action1 | ||
Line 33: | Line 33: | ||
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. | ||
+ | <code matlab> | ||
+ | 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 | ||
+ | if units == 'F' | ||
+ | tK = 5/9*t + 255.37; | ||
+ | elseif units == 'C' | ||
+ | tK = t + 273.15; | ||
+ | elseif units == 'K' | ||
+ | tK = t; | ||
+ | else | ||
+ | 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 | ||
+ | </code> | ||
====switch statement==== | ====switch statement==== | ||
- | The above problem is actually better done with a switch statement. Switch statements perform conditional execution based on the value of a variable. The general form is | + | The above problem is actually better done with a switch statement. Switch statements perform conditional execution based on the value of a variable or an expression. The general form is |
<code matlab> | <code matlab> | ||
switch expression | switch expression | ||
Line 47: | Line 71: | ||
otherwise | otherwise | ||
action4 | action4 | ||
+ | end | ||
+ | </code> | ||
+ | |||
+ | Here's a solution to the problem using a ''switch'' statement. | ||
+ | <code matlab> | ||
+ | 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 | ||
</code> | </code> |