Project

Data Parsing with MATLAB

Aim: To extract data from therm.data file and plot graph for relation of temprature with specific heat, enthalpy. entropy for different elements.

Theory:

Data parsing is a process of extracting data from a file and use that data for further calculation or study or making some important comclusions.

In the study presented here, NASA thermodynamic data is used which gives 14 coefficients that describes relation of specific heat, enthalpy, entropy with temprature.

The formulas are as follows:

Here, R is universal gas constant and T is temprature.

In data file, The first 7 coefficients are high temperature coefficients and the second 7 coefficients are low temperature coefficients.

MATLAB Main Program:

% Program to Parse NASA\'s thermodynamic data
clear all
close all
clc

% Universal Gas constant(R)
R = 8.314;

% loading data file
File = fopen(\'THERMO.dat\',\'r\');

% reads 1st line
fgetl(File);

% collect Global temprature
tline = fgetl(File);
Global_Temp = strsplit(tline,\' \');
Gobal_low_temp = str2num(Global_Temp{2});
Gobal_mid_temp = str2num(Global_Temp{3});
Gobal_high_temp = str2num(Global_Temp{4});

% skip comments
fgetl(File);
fgetl(File);
fgetl(File);

for i = 1 : 53  % to iterate through all 53 elements

    % reads species
    line1 = fgetl(File);
    species = get_species(line1);
    
    % calculate molecular weight and print it
    molecular_weight = get_molecular_weight(species);
    fprintf(\'\\nMolecular Weight of %s is : %d\',species,molecular_weight)
    
    % Collect local temprature range
    local_temp_range = get_local_temp_range(line1);

    % collect Coefficient data
    a(1:5) = get_coeff(fgetl(File));
    a(6:10) = get_coeff(fgetl(File));
    a(11:14) = get_coeff(fgetl(File));

    % Temprature 
    T = linspace(local_temp_range(1),local_temp_range(3),500);

    % Specific Heat
    Cp = Specific_heat(a, local_temp_range, T, R);

    % Enthalpy
    H = Enthalpy(a, local_temp_range, T, R);

    % Entropy
    S = Entropy(a, local_temp_range, T, R);

    % Plotting and saving in folder

    current_dir = pwd;
    mkdir(species);     % creates new folder with species name
    cd(species);        % open that folder

    % plotting temprature vs specific heat
    figure(1)
    plot(T,Cp,\'linewidth\',2,\'color\',\'b\')
    xlabel(\'Temprature[K]\');
    ylabel(\'Specific Heat[KJ/Kg-K]\');
    title(sprintf(\'Temprature vs Specific Heat plot for %s\',species))
    grid on

    % Save jpg of figure(1)
    saveas(figure(1), \'Specific Heat.jpg\')

    % plotting temprature vs enthalpy
    figure(2)
    plot(T,H,\'linewidth\',2,\'color\',\'b\')
    xlabel(\'Temprature[K]\');
    ylabel(\'Enthalpy[KJ/Kg]\');
    title(sprintf(\'Temprature vs Enthalpy plot for %s\',species))
    grid on

    % Save jpg of figure(2)
    saveas(figure(2), \'Enthalpy.jpg\')

    % plotting temprature vs entropy
    figure(3)
    plot(T,S,\'linewidth\',2,\'color\',\'b\')
    xlabel(\'Temprature[K]\');
    ylabel(\'Entropy[KJ/Kg-K]\');
    title(sprintf(\'Temprature vs Entropy plot for %s\',species))
    grid on

    % Save jpg of figure(3)
    saveas(figure(3), \'Entropy.jpg\')

    cd(current_dir) % goes back to parent folder

end

get_species Function:

function species = get_species(line)

temp = strsplit(line, \' \');
species = (temp{1});

end

This function from a given line returns the species of the element.

get_molecular_weight Function:

function molecular_weight = get_molecular_weight(species)

molecular_weight = 0;
Atoms = [\'O\', \'H\', \'N\', \'C\', \'S\', \'A\'];
Atomic_weights = [16, 1, 14, 12, 32, 40];

for i = 1 : length(species)
    for j = 1 : length(Atoms)
        if strcmpi(species(i), Atoms(j))
            molecular_weight = molecular_weight + Atomic_weights(j);
            mark = j;
        end
    end
    k = str2num(species(i));
        if k>1
            molecular_weight = molecular_weight + Atomic_weights(mark)*(k-1);
        end
        
    
end

This function calculates the molecular weight of given species.

In this function each constituent of the species is comapred with standard atoms to calculate molecular weight.

get_local_temp_range:

function [temp] = get_local_temp_range(line)

split = strsplit(line, \' \');
l = length(split);
mid_temp = str2num(split{l-1});
high_temp = str2num(split{l-2});
low_temp = str2num(split{l-3});

temp = [low_temp mid_temp high_temp];
end

This function return local temprature range for each species.

It takes line containing local tempratures as argument.

get_coeff Function:

function [a] = get_coeff(line)

E = findstr(line,\'E\');
a(1) = str2num(line(1:E(1)+3));
for i = 1 : length(E)-1
    temp = str2num(line(E(i)+4:E(i+1)+3));
    a(i+1) = temp;
end

end

This function returns coeffiecients from a given line.

Specific_heat Function:

function Cp = Specific_heat(a, local_temp_range, T, R)

for i = 1 : length(T)
    if T(i) <= local_temp_range(2)
        Cp(i) = R*(a(8) + a(9)*T(i) + a(10)*T(i)^2 + a(11)*T(i)^3 + a(12)*T(i)^4);
    else
        Cp(i) = R*(a(1) + a(2)*T(i) + a(3)*T(i)^2 + a(4)*T(i)^3 + a(5)*T(i)^4);
    end
end

Enthalpy Function:

function H = Enthalpy(a, local_temp_range, T, R)

for i = 1 : length(T)
    if T(i) <= local_temp_range(2)
        H(i) = R*T(i)*(a(8) + a(9)*T(i)/2 + a(10)*(T(i)^2)/3 + a(11)*(T(i)^3)/4 + a(12)*(T(i)^4)/5 + a(13)/T(i));
    else
        H(i) = R*T(i)*(a(1) + a(2)*T(i)/2 + a(3)*(T(i)^2)/3 + a(4)*(T(i)^3)/4 + a(5)*(T(i)^4)/5 + a(6)/T(i));
    end
end

Entropy Function:

function S = Entropy(a, local_temp_range, T, R)

for i = 1 : length(T)
    if T(i) <= local_temp_range(2)
        S(i) = R*(a(8)*log(T(i)) + a(9)*T(i) + a(10)*(T(i)^2)/2 + a(11)*(T(i)^3)/3 + a(12)*(T(i)^4)/4 + a(14));
    else
        S(i) = R*(a(1)*log(T(i)) + a(2)*T(i) + a(3)*(T(i)^2)/2 + a(4)*(T(i)^3)/3 + a(5)*(T(i)^4)/4 + a(7));
    end
end

Specific heat, Enthalpy and Entropy Functions calculates Cp, H and S respectively for a given temprature range.

Local Temprature Range is used for plotting Graphs.

Output:

Output Files:

Plots for O2:

Plot for N2:

Plot for CO2:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.