No BSD License  

Highlights from
Unit Conversion

image thumbnail

Unit Conversion

by

 

25 Mar 2003 (Updated )

MATLAB function, GUI & Simulink block for Unit Conversion.

unitconversion(type,input,output,user_sg);
function [factor,offset] = unitconversion(type,input,output,user_sg);
%unitconversion   Factors for engineering units conversion.
%   [Factor,Offset] = unitconversion(Type,In,Out,SG)
%   Returns the factor & offset for chosen unit conversion.
%   Type is unit type index (see chart below).
%   In is input unit in/out index (see chart below).
%   Out is output unit in/out index (see chart below).
%   SG is an optional spec gravity for converting flows (default 1).
%
%   unitconversion works on matrices but any which are must be the same size.
%
%    in/out: 1        2        3        4        5         6         7        8        9  
% type     
% 1 Angle    cycle    rad      deg      min      sec 
% 2 Area     km2      hectare  m2       cm2      mm2       mile2     acre     ft2      in2 
% 3 Density  tonne/m3 kg/m3    sg       lb/in3   lb/ft3    lb/igal   lb/usgal slug/ft3
% 4 Energy   MJ       J (Nm)   kW.hr    cal      Chu       therm     Btu      ft.lbf   hp.s
% 5 Flow     m3/s     litre/s  cc/s     in3/s    gal/hr    kg/s      lb/s     lb/hr
% 6 FlowNum  cc/s /   cc/s /   gph /    in3/s /  
%            sq(kPa)  sq(bar)  sq(psi)  sq(psi)
% 7 Force    bar.m2   kN       N        kPa.mm2  dyne      kgf       tonf     lbf      pdl
% 8 Freq     1/ms     Hz       rad/s    deg/s    rpm       1/hour    1/day 
% 9 FuelCons 100km/l  km/ltr   m/ltr    mi/i-gal mi/us-gal   
%10 Inertia  kg.m2    lb.ft2   lb.in2   
%11 Length   km       m        cm       mm       n-mile    mile      yard     ft       in 
%12 Velocity km/s     km/hr    m/s      m/min    m/hr      knot      mph      ft/s     ft/hr  
%13 Mass     tonne    kg       g        ton      slug      stone     lb       oz       dr 
%14 Power    kW       W        Chu/s    Btu/s    Btu/hr    ft.lbf/s  PS       hp       
%15 Pressure MPa      bar      kPa      Pa       atm       ton/in2   psi      mm(Hg)
%16 SpecHeat kJ/kg.K  J/kg.K   Chu/lb.K Btu/lb.R hp.s/lb.K ft.lbf/lb.R
%17 Temp     K        C        R        F
%18 Kin Vis  m2/s     cm2/s    cStoke   ft2/s    in2/s
%19 Volume   m3       litre    cm3      mm3      ft3       in3       i-gal    pint     us-gal 

%   Author: Mike Thomson   4 August 2001 
%   Version2: 18 November 2003 - Definition of therm corrected

% Check inputs & set defaults
error(nargchk(3,4,nargin));
if nargin<=3, user_sg=1; end;  % Make sure there's a user_sg

%Check param sizes
dims=[ndims(type),ndims(input),ndims(output),ndims(user_sg)];
sizes={size(type);size(input);size(output);size(user_sg)};
bigmxip=find(dims>2);
matrxip=find([length(type(:)),length(input(:)),length(output(:)),length(user_sg(:))]>1);
if ~isempty(bigmxip),
   %if any are big they must be the same dims, & same size
   if any(dims(bigmxip)~=dims(bigmxip(1))), error('Bad input size'); end;  %check matrix dims
   if any(any(cat(1,sizes{bigmxip})~=ones(length(bigmxip),1)*sizes{bigmxip(1)})), error('Bad input size'); end;  %check matrix sizes
   %if any are big the same ones must be matrix
   if length(bigmxip)~=length(matrxip) | bigmxip~=matrxip, error('Bad input size'); end;  %check matrix size
else
   %if none big, any matrix must be same size
   if ~isempty(matrxip),
      if any(any(cat(1,sizes{matrxip})~=ones(length(matrxip),1)*sizes{matrxip(1)})),
         error('Bad input size');   %check matrix sizes
      end
   end
end

% Check input values
if any(type(:)<1) | any(type(:)>19);error('Type out of range');end;
if any(input(:)<1) | any(input(:)>9);error('Input out of range');end;
if any(output(:)<1) | any(output(:)>9);error('Output out of range');end;
if any(user_sg(:)<=0), error('Invalid SG value'); end;

% Determine output size
if ~isempty(matrxip),
   opsiz=sizes{matrxip(1)};
else
   opsiz=[1,1];
end
oplen=prod(opsiz);

