User Tools

Site Tools


gibson:teaching:fall-2014:math445:hw4solns

Math 445 HW4 solutions

Problem 1: Write a function coincount that computes the dollar value of a number of quarters, dimes, and nickels. Use it to compute the dollar value of 18 quarters, 5 dimes, and 12 nickels.

function dollars = coincount(q, d, n)
% return dollar value of q quarters, d dimes, and n nickels
  dollars = 0.25*q + 0.10*d + 0.05*n;
end
>> coincount(18,5,12)
ans =
    5.6000

Note: It would be good programming practice to check that arguments q, d, n all have integer values and emit and warning if not.

function dollars = coincount(q, d, n)
% return dollar value of q quarters, d dimes, and n nickels
 
  if q != round(q) || d != round(d) || n != round(n)
    fprintf('error: arguments q,d,n should all have integer values');
  dollars = 0.25*q + 0.10*d + 0.05*n;
end

Problem 2: Write a function hms2decimal that converts a time in hours, minutes, and seconds and converts it to hours in decimal. Use it to calculate the decimal equivalent of 6 hours, 27 minutes, and 18 seconds.

function d = hms2decimal(h,m,s)
% compute decimal time from hours h, minutes m, and seconds s
  d = h + m/60 + s/3600;
end
>> hms2decimal(6,27,18)
ans =
    6.4550

Problem 3: Write a function decimal2hms that converts a time in decimal hours to hours, minutes, and seconds. Use it to compute the hours, minute, and seconds equivalent of 18.782 hours.

function [h,m,s] = decimal2hms(t)
% compute hours h, minutes m, and seconds s from time t in decimal hours
 
  % change all times to positive for ease of conversion
  sig = sign(t);
  t = abs(t);
                % at this point t = xxx.xxxxx hours
  h = floor(t); % get # hours by rounding t down to next integer
  t = t - h;    % remove integer hour part, so that t is 0.xxxxx hr
 
  t = t*60;     % convert t to minutes: 0.xxxx hr * 60 min/hr = yy.yyy min
  m = floor(t); % get # minutes by rounding t down to next integer
  t = t - m;    % remove integer hour part, so that t is 0.yyy min
 
  t = t*60;     % convert t to seconds: 0.yyyy min * 60 sec/min = zz.zzz min
  s = t;        % no further conversion, so keep decmial part of seconds
 
end

First test decimal2hms by inverting the decimal time we got from hms2decimal for 6 hrs, 27 min, 18 sec.

>> [h,m,s] = decimal2hms(6.455)
h =
     6
m =
    27
s =
   18.0000

Hooray! We get the same answer. Now evaluate 18.782 hours as asked

>> [h,m,s] = decimal2hms(18.782)
h =
    18
m =
    46
s =
   55.2000

Problem 4: Write a function polar2cartesian that converts polar coordinates r, theta to Cartesian coordinates x,y. Use it to compute the Cartesian coordinates of r=2, theta=pi/6.

fucntion [x,y] = polar2cartesian(r, theta);
% convert polar (r,theta) coordinates to Cartesian (x,y)
  x = r*cos(theta);
  y = r*sin(theta);
end
polar2cartesian
>> [x,y] = polar2cartesian(2,pi/6)
x =
    1.7321
y =
    1.0000

Problem 5: Write a function cartesian2polar that converts Cartesian coordinates x,y to polar coordinates r, theta. Use it to compute the polar coordinates of x=2, y=3. Hint: use the arctangent function atan to compute theta from x and y.

function [r, theta] = cartesian2polar(x, y)
% convert Cartesian coordinates (x,y) to polar (r, theta)
  r = sqrt(x^2 + y^2)
  theta = atan(y/x)
end

Test this code by inverting x,y answer from previous problem. Should give us r, theta = 2, pi/6.

>> [r, theta] = cartesian2polar(1.7321,1)
r =
    2.0000
theta =
    0.5236
>> pi/6
ans =
    0.5236

Yay!

Problem 6: Are there any values of x,y for which your function from problem 4 fails? Try these values of (x,y): (1,0), (-1,0), (0,0). Revise cartesian2polar function to fix these the problems these examples point out using if-else statements, so that your function gives correct r, theta values for all x,y values.

function [r, theta] = cartesian2polar(x, y)
% convert Cartesian coordinates (x,y) to polar (r, theta)
  r = sqrt(x^2 + y^2);
 
  % the computation atan(y/x) is problematic for x == 0, so spell out 
  % possible cases without doing this division
  if x == 0 
    if y == 0         % x,y is origin => theta is arbitray, set it to zero 
      theta = 0.0;  
    elseif y>0        % x,y is on positive y axis, set theta to pi/2
      theta = pi/2;
    else              % x,y is on negative y axis, set theta to -pi/2
      theta = -pi/2;  
    end
 
  % for x ~= 0, there are still possible problems with atan(y/x)  since
  % x,y == -1,-1 in 3rd quadrant will produce the same theta as x,y == 1,1,
  % which is in the 1st quadrant!! Experimenting with Matlab's atan function 
  % a bit shows it returns atan(theta) in the range -pi/2 to /pi/2 (4th and
  % 1st quadrants). So we have to adjust theta if x,y falls in the 2nd or
  % 3rd quadrant. 
  else 
    theta = atan(y/x); % produces theta in 1st or 2nd quadrant
    if x <= 0 & y>=0       % x,y is really in 2nd quadrant
      theta = theta + pi;  % add pi to move theta from 4th to 2nd quadrant
    elseif x => 0 & y<=0   % x,y is really in 3rd quadrant
      theta = theta + pi;  % subtract pi to move theta from 1st to 3rd quadrant
    end
 
  % theta will now fall in the range -pi to pi.
end

Now test the code on the origin and (x,y) == (r cos theta, r sin theta) for theta ranging from -pi to pi

>> [r,theta] = cartesian2polar(0,0)
r =
     0
theta =
     0
>> r=1; theta=-pi; [r,theta] = cartesian2polar(r*cos(theta),r*sin(theta))
r =
     1
theta =
   -3.1416
>> r=1; theta=-3*pi/4; [r,theta] = cartesian2polar(r*cos(theta),r*sin(theta))
r =
     1
theta =
   -2.3562

etc. Continuing around the unit circle in steps of delta theta = pi/4, it gets them all right.

Problem 7: Write a tempconvert function that converts a temperature in any of C, F,or K units and converts it to any desired units. The function should take a single input argument t. It should then prompt the user for the units of t using an input statement, prompt again for the desired units of the output, print a statement using fprintf of the form 67 F is equivalent to 292.594 K (where the numbers and units depend on the input values), and then return the numerical value of the temperature in the desired output units. If the input temperature is below absolute value, the program should print an error message and return absolute zero in the desired units.

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

A few non-comprehensive tests:

>> 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
gibson/teaching/fall-2014/math445/hw4solns.txt · Last modified: 2014/10/23 09:09 by gibson