Page 1 of 1

Using other energy units than MHz

Posted: Wed Mar 23, 2016 3:07 am
by Charles
I don't like MHz and prefer K as an energy unit. Because I was tired of thinking every time how to convert it, I wrote a script which converts energy units and energy equivalent units. Maybe some of you can also use this:

Usage is for example

Sys.D = nrg_convert(-1.5,'K','MHz'); %ZFS: -1.5 Kelvin

Code: Select all

function result = nrg_convert(value,from,to)
%nrg_convert(value,from,to)
% Converts energy units or energy equivalent units
%
% Parameter value: the numeric value of the input
% Parameter from: the unit of the input 
% Parameter to: the unit of the destination
%
% form and to have to be deliverd as strings. Possible values are J
% (Joule), eV (electronvolt), Hz (Hertz), cm-1 (wavenumbers), K (Kelvin), T
% (Tesla), J/mol (joule per mol). You can also use the prefixes m
% (milli), k (kilo), M (Mega) and G (Giga), for example meV is
% millielectronvolt or GHz is Gigahertz.

%factors are from Pure and Applied Chemistry, Vol. 77, page 511
A = [      1     6.24151e18  1.50919e33 5.03411e22  7.24292e22  1.07828e23  6.02214e23 ;
     1.60218e-19       1     2.41799e14 8.06554e03  1.16045e04  1.72760e04  9.64853e04 ;
     6.62607e-34 4.13567e-15       1    3.33564e-11 4.79922e-11 7.14477e-11 3.99031e-09;
     1.98645e-23 1.23984e-04 2.99792e10       1     1.43877e00  2.14195e01  1.9626e01  ;
     1.38066e-23 8.61739e-05 2.08367e10 6.95039e-01       1     1.48874e01  8.31451e01 ;
     9.27402e-24 5.78839e-05 1.39963e10 4.66864e-01 6.71710e-01       1     5.58494e01 ;
     1.66045e-18 1.03642e-05 2.50607e09 8.35933e-02  1.20272e-01 1.79053e-01      1   ];
units = {'J', 'eV', 'Hz', 'cm-1', 'K', 'T', 'J/mol'};

%Prefix form

switch from(1)
    case 'm'
    value = value*1e-3;
    from(1) = [];
    case 'k'
    value = value*1e3;
    from(1) = [];
    case 'M'
    value = value*1e6;
    from(1) = [];
    case 'G'
    value = value*1e9;
    from(1) = [];
end;

%Prefix to

switch to(1)
    case 'm'
    value = value/1e-3;
    to(1) = [];
    case 'k'
    value = value/1e3;
    to(1) = [];
    case 'M'
    value = value/1e6;
    to(1) = [];
    case 'G'
    value = value/1e9;
    to(1) = [];
end;

%Which unit?

for i=1:length(units)
    if(strcmp(from,units{i}))
        fromi = i;
    end;
    if(strcmp(to,units{i}))
        toi = i;
    end;
end;

%Is a valid unit given?
if(~ exist('fromi','var'))
    error(['parameter from (',from,') is not a valid unit. Use one of the following: J, eV, Hz, cm-1, K, T, J/mol']);
end;

if(~ exist('toi','var'))
    error(['parameter to (',to,') is not a valid unit. Apply one of the following: J, eV, Hz, cm-1, K, T, J/mol']);
end;

%convert...

result = value * A(fromi,toi);
end