% lect9_4: Converting units of energy % Convert a quantity of energy given in Joules J), ft-lbs (ft-lb), calories (cal), % or electron volts (eV) to another unit. % Obtain the energy value, current units, and output units Ein = input('Enter the value of energy to be converted: '); EinUnits = input('Enter the current units (J, ft-lb, cal, eV): ','s'); EoutUnits = input('Enter the new units (J, ft-lb, cal, eV): ','s'); % Clear the error flag ErrorFlag = 0; % Convert Ein to Joules switch EinUnits case 'J' EJ = Ein; case 'ft-lb' EJ = Ein/0.738; case 'cal' EJ = Ein/0.239; case 'eV' EJ = Ein/6.24e18; otherwise ErrorFlag = 1; end % Convert Ein (J) to new units switch EoutUnits case 'J' Eout = EJ; case 'ft-lb' Eou = EJ*0.738; case 'cal' Eout = EJ*0.239; case 'eV' Eout = EJ*6.24e18; otherwise ErrorFlag = 1; end % Check to see if there was an error. If not, print results. if ErrorFlag disp('Error: current or new units are typed incorrectly.'); else fprintf('E = %g %s\n', Eout, EoutUnits); end