function [mV] = t_inC_to_mV(Tin, Tref)
    % by Eugeniy E. Mikhailov 2020/09/01
    % Converts temperature in C to mV reading of type-T thermocouple
    % Tref corresponds to temperature of the reference,
    % i.e. the temperature at which the other end is kept. See
    % https://en.wikipedia.org/wiki/Thermocouple.
    % Tref is optional argument, if it is not supplied it assumed that
    % Tref = 0
    %
    % Conversion coefficients  are from
    % https://srdata.nist.gov/its90/download/type_t.tab
    % for temperature range -270C to 0C
    % NIST formula mV = sum(i=0 to n) c_i t^i.
    % IMPORTANT: the NIST formula assumes that reference is kept at Tref=0C
    % Since we mostly interested in below 0 temperatures, we do a sing flip
    % for voltage relative to NIST table
        
    c_i_below_0 = [0.000000000000E+00
        0.387481063640E-01
        0.441944343470E-04
        0.118443231050E-06
        0.200329735540E-07
        0.901380195590E-09
        0.226511565930E-10
        0.360711542050E-12
        0.384939398830E-14
        0.282135219250E-16
        0.142515947790E-18
        0.487686622860E-21
        0.107955392700E-23
        0.139450270620E-26
        0.797951539270E-30];
    c_i_above_0 = [0.000000000000E+00
        0.387481063640E-01
        0.332922278800E-04
        0.206182434040E-06
        -0.218822568460E-08
        0.109968809280E-10
        -0.308157587720E-13
        0.454791352900E-16
        -0.275129016730E-19];
    
    mV = zeros(size(Tin));
    for i=1:length(Tin)
        T = Tin(i);
        if T <= 0
            c_i=c_i_below_0;
        else
            c_i = c_i_above_0;
        end
        % matlab has builtin polynomial calculation
        % but order of coefficients is different
        c=flipud(c_i);
        if nargin == 1
            mV(i) = -polyval(c, T); % flipping the sign relative to NIST values
        else
            mV(i) = t_inC_to_mV(T) - t_inC_to_mV(Tref);
        end
    end
end
