function e = emfKtype(T)
% emfKtype  EMF of K type thermocouple as function of temperature
%
% Synopsis:  e = emfKtype(T)
%
% Input:  T = temperature, degrees C,  -200 <= T <= 1372 C
%
% Output: e = thermocouple emf in Volts
e = zeros(size(T));    %  preallocate so e vector will be contiguous in memory
n = 1:length(T);       %  vector to monitor which T values have been converted
ihi = find(0<=T & T<=1372);  %  vector of indices such that 0 <= T(i) <= 1372
ilo = find(-270<=T & T<0);   %  vector of indices such that -270 <= T(i) <= 0
if ~isempty(ihi)
	c = [-1.2104721275e-26  9.7151147152e-23  -3.2020720003e-19  5.6075059059e-16 ...
       -5.6072844889e-13  3.1840945719e-10  -9.9457592874e-8   1.8558770032e-5  ...
        3.8921204975e-2  -1.7600413686e-2];
  e(ihi) = polyval(c,T(ihi)) + 0.1185976*exp( -1.183432e-4*(T(ihi)-126.9686).^2 );
  n(ihi) = 0;   %  these i values are done, set elements in monitor vector to zero
end
if ~isempty(ilo)
  c = [ -1.6322697486e-23  -1.9889266878e-20  -1.0451609365e-17  -3.1088872894e-15 ...
        -5.7410327428e-13  -6.7509059173e-11  -4.9904828777e-9   -3.2858906784e-7 ...
         2.3622373598e-5    3.9450128025e-2    0];
  e(ilo) = polyval(c,T(ilo));
  n(ilo) = 0;   %  these i values are done, set elements in monitor vector to zero
end
e = e*1e-3;   %  convert millivolts to volts
% --- Check to see if all elements of T vector have been converted
if any(n)
  nn = n(find(n~=0));   %  select only the nonzero indices
  warning(sprintf('T = %g value outside of range -270 <= T <= 1372 C\n',T(nn)));
end
