====== 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:hw4solns [2014/10/23 07:37] gibson |
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> | ||
+ | function Tout = tempconvert2(Tin) | ||
+ | % 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'); | ||
+ | |||
+ | % convert input temperature T to Celsius | ||
+ | switch uin | ||
+ | case 'C' | ||
+ | T = Tin; | ||
+ | case 'F' | ||
+ | T = (Tin-32)*(5/9); | ||
+ | case 'K' | ||
+ | T = Tin-273.15; | ||
+ | otherwise | ||
+ | disp('Input unit is not valid. Returning NaN.') | ||
+ | T = NaN; | ||
+ | end | ||
+ | |||
+ | if T < -273.15 | ||
+ | disp('Warning: temperature is less than absolute zero') | ||
+ | end | ||
+ | |||
+ | uout = input('Enter the unit you want to convert to, K, C, or F: ', 's'); | ||
+ | |||
+ | % convert T from Celsiss to output units | ||
+ | switch uout | ||
+ | case 'C' | ||
+ | Tout = T; | ||
+ | case 'F' | ||
+ | Tout = (9/5)*T+32; | ||
+ | case 'K' | ||
+ | Tout = T + 273; | ||
+ | otherwise | ||
+ | disp('Output unit is not valid. Returning NaN.') | ||
+ | Tout = NaN; | ||
+ | end | ||
+ | fprintf('%d %s is equivalent to %d %s\n', Tin, uin, Tout, uout) | ||
+ | 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> | </code> | ||
- |