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:hw4solns [2014/10/23 07:53] gibson [Math 445 HW4 solutions] |
gibson:teaching:fall-2014:math445:hw4solns [2014/10/23 09:09] (current) gibson |
||
|---|---|---|---|
| Line 22: | Line 22: | ||
| % return dollar value of q quarters, d dimes, and n nickels | % return dollar value of q quarters, d dimes, and n nickels | ||
| - | if q != round(q) | d != round(d) | n != round(n) | + | if q != round(q) || d != round(d) || n != round(n) |
| fprintf('error: arguments q,d,n should all have integer values'); | fprintf('error: arguments q,d,n should all have integer values'); | ||
| dollars = 0.25*q + 0.10*d + 0.05*n; | dollars = 0.25*q + 0.10*d + 0.05*n; | ||
| Line 187: | Line 187: | ||
| <code matlab> | <code matlab> | ||
| - | |||
| - | </code> | ||
| - | </code> | ||
| function Tout = tempconvert2(Tin) | function Tout = tempconvert2(Tin) | ||
| % convert input temperature T from any of K,C,F units to any of same units. | % convert input temperature T from any of K,C,F units to any of same units. | ||
| - | + | ||
| - | uin = input('Enter the current units of temperature, K, C, or F: ','s'); | + | uin = input('Enter the current units of temperature, K, C, or F: ', 's'); |
| + | |||
| % convert input temperature T to Celsius | % convert input temperature T to Celsius | ||
| switch uin | switch uin | ||
| Line 207: | Line 204: | ||
| T = NaN; | T = NaN; | ||
| end | end | ||
| + | |||
| if T < -273.15 | if T < -273.15 | ||
| - | disp('Error: Temperature is less than absolute zero') | + | disp('Warning: temperature is less than absolute zero') |
| end | end | ||
| - | + | ||
| - | uout = input(Enter the unit you want to convert to, K, C, or F: ','s'); | + | uout = input('Enter the unit you want to convert to, K, C, or F: ', 's'); |
| + | |||
| % convert T from Celsiss to output units | % convert T from Celsiss to output units | ||
| switch uout | switch uout | ||
| Line 228: | Line 225: | ||
| fprintf('%d %s is equivalent to %d %s\n', Tin, uin, Tout, uout) | fprintf('%d %s is equivalent to %d %s\n', Tin, uin, Tout, uout) | ||
| end | end | ||
| + | </code> | ||
| + | A few non-comprehensive tests: | ||
| + | <code matlab> | ||
| + | >> T = tempconvert(0) | ||
| + | Enter the current units of temperature, K, C, or F: C | ||
| + | Enter the unit you want to convert to, K, C, or F: F | ||
| + | 0 C is equivalent to 32 F | ||
| + | T = | ||
| + | 32 | ||
| + | >> T = tempconvert(0) | ||
| + | Enter the current units of temperature, K, C, or F: K | ||
| + | Enter the unit you want to convert to, K, C, or F: C | ||
| + | 0 K is equivalent to -2.731500e+02 C | ||
| + | T = | ||
| + | -273.1500 | ||
| + | </code> | ||