Parsing NASA thermodynamic data
AIM :
To calculate specific heat, enthalpy and entropy for all species in the given data file using the co-efficients,
and plot it against various temperature ranges,
and to calculate molecular weight of all the species in the given data file.
MAIN PROGRAM :
% To calculate specific heat, enthalpy and entropy for all species in the given data file using the co-efficients,
...and plot it against various temperature ranges,
...and to calculate molecular weight of all the species in the given data file.
clear all
close all
clc
% Universal gas constant ( in 'J/mol-K' )
R = 8.314;
% Reading the given data file using 'fopen' and returning the file ID 'f'
f = fopen('THERMO.dat','r');
% Removes the 'Species' folder, Incase the 'Species' folder is present in the current directory
del = rmdir('Species','s');
% Preallocation of variables inside the loops
species_data = cell(218,1);
species_specific_lines = cell(53,1);
species_names = cell(1,53);
fig_Cp = gobjects(1,53);
fig_H = gobjects(1,53);
fig_S = gobjects(1,53);
Mol_wt = zeros(1,53);
% for each iteration from 1 to 218, a line is read from the data file
...and stored in a cell array 'species_data'
for a = 1 : 218
species_data{a} = fgetl(f);
end
% Closes the datafile
fclose(f);
% Getting the specific line which has the global temperatures in the cell array
stg_global_temp = species_data{2};
% Splitting the line that contains the global temperatures
...by using space delimiter in 'strsplit' command and
...getting the different global temperatures in that line
global_temp = strsplit(stg_global_temp,' ');
% Converting the global temperatures from string to a integer
...using the command 'str2double' and storing the values individually
global_low_temp = str2double(global_temp{2});
global_mid_temp = str2double(global_temp{3});
global_high_temp = str2double(global_temp{4});
% Initial counter value of the loop
c = 1;
% Getting the lines that contains species names from 'species_data'
for b = 6 : 4 : 217
species_specific_lines{c} = species_data{b};
c = c + 1;
end
% Splitting the cell array 'species_specific_lines' using 'strsplit' command by
...space delimiter, storing it in each cell of 'd' and getting the first cell
...from 'd' which has the species name, over the 53 iterations of 'for' loop
...to get '53' species names
for k = 1 : 53
d = strsplit(species_specific_lines{k},' ');
species_names{k} = d{1};
end
% Initial counter value of the loop
ct = 1;
% Since, to get to a particular line of the species, the 'for' loop starts has
...the index variable with the intial value from 6 to the end value of 217 by
... incremental factor of 4
for n = 6 : 4 : 217
% For each iteration, inputs 'R', 'species_data' and 'n' are passed to the function 'Species_therm_prop'
...which calculates and returns the values of C_p_L, H_L, S_L, C_p_H, H_H, S_H, T1, T2
[C_p_L, H_L, S_L, C_p_H, H_H, S_H, T1, T2] = Species_therm_prop(R, species_data, n);
% For each iteration, three figure windows are created with the respective species name by
... plotting 'Cp', 'H' and 'S' against various temperatures respectively
fig_Cp(ct) = figure('Name',[' Specific Heat of ', species_names{ct}],'visible','on');
hold on
plot(T1, C_p_L, 'LineWidth', 1, 'Color', 'b')
plot(T2, C_p_H, 'LineWidth', 1, 'Color', 'r')
xlabel("Temperature (T in 'K')")
ylabel("Specific heat (Cp in 'J/mol-K')")
legd_1 = legend('Cp for low temperature co-efficients','Cp for high temperature co-efficients');
set(legd_1, 'color', 'none')
title(['Specific heat vs Temperature for ' species_names{ct}])
grid on
hold off
fig_H(ct) = figure('Name',[' Enthalpy of ', species_names{ct}],'visible','on');
hold on
plot(T1, H_L, 'LineWidth', 1, 'Color','b')
plot(T2, H_H, 'LineWidth', 1, 'Color', 'r')
xlabel("Temperature (T in 'K')")
ylabel("Enthalpy (H in 'J/mol')")
legd_2 = legend(' Enthalpy for low temperature co-efficients','Enthalpy for high temperature co-efficients');
set(legd_2, 'color', 'none')
title(['Enthalpy vs Temperature for ' species_names{ct}])
grid on
hold off
fig_S(ct) = figure('Name',[' Entropy of ',species_names{ct}],'visible','on');
hold on
plot(T1, S_L, 'LineWidth', 1, 'Color', 'b')
plot(T2, S_H,'LineWidth', 1, 'Color', 'r')
xlabel("Temperature (T in 'K')")
ylabel("Entropy (S in 'J/mol-K')")
legd_3 = legend('Entropy for low temperature co-efficients','Entropy for high temperature co-efficients');
set(legd_3, 'color', 'none')
title(['Entropy vs Temperature for ' species_names{ct}])
grid on
hold off
% Creating the subfolder by respective species name in the folder 'Species' for each iteration
folder_status = mkdir('Species',species_names{ct});
% Changing the current folder to 'Species' folder
...and returning the previous folder directory to the variable 'previous_folder' for every iteration
previous_folder = cd('Species');
% Again changing the directory from the current folder 'Species' to the specific subfolder of the species
cd(species_names{ct})
% Saving each figure in 'png' format with a specific species name, for each iteration
saveas(fig_Cp(ct),['Specific Heat of ' species_names{ct}],'png')
saveas(fig_H(ct),['Enthalpy of ' species_names{ct}],'png')
saveas(fig_S(ct),['Entropy of ' species_names{ct}],'png')
% Getting back to the 'previous_folder' directory in order to run the program
cd(previous_folder)
% Incrementing the counter value for nest iteration
ct = ct + 1;
end
% Finding the molecular weight for every species in the data file and displaying it in the command window
% The individual elements which make up the species in the data file
Elements = ["H", "C", "N", "O", "A"];
% Atomic weight for those individual elements
At_wt = [1.00794, 12.0107, 14.0067, 15.9994, 39.948];
format short
for l = 1: 53
% For each iteration, a species name is stored in the variable 'species_name'
species_name = species_names{l} ;
% For each iteration, inputs 'Elements', 'At_wt' and 'species_name' are passed
...to the function 'Molecular_weight' which calculates and returns array of values to
...the variable 'ind_At_wt' containing atomic weight of each individual element of the species
ind_At_wt = Molcular_weight(Elements, At_wt, species_name);
% The sum of the values in the array 'ind_At_wt' is calculated and returned
... to the variable 'Mol_wt' for each iteration, which is the molecular weight of the species
Mol_wt(l) = sum(ind_At_wt);
% Displays the molecular weight of the each species in the command window
fprintf("The Molecular weight of %-6s is %10.4fnn", species_name, Mol_wt(l))
end
SUPPLEMENTARY PROGRAM :
1. Function that extracts the co-efficients from the data file and calculates the specific heat, enthalpy and entropy of all the species in the data file,
function [C_p_L, H_L, S_L, C_p_H, H_H, S_H, T1, T2] = Species_therm_prop(R, species_data, n)
% Getting the specific line that contains the local temperatures
...for the species in 'species_data'
stg_local_temp = species_data{n};
% Splitting the line that contains the local temperatures
...by using space delimiter in 'strsplit' command and
...getting the different local temperatures in that line
local_temp = strsplit(stg_local_temp,' ');
% Converting the local temperatures from string to a integer
...using the command 'str2double' and storing the values individually
local_low_temp = str2double(local_temp{end-3});
local_high_temp = str2double(local_temp{end-2});
local_mid_temp = str2double(local_temp{end-1});
% Local low temperature range of the species
T1 = local_low_temp : local_mid_temp;
% Local high temperature range of the species
T2 = local_mid_temp : local_high_temp;
% Getting the first line that contains the co-efficients for the species
co_effs_1 = species_data{n + 1};
% Finding the positions of 'E' in 'co_effs_1'
pos_E_1 = strfind(co_effs_1,'E');
format long
% Since we know from the data file that the number of digits that a co-efficient have,
...is upto the third digit after the the position of 'E' and after that, the next 'co-eff' starts
% Storing the first seven co-efficients which are the high temperature
...co-efficients for the species, in the variables from 'a_h_1' to 'a_h_7'
a_h_1 = str2double(co_effs_1(1 : pos_E_1(1)+3));
a_h_2 = str2double(co_effs_1(pos_E_1(1)+4 : pos_E_1(2)+3));
a_h_3 = str2double(co_effs_1(pos_E_1(2)+4 : pos_E_1(3)+3));
a_h_4 = str2double(co_effs_1(pos_E_1(3)+4 : pos_E_1(4)+3));
a_h_5 = str2double(co_effs_1(pos_E_1(4)+4 : pos_E_1(5)+3));
co_effs_2 = species_data{n + 2};
pos_E_2 = strfind(co_effs_2,'E');
a_h_6 = str2double(co_effs_2(1 : pos_E_2(1)+3));
a_h_7 = str2double(co_effs_2(pos_E_2(1)+4 : pos_E_2(2)+3));
% Similarly, Storing the last seven co-efficients which are the low temperature
...co-efficients for the species, in the variables from 'a_l_1' to 'a_l_7'
a_l_1 = str2double(co_effs_2(pos_E_2(2)+4 : pos_E_2(3)+3));
a_l_2 = str2double(co_effs_2(pos_E_2(3)+4 : pos_E_2(4)+3));
a_l_3 = str2double(co_effs_2(pos_E_2(4)+4 : pos_E_2(5)+3));
co_effs_3 = species_data{n + 3};
pos_E_3 = strfind(co_effs_3,'E');
a_l_4 = str2double(co_effs_3(1 : pos_E_3(1)+3));
a_l_5 = str2double(co_effs_3(pos_E_3(1)+4 : pos_E_3(2)+3));
a_l_6 = str2double(co_effs_3(pos_E_3(2)+4 : pos_E_3(3)+3));
a_l_7 = str2double(co_effs_3(pos_E_3(3)+4 : pos_E_3(4)+3));
% Preallocation of variables inside the loop
C_p_L = zeros(1,numel(T1));
H_L = zeros(1,numel(T1));
S_L = zeros(1,numel(T1));
C_p_H = zeros(1,numel(T2));
H_H = zeros(1,numel(T2));
S_H = zeros(1,numel(T2));
% Calculating Specific heat, enthalpy and entropy for low temperature
...co-efficients
for i = 1 : numel(T1)
C_p_L(i) = R * ( a_l_1 + (a_l_2 * T1(i)) + (a_l_3 * T1(i)^2) + (a_l_4 * T1(i)^3) + (a_l_5 * T1(i)^4));
H_L(i) = R * T1(i)*( a_l_1 + (a_l_2 * T1(i)/2) + (a_l_3 * (T1(i)^2)/3) + (a_l_4 * (T1(i)^3)/4) + (a_l_5 * (T1(i)^4)/5) + a_l_6/T1(i));
S_L(i) = R * ( a_l_1 * log(T1(i)) + (a_l_2 * T1(i)) + (a_l_3 * (T1(i)^2)/2) + (a_l_4 * (T1(i)^3)/3) + (a_l_5 * (T1(i)^4)/4) + a_l_7);
end
% Calculating Specific heat, enthalpy and entropy for high temperature
...co-efficients
for j = 1 : numel(T2)
C_p_H(j) = R * ( a_h_1 + (a_h_2 * T2(j)) + (a_h_3 * T2(j)^2) + (a_h_4 * T2(j)^3) + (a_h_5 * T2(j)^4));
H_H(j) = R * T2(j)*( a_h_1 + (a_h_2 * T2(j)/2) + (a_h_3 * (T2(j)^2)/3) + (a_h_4 * (T2(j)^3)/4) + (a_h_5 * (T2(j)^4)/5) + a_h_6/T2(j));
S_H(j) = R * ( a_h_1 * log(T2(j)) + (a_h_2 * T2(j)) + (a_h_3 * (T2(j)^2)/2) + (a_h_4 * (T2(j)^3)/3) + (a_h_5 * (T2(j)^4)/4) + a_h_7);
end
end
2. Function that is used to calculate the molecular weight of all species in the data file,
function ind_At_wt = Molcular_weight(Elements, At_wt, species_name)
% Intialization of the atomic weight for each individual atoms that make up the species
ind_At_wt = zeros(1,length(species_name));
% for the length of each species, the atomic weight of that individual element
...in the species is calculated and stores each value as array in 'ind_At_wt' for the species
% For each iteration of 'i'(length of 'species_name'), the respective
...element is selected by indexing the 'i'th value from the 'species_name' and
...is compared with each element in 'Elements' by indexing 'j'th value from 'Elements' for 'j' no. of iterations
for i = 1 : length(species_name)
for j = 1 : length(Elements)
% If the string comparison is true (i.e, equal to 1), then, the respective 'i'th value is indexed from 'At_wt'
... and stored in the 'i'th position of the array 'ind_At_wt', the loop breaks out from this nested 'for' loop
...and return back for next iteration of the main 'for' loop
if strcmp(species_name(i),Elements(j)) == 1
ind_At_wt(i) = At_wt(j);
break
% If the above comparison is not true and if 'i'th index from the 'species_name' is equal to '2',
...then, the (i-1)th value of 'ind_At_wt' is multiplied by 2 and stored in the position (i-1)th of 'ind_At_wt'
elseif species_name(i) == '2'
ind_At_wt(i-1) = 2 * ind_At_wt(i-1);
break
% Similarly, if the above two comparison is not true and if 'i'th index from the 'species_name' is equal to '3',
...then, the (i-1)th value of 'ind_At_wt' is multiplied by 3 and stored in the position (i-1)th of 'ind_At_wt'
elseif species_name(i) == '3'
ind_At_wt(i-1) = 3 * ind_At_wt(i-1);
break
% Therefore, the desired value is chosen if that condition satisfy
elseif species_name(i) == '4'
ind_At_wt(i-1) = 4 * ind_At_wt(i-1);
break
elseif species_name(i) == '5'
ind_At_wt(i-1) = 5* ind_At_wt(i-1);
break
elseif species_name(i) == '6'
ind_At_wt(i-1) = 6 * ind_At_wt(i-1);
break
elseif species_name(i) == '7'
ind_At_wt(i-1) = 7 * ind_At_wt(i-1);
break
elseif species_name(i) == '8'
ind_At_wt(i-1) = 8 * ind_At_wt(i-1);
break
elseif species_name(i) == '9'
ind_At_wt(i-1) = 9 * ind_At_wt(i-1);
break
end
end
end
ATTACHMENT :
- Screenshot of the window where the species folders are saved,

OUTPUT PLOTS :
- Specific heat of O2,

- Enthalpy of O2,

- Entropy of 02,

- Specific heat of N2,

- Enthalpy of N2,

- Entropy of N2,

- Specific heat of CO2,

- Enthalpy of CO2,

- Entropy of CO2,

OUTPUT shown in command window :
- Molecular weights for all the species in the data file,



FORMULAS USED :
- NASA 7 term polynomials which is used to calculate thermodynamic properties such as specific heat (Cp), enthalpy(H) and entropy(S) of more than 1000 species can be calculated was first invented by Zeleznik and gordon in the year 1962.
- The first 7 co-efficients of polynomial in the data file of a species are used for high temperature range and the second 7 co-efficients are used for low temperature range.
- The forlumas are as follows,
`C_p / R = a_1 + a_2 * T + a_3 * T^2 + a_4 * T^3 + a_5 * T^4`
`H / (R*T) = a_1 + a_2 * (T/2) + a_3 * ((T^2)/3) + a_4 * ((T^3)/4) + a_5 * ((T^4)/5) + a_6 / T`
`S / R = a_1 * ln(T) + a_2 * T + a_3 * ((T^2)/2) + a_4 * ((T^3)/3) + a_5 * ((T^4)/4) + a_7`
Where,
- `C_p` - Specific heat of the species (in 'J/mol-K')
- `H` - Enthalpy of the species (in 'J/mol')
- `S` - Entropy of the species (in 'J/mol-K')
- `R` - Universal gas constant ( 8.314 J/mol-K )
- `T` - Temperature (in 'K')
- `a_1,a_2,a_3,a_4,a_5,a_6 and a_7` - Co-efficients of the polynomial
EXPLANATION :
- Read all the lines from the data file into a cell array since it will be easy to get into any line of the data by cell indexing
- Get the species name from the cell array by getting into the specific lines that contains the species name in it, splitting it into individual cells and and storing the first cell of those into another array.
- Create a function to extract the local temperatures and respective co-efficients of the species from the cell array by getting into the line directly by giving respective inputs from the main script.
- Calculate specific heat(Cp), enthalpy(H) and entropy(S) for both high and low temperature range and return the output to the main script.
- Create multiple figures for Cp, H, and S against temperature (T) by the values returned from the function.
- Save the three figures for each species (53) obtained in seperate folders.
- Create a function that is used to calculate the molecular weight of all 53 species in given data file by finding the atomic weight of individual elements which is present in every species and calculating it individually.
- Then, this function returns a array of values for a particular species to the main script and these values are added to get the molecular weight of every species
- Finally, it is displayed in the command window using 'fprintf'.
CODE EXPLANATION :
clear all - Clears all the variables in the workspace.
close all - Closes all the figure windows.
clc - Clears the command window.
R = 8.314; - Universal gas constant ( in 'J/mol-K' )
f = fopen('THERMO.dat','r'); - Reads the file 'THERMO.dat' using the command 'fopen' and returns the integer to the file ID 'f'.
del = rmdir('Species','s'); - Removes the 'Species' folder, Incase the 'Species' folder is present in the current directory.
Preallocation of variables inside the loops
species_data = cell(218,1); - Creates a cell array that contains 218 rows and 1 column
species_specific_lines = cell(53,1); - Creates a cell array that contains 53 rows and 1 column
species_names = cell(1,53); - Creates a cell array containing 1 row and 53 columns
fig_Cp = gobjects(1,53); - Intialize array of graphical objects of length 1 x 53
fig_H = gobjects(1,53); - Intialize array of graphical objects of length 1 x 53
fig_S = gobjects(1,53); - Intialize array of graphical objects of length 1 x 53
Mol_wt = zeros(1,53); - Creates a row vector with zero values of length 1 x 53
For each iteration from 1 to 218, each line is read from the data file and stored in a cell array 'species_data'
for a = 1 : 218 - Runs 218 iterations, by using the index value 'a' of the loop which increments from 1 to 218
species_data{a} = fgetl(f); - Gets each consecutive line from the file ID 'f' for each iteration using the command 'fgetl' and stores it in a cell array 'species_data' of length 218 x 1
end - Ends the for loop
fclose(f); - Closes the datafile with file ID 'f' using the command 'fclose'
stg_global_temp = species_data{2}; - Gets the cell that contains the global temperatures by indexing the 2nd cell from 'species_data' and storing it in 'stg_global_temp'
global_temp = strsplit(stg_global_temp,' '); - Splitting the string in the cell array 'stg_global_temp' using the command 'strsplit' by giving space delimiter as a input
global_low_temp = str2double(global_temp{2}); - Converts the string in the 2nd cell of 'global_temp' to integer using the command 'str2double' and stores it in 'global_low_temp'
global_mid_temp = str2double(global_temp{3}); - Converts the string in the 3nd cell of 'global_temp' to integer using the command 'str2double' and stores it in 'global_mid_temp'
global_high_temp = str2double(global_temp{4}); - Converts the string in the 4nd cell of 'global_temp' to integer using the command 'str2double' and stores it in 'global_high_temp'
Storing specific lines that contain species names, from the cell array 'species_data'
c = 1; - Initial counter value of the loop
for b = 6 : 4 : 217 - Runs the loop for 53 times, by using the index value 'b' which increments from 6 to 217 by a step value of 4
species_specific_lines{c} = species_data{b}; - Getting the specific line by indexing the cell array 'species_data' by loop's index value 'b' and storing it each in 'species_specific_lines' for a
specific cell index position specified by 'c'
c = c + 1; - Incrementing the counter value 'c' for the next iteration of the loop
end - Ends this for loop
Storing the species names individually in the cell array 'species_names'
for k = 1 : 53 - Runs 53 iterations, by using the loop index 'k' which increments from 1 to 53
d = strsplit(species_specific_lines{k},' '); - Splitting each cell that contains string from 'species_specific_lines' by using the command 'strsplit' and storing it in individual cells of the cell array 'd'
species_names{k} = d{1}; - Getting the first string value for every iteration and storing it in each cell of 'species_names' specified by loop index 'k'
end - Ends this loop
ct = 1; - Initial counter value of the loop
Since, to get to a particular line of the species, the 'for' loop starts has the index variable 'n' with the intial value of 6 to the end value of 217 which increments by 4
for n = 6 : 4 : 217 - Runs the loop for 53 times, by using the index value 'n' which increments from 6 to 217 by a step value of 4
[C_p_L, H_L, S_L, C_p_H, H_H, S_H, T1, T2] = Species_therm_prop(R, species_data, n); - For every iteration of the loop, Passes the inputs 'R', 'species_data', and 'n' to the function 'Species_therm_prop' which calculates and returns 'C_p_L', 'H_L', 'S_L', 'C_p_H', 'H_H', 'S_H', 'T1' and 'T2'
fig_Cp(ct) = figure('Name',[' Specific Heat of ', species_names{ct}],'visible','on'); - Creates number of figure windows for number of iterations with name specified inside the square brackets and it is stored in the figure handle 'fig_Cp' with the index specified by 'ct'
hold on - Holds the same figure window for multiple plots
plot(T1, C_p_L, 'LineWidth', 1, 'Color', 'b') - Plots 'T1' against 'C_p_L' with linewidth of 1 and colour 'blue'
plot(T2, C_p_H, 'LineWidth', 1, 'Color', 'r') - Plots 'T2' against 'C_p_H' with linewidth of 1 and colour 'red'
xlabel("Temperature (T in 'K')") - Labels the x-axis as "Temperature (T in 'K')"
ylabel("Specific heat (Cp in 'J/mol-K')") - Labels the y-axis as "Specific heat (Cp in 'J/mol-K')"
legd_1 = legend('Cp for low temperature co-efficients','Cp for high temperature co-efficients'); - Creates legend for the plots with titles given as inputs to the command 'legend'
set(legd_1, 'color', 'none') - Sets the legend background to transparent
title(['Specific heat vs Temperature for ' species_names{ct}]) - Creates the title as mentioned inside square brackets for each figure window specified by index 'ct'
grid on - Makes grid lines visible in the figure window
hold off - Sets the 'hold' command to off
fig_H(ct) = figure('Name',[' Enthalpy of ', species_names{ct}],'visible','on'); - Creates number of figure windows for number of iterations with name specified inside the square brackets and it is stored in the figure handle 'fig_H' with the index specified by 'ct'
hold on - Holds the same figure window for multiple plots
plot(T1, H_L, 'LineWidth', 1, 'Color','b') - Plots 'T1' against 'H_L' with linewidth of 1 and colour 'blue'
plot(T2, H_H, 'LineWidth', 1, 'Color', 'r') - Plots 'T2' against 'H_H' with linewidth of 1 and colour 'red'
xlabel("Temperature (T in 'K')") - Labels the x-axis as "Temperature (T in 'K')"
ylabel("Enthalpy (H in 'J/mol')") - Labels the y-axis as "Enthalpy (H in 'J/mol')"
legd_2 = legend(' Enthalpy for low temperature co-efficients','Enthalpy for high temperature co-efficients'); - Creates legend for the plots with titles given as inputs to the command 'legend'
set(legd_2, 'color', 'none') - Sets the legend background to transparent
title(['Enthalpy vs Temperature for ' species_names{ct}]) - Creates the title as mentioned inside square brackets for each figure window specified by index 'ct'
grid on - Makes grid lines visible in the figure window
hold off - Sets the 'hold' command to off
fig_S(ct) = figure('Name',[' Entropy of ',species_names{ct}],'visible','on'); - Creates number of figure windows for number of iterations with name specified inside the square brackets and it is stored in the figure handle 'fig_S' with the index specified by 'ct'
hold on - Holds the same figure window for multiple plots
plot(T1, S_L, 'LineWidth', 1, 'Color', 'b') - Plots 'T1' against 'S_L' with linewidth of 1 and colour 'blue'
plot(T2, S_H,'LineWidth', 1, 'Color', 'r') - Plots 'T2' against 'S_H' with linewidth of 1 and colour 'red'
xlabel("Temperature (T in 'K')") - Labels the x-axis as "Temperature (T in 'K')"
ylabel("Entropy (S in 'J/mol-K')") - Labels the y-axis as "Entropy (S in 'J/mol-K')"
legd_3 = legend('Entropy for low temperature co-efficients','Entropy for high temperature co-efficients'); - Creates legend for the plots with titles given as inputs to the command 'legend'
set(legd_3, 'color', 'none') - Sets the legend background to transparent
title(['Entropy vs Temperature for ' species_names{ct}]) - Creates the title as mentioned inside square brackets for each figure window specified by index 'ct'
grid on - Makes grid lines visible in the figure window
hold off - Sets the 'hold' command to off
folder_status = mkdir('Species',species_names{ct}); - Creates folder 'Species' and, subfolder with name of the species from the cell array 'species_names' by index value 'ct' , for each iteration of the loop
previous_folder = cd('Species'); - Changes the current folder directory to 'Species' folder and returns the previous folder directory to 'previous_folder'
cd(species_names{ct}) - Changes from 'Species' folder directory to the respective subfolder of the species
saveas(fig_Cp(ct),['Specific Heat of ' species_names{ct}],'png') - Saves the figure 'fig_Cp' indexed by 'ct' in 'png' format with respective name given inside square brackets
saveas(fig_H(ct),['Enthalpy of ' species_names{ct}],'png') - Saves the figure 'fig_H' indexed by 'ct' in 'png' format with respective name given inside square brackets
saveas(fig_S(ct),['Entropy of ' species_names{ct}],'png') - Saves the figure 'fig_S' indexed by 'ct' in 'png' format with respective name given inside square brackets
cd(previous_folder) - Changes the current directory to previous directory specified by 'previous_folder'
ct = ct + 1; - Increments the counter value for next iteration
end - Ends this for loop
Finding the molecular weight for every species in the data file and displaying it in the command window
Elements = ["H", "C", "N", "O", "A"]; - Creates the row vector 'Elements' with individual elements which is used make the species in the data file
At_wt = [1.00794, 12.0107, 14.0067, 15.9994, 39.948]; - Creates row vector 'At_wt' with atomic weights for those individual elements prresent in 'Elements'
format short - Formats the values to have maximum of three digits after the decimal point
for l = 1: 53 - Runs the loop by index value 'l' which increments from 1 to 53
species_name = species_names{l} ; - For each iteration, a species name is stored in the variable 'species_name'
ind_At_wt = Molcular_weight(Elements, At_wt, species_name); - For each iteration, the inputs 'Elements', 'At_wt' and 'species_name' are passed to the function 'Molecular_weight' which calculates and returns a array of values to the variable 'ind_At_wt' which contains the atomic weight of each individual element of the species
Mol_wt(l) = sum(ind_At_wt); - Sums every value in the array 'ind_At_wt' and returns the output which contains molecular weight of the species to 'Mol_wt' for each iteration
fprintf("The Molecular weight of %-6s is %10.4fnn", species_name, Mol_wt(l)) - Displays the molecular weight of each species in the command window using the command 'fprintf' with field width and precision for each coversion character
end - Ends the loop
Function that extracts the co-efficients from the data file and calculates the specific heat, enthalpy and entropy of all the species in the data file,
function [C_p_L, H_L, S_L, C_p_H, H_H, S_H, T1, T2] = Species_therm_prop(R, species_data, n) - Function 'Species_therm_prop' that gets 'R', 'species_data' and 'n' as inputs and returns 'C_p_L', 'H_L', 'S_L', 'C_p_H', 'H_H', 'S_H', 'T1' and 'T2'
Getting the specific line that contains the local temperatures for specific species from 'species_data'
stg_local_temp = species_data{n}; - Gets each data from 'species_data' which was indexed by the variable 'n' and stores it in 'stg_local_temp'
local_temp = strsplit(stg_local_temp,' '); - Splits each cell in 'stg_local_temp' by using 'strsplit' command and stores it in the cell array 'local_temp'
local_low_temp = str2double(local_temp{end-3}); - Converts the string to number, in the third cell from last of 'local_temp' and stores it in 'local_low_temp'
local_high_temp = str2double(local_temp{end-2}); - Converts the string to number, in the second cell from last of 'local_temp' and stores it in 'local_high_temp'
local_mid_temp = str2double(local_temp{end-1}); - Converts the string to number, in the second cell from last of 'local_temp' and stores it in 'local_mid_temp'
Local low temperature range of the species
T1 = local_low_temp : local_mid_temp; - Creates a row vector 'T1' of values ranging from 'local_low_temp' to 'local_mid_temp'
Local high temperature range of the species
T2 = local_mid_temp : local_high_temp; - Creates a row vector 'T2' of values ranging from 'local_mid_temp' to 'local_high_temp'
Getting the first line that contains the co-efficients for the species
co_effs_1 = species_data{n + 1}; - Getting the data by indexing 'n+1'th cell from the cell array 'species_data' and storing it in 'cos_effs_1'
pos_E_1 = strfind(co_effs_1,'E'); - Finds the positions of 'E' in 'co_eff_1' and stores it in 'pos_E_1'
format long - Formats the values to have more than four digits after decimal point
Since we know from the data file that the number of digits that a co-efficient have, is upto the third digit after the the position of 'E' and after that, the next 'co-eff' starts
High temperature co-efficients of the species
a_h_1 = str2double(co_effs_1(1 : pos_E_1(1)+3)); - Converts the string to number, from 'cos_effs_1' by indexing from 1 to the sum of first value of 'pos_E_1' and 3, and stores the value in 'a_h_1'
a_h_2 = str2double(co_effs_1(pos_E_1(1)+4 : pos_E_1(2)+3)); - Converts the string to number, from 'cos_effs_1' by indexing from the sum of 1st value of 'pos_E_1' and 4, to the sum of 2nd value of 'pos_E_1' and 3, and stores the value in 'a_h_2'
a_h_3 = str2double(co_effs_1(pos_E_1(2)+4 : pos_E_1(3)+3)); - Converts the string to number, from 'cos_effs_1' by indexing from the sum of 2nd value of 'pos_E_1' and 4, to the sum of 3rd value of 'pos_E_1' and 3, and stores the value in 'a_h_3'
a_h_4 = str2double(co_effs_1(pos_E_1(3)+4 : pos_E_1(4)+3)); - Converts the string to number, from 'cos_effs_1' by indexing from the sum of 3rd value of 'pos_E_1' and 4, to the sum of 4th value of 'pos_E_1' and 3, and stores the value in 'a_h_4'
a_h_5 = str2double(co_effs_1(pos_E_1(4)+4 : pos_E_1(5)+3)); - Converts the string to number, from 'cos_effs_1' by indexing from the sum of 4th value of 'pos_E_1' and 4, to the sum of 5th value of 'pos_E_1' and 3, and stores the value in 'a_h_5'
co_effs_2 = species_data{n + 2}; - Getting the data by indexing 'n+2'th cell from the cell array 'species_data' and storing it in 'cos_effs_2'
pos_E_2 = strfind(co_effs_2,'E'); - Finds the positions of 'E' in 'co_eff_2' and stores it in 'pos_E_2'
a_h_6 = str2double(co_effs_2(1 : pos_E_2(1)+3)); - Converts the string to number, from 'cos_effs_2' by indexing from 1 to the sum of first value of 'pos_E_2' and 3, and stores the value in 'a_h_6'
a_h_7 = str2double(co_effs_2(pos_E_2(1)+4 : pos_E_2(2)+3)); - Converts the string to number, from 'cos_effs_2' by indexing from the sum of 1st value of 'pos_E_2' and 4, to the sum of 2nd value of 'pos_E_2' and 3, and stores the value in 'a_h_7'
Low temperature co-efficients of the species
a_l_1 = str2double(co_effs_2(pos_E_2(2)+4 : pos_E_2(3)+3)); - Converts the string to number, from 'cos_effs_2' by indexing from the sum of 2nd value of 'pos_E_2' and 4, to the sum of 3rd value of 'pos_E_2' and 3, and stores the value in 'a_l_1'
a_l_2 = str2double(co_effs_2(pos_E_2(3)+4 : pos_E_2(4)+3)); - Converts the string to number, from 'cos_effs_2' by indexing from the sum of 3rd value of 'pos_E_2' and 4, to the sum of 4th value of 'pos_E_2' and 3, and stores the value in 'a_l_2'
a_l_3 = str2double(co_effs_2(pos_E_2(4)+4 : pos_E_2(5)+3)); - Converts the string to number, from 'cos_effs_2' by indexing from the sum of 4th value of 'pos_E_2' and 4, to the sum of 5th value of 'pos_E_2' and 3, and stores the value in 'a_l_3'
co_effs_3 = species_data{n + 3}; - Getting the data by indexing 'n+3'th cell from the cell array 'species_data' and storing it in 'cos_effs_3'
pos_E_3 = strfind(co_effs_3,'E'); - Finds the positions of 'E' in 'co_eff_3' and stores it in 'pos_E_3'
a_l_4 = str2double(co_effs_3(1 : pos_E_3(1)+3)); - Converts the string to number, from 'cos_effs_3' by indexing from 1 to the sum of first value of 'pos_E_3' and 3, and stores the value in 'a_l_4'
a_l_5 = str2double(co_effs_3(pos_E_3(1)+4 : pos_E_3(2)+3)); - Converts the string to number, from 'cos_effs_3' by indexing from the sum of 1st value of 'pos_E_3' and 4, to the sum of 2nd value of 'pos_E_3' and 3, and stores the value in 'a_l_5'
a_l_6 = str2double(co_effs_3(pos_E_3(2)+4 : pos_E_3(3)+3)); - Converts the string to number, from 'cos_effs_3' by indexing from the sum of 2nd value of 'pos_E_3' and 4, to the sum of 3rd value of 'pos_E_3' and 3, and stores the value in 'a_l_6'
a_l_7 = str2double(co_effs_3(pos_E_3(3)+4 : pos_E_3(4)+3)); - Converts the string to number, from 'cos_effs_3' by indexing from the sum of 3rd value of 'pos_E_3' and 4, to the sum of 4th value of 'pos_E_3' and 3, and stores the value in 'a_l_7'
Preallocation of variables inside the loop
C_p_L = zeros(1,numel(T1)); - Creates a row vector of zeros with length 1 x (no. of elements in 'T1')
H_L = zeros(1,numel(T1)); - Creates a row vector of zeros with length 1 x (no. of elements in 'T1')
S_L = zeros(1,numel(T1)); - Creates a row vector of zeros with length 1 x (no. of elements in 'T1')
C_p_H = zeros(1,numel(T2)); - Creates a row vector of zeros with length 1 x (no. of elements in 'T2')
H_H = zeros(1,numel(T2)); - Creates a row vector of zeros with length 1 x (no. of elements in 'T2')
S_H = zeros(1,numel(T2)); - Creates a row vector of zeros with length 1 x (no. of elements in 'T2')
Calculating Specific heat, enthalpy and entropy for low temperature co-efficients
for i = 1 : numel(T1) - Runs the loop for index value 'i' that increments from 1 to 'numel(T1)' ( 'numel(T1)' gives the number of array elements in 'T1' )
C_p_L(i) = R * ( a_l_1 + (a_l_2 * T1(i)) + (a_l_3 * T1(i)^2) + (a_l_4 * T1(i)^3) + (a_l_5 * T1(i)^4)); - Calculates specific heat for each value of 'T1' indexed by 'i' and stores each in index position specified by 'i' in 'C_p_L'
H_L(i) = R * T1(i)*( a_l_1 + (a_l_2 * T1(i)/2) + (a_l_3 * (T1(i)^2)/3) + (a_l_4 * (T1(i)^3)/4) + (a_l_5 * (T1(i)^4)/5) + a_l_6/T1(i)); - Calculates enthalpy for each value of 'T1' indexed by 'i' and stores each in index position specified by 'i' in 'H_L'
S_L(i) = R * ( a_l_1 * log(T1(i)) + (a_l_2 * T1(i)) + (a_l_3 * (T1(i)^2)/2) + (a_l_4 * (T1(i)^3)/3) + (a_l_5 * (T1(i)^4)/4) + a_l_7); - Calculates entropy for each value of 'T1' indexed by 'i' and stores each in index position specified by 'i' in 'S_L'
end - Ends the loop
Calculating Specific heat, enthalpy and entropy for high temperature co-efficients
for j = 1 : numel(T2) - Runs the loop for index value 'j' that increments from 1 to 'numel(T2)' ( 'numel(T2)' gives the number of array elements in 'T2' )
C_p_H(j) = R * ( a_h_1 + (a_h_2 * T2(j)) + (a_h_3 * T2(j)^2) + (a_h_4 * T2(j)^3) + (a_h_5 * T2(j)^4)); - Calculates specific heat for each value of 'T2' indexed by 'j' and stores each in index position specified by 'j' in 'C_p_H'
H_H(j) = R * T2(j)*( a_h_1 + (a_h_2 * T2(j)/2) + (a_h_3 * (T2(j)^2)/3) + (a_h_4 * (T2(j)^3)/4) + (a_h_5 * (T2(j)^4)/5) + a_h_6/T2(j)); - Calculates enthalpy for each value of 'T2' indexed by 'j' and stores each in index position specified by 'j' in 'H_H'
S_H(j) = R * ( a_h_1 * log(T2(j)) + (a_h_2 * T2(j)) + (a_h_3 * (T2(j)^2)/2) + (a_h_4 * (T2(j)^3)/3) + (a_h_5 * (T2(j)^4)/4) + a_h_7); - Calculates entropy for each value of 'T2' indexed by 'j' and stores each in index position specified by 'j' in 'S_H'
end - Ends the loop
end - Ends the function
Function that is used to calculate the molecular weight of all species in the data file,
function ind_At_wt = Molcular_weight(Elements, At_wt, species_name) - This funtion 'Molecular_weight' takes 'Elements', 'At_wt' and 'species_name' as inputs from main program and returns the array 'ind_At_wt' to the main program
Intialization of the atomic weight for each individual atoms that make up the species
ind_At_wt = zeros(1,length(species_name)); - Creates a row vector 'ind_At_wt' with zero values for the length of 'species_name'
For the length of each species, the atomic weight of that individual element in the species is calculated and each value is stored as array in 'ind_At_wt' for the species
for i = 1 : length(species_name) - This loop runs for index value 'i' which increments from 1 to length of 'species_name'
for j = 1 : length(Elements) - This loop runs for index value 'j' which increments from 1 to length of 'Elements'
The condition statement runs for each value of 'i' and 'j' such that if the condition is true, it executes the statement inside it, and breaks out of the nested 'for' loop to return to the next iteration in the main 'for' loop
if strcmp(species_name(i),Elements(j)) == 1 - Compares the string 'i'th element of 'species_name with 'j'th element in 'Elements' and if it's true, then the following statement is executed
ind_At_wt(i) = At_wt(j);
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '2' - If the previous condition is not true and if the 'i'th element of 'species_name' is equal to '2', then the following is executed
ind_At_wt(i-1) = 2 * ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 2 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '3' - If the previous conditions aren't true and if the 'i'th element of 'species_name' is equal to '3', then the following is executed
ind_At_wt(i-1) = 3 * ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 3 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '4' - If the previous conditions aren't true and if the 'i'th element of 'species_name' is equal to '4', then the following is executed
ind_At_wt(i-1) = 4 * ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 4 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '5' - If the previous conditions aren't true and if the 'i'th element of 'species_name' is equal to '5', then the following is executed
ind_At_wt(i-1) = 5* ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 5 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '6' - If the previous conditions aren't true and if the 'i'th element of 'species_name' is equal to '6', then the following is executed
ind_At_wt(i-1) = 6 * ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 6 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '7' - If the previous conditions aren't true and if the 'i'th element of 'species_name' is equal to '7', then the following is executed
ind_At_wt(i-1) = 7 * ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 7 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '8' - If the previous conditions aren't true and if the 'i'th element of 'species_name' is equal to '8', then the following is executed
ind_At_wt(i-1) = 8 * ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 8 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
elseif species_name(i) == '9' - If the previous conditions aren't true and if the 'i'th element of 'species_name' is equal to '9', then the following is executed
ind_At_wt(i-1) = 9 * ind_At_wt(i-1); - The (i-1)th value of 'ind_At_wt' is multiplied by 9 and stored in the position (i-1)th of 'ind_At_wt'
break - Breaks this nested 'for' loop and returns to the next iteration in the main 'for' loop
end - Ends the nested 'for' loop
end - Ends the 'for' loop
end - Ends the function
ERROR OCCURRED WHILE PROGRAMMING :
- An error was occured in the command window after the execution of the 1st iteration of the 'for' loop stating that, to change the current folder directory.
- Then, it was corrected by using 'cd' command which changes the folder directory to the previous directory where the program was saved.

REFERENCE :