function matlab_intro_part_1_function
%MATLAB_INTRO_PART_1_MFILES This function M-file covers the usage of MATLAB
%M-files and how to create user-defined functions.
%   In this example, let us calculate the compressibility factor, Z, of a
%   pure gas by using the generalized virial equation.

The equation is given by:

$$Z = 1 + \frac{P_r}{T_r}(B^0 + \omega B^1)$$

where:

$$B^0 = 0.083 - \frac{0.422}{T_r^{1.6}}$$

$$B^1 = 0.139 - \frac{0.172}{T_r^{4.2}}$$

    % unit class
    u = cmu.units;

    % Data for methane
    P = 15*u.bar; % pressure
    Pc = 45.99*u.bar; % Critical pressure
    T = 400*u.K; % Temperature
    Tc = 190.6*u.K; % Critical temperature
    omega = 0.012; % Acentric factor

    Z = virialgen(P,Pc,T,Tc,omega);
    str = sprintf('Methane at %.2f and %.2f has Z = %.4f', P, T, Z);
    disp(str);
end

function Z = virialgen(P,Pc,T,Tc,omega)
    Pr = P/Pc;
    Tr = T/Tc;
    [B0,B1] = virialB(Tr);
    Z = 1 + Pr/Tr*(B0 + omega*B1);
end

function [B0,B1] = virialB(Tr)
    B0 = 0.083 - 0.422/Tr^1.6;
    B1 = 0.139 - 0.172/Tr^4.2;
end
Methane at 1500000.00 (m/s^2*kg)^-1 and 400.00 K has Z = 0.9931