% Define basic numbers
yd=1/0.9144; yd2=yd^2; yd3=yd*yd2; mi=1760; acr=4840; gal=1/4.546092; usgl=231; 
lb=1/0.45359237; ton=2240; g=9.80665; atm=101.325; hg=750.0616827e-5; 
ps=1/75; hp=550; cal=0.238846; chu=1/1899.1005354; k2r=1.8; abz=273.15;
sg=1;

% For future development - if required
% angstrom=1e-10m; rod=5.5yard; chain=22yard; furlong=10chain; fathom=2yard;
% short_ton=2000 lb; cwt=112 lb; lb=7000grain; metric_carat=0.2g;
% chaldron=36bushel; barrel=36gal bushel=8gal; peck=2gal; quart=2pint; pint=4gill; gill=5fl.oz; fl.oz=8fl.drachm

fact = [1/2,pi,180,180*60,180*3600,0,0,0,0;...
1e-6,1e-4,1,1e4,1e6,yd2/mi^2,yd2/acr,9*yd2,1296*yd2;...
1e-3,1,1e-3,lb/(46656*yd3),lb/(27*yd3),lb/(1e3*gal),lb*usgl/(46656*yd3),lb/(81*yd2^2*g),0;...
1e-6,1,1e-6/3.6,cal,chu,chu*k2r*1e-5,chu*k2r,3*yd*lb/g,3*yd*lb/g/hp;...
1e-6,1e-3,1,0.046656*yd3,gal*3.6,sg*1e-3,lb*sg*1e-3,lb*sg*1e-1*36,0;...
1,10,gal*yd*12.96*sqrt(0.1*g/lb),(0.1296*yd2)^2*sqrt(10*g/lb),0,0,0,0,0;...
1e-5,1e-3,1,1e3,1e5,1/g,lb/(g*ton),lb/g,lb*yd*3;...
1e-3,1,2*pi,360,60,3600,86400,0,0;...
1e-5,1e-3,1,yd/(mi*gal),yd*usgl/(mi*yd3*46.656),0,0,0,0;...
1,lb*9*yd2,lb*1296*yd2,0,0,0,0,0,0;...
1e-3,1,100,1000,1/1852,yd/mi,yd,3*yd,36*yd;...
1e-3,3.6,1,60,3600,3600/1852,yd*3600/mi,3*yd,3*yd*3600;...
1e-3,1,1000,lb/ton,lb/(yd*3*g),lb/14,lb,lb*16,lb*16^2;...
1e-3,1,chu,chu*k2r,chu*k2r*3600,3*yd*lb/g,ps/g,3*yd*lb/g/hp,0;...
1e-6,1e-5,1e-3,1,1e-3/atm,lb/(1296*ton*g*yd2),lb/(1296*g*yd2),hg,0;...
1e-3,1,chu/lb,chu/lb,3*yd/(g*hp),3*yd/g/k2r,0,0,0;...
1,1,k2r,k2r,0,0,0,0,0;...
1,1e4,1e6,9*yd2,1296*yd2,0,0,0,0;...
1,1e3,1e6,1e9,27*yd3,27*yd3*1728,1e3*gal,gal*8000,yd3*46656/usgl];

off = [abz,0,abz,32/k2r,0,0,0,0,0];

% Set factor
% sub2ind indexes into a matrix; matrix(a,b) is a matrix if both a & b are vectors
ind=sub2ind( size(fact), ones(oplen,1).*type(:), ones(oplen,1).*input(:) );
in_fact=fact(ind); 
ind=sub2ind( size(fact), ones(oplen,1).*type(:), ones(oplen,1).*output(:) );
out_fact=fact(ind); 

% Check choices are in list
if any(any(in_fact==0)) | any(any(out_fact==0)), error('Invalid unit choice'); end;

% Set offset - apply if type==17(temp), else 0
in_off=off(input(:))'.*(type(:)==17);
out_off=off(output(:))'.*(type(:)==17);

% Previous un-vectorised code:-
% If user chose Qflow & Mflow use sg - sg defaults 1, keep here in case it's changed
%if type==5 & ((input>5) ~= (output>5))
%  if input>5 then
%     in_fact = in_fact(:) .* user_sg(:)/sg;
%  else
%     out_fact = out_fact(:) .* user_sg(:)/sg;
%  end;
%end;
% This sg thing adds a complication for vector/matrix arithmetic:
flow_conv=(type(:)==5 & ((input(:)>5) ~= (output(:)>5))); % vector of whether sg is needed
ip_gt_5=(input(:)>5)*2-1;  % vector of 1 or -1 s : 1->multiply; (-1)->divide
in_fact = in_fact(:) .* ((user_sg(:)/sg) .^ flow_conv).^ip_gt_5;

% Set factor & offset
factor=out_fact(:)./in_fact(:);
offset=(out_off(:)-in_off(:)).*out_fact(:);

% Reshape to match input matrix size
factor=reshape(factor,opsiz);
offset=reshape(offset,opsiz);

Contact us