Project 1 - Parsing NASA thermodynamic data
AIM : To calculate the molecular weight and plot the graphs of specific heat(cp), enthalpy(H), entropy(S) for the local temperature range (low temperature : high temperature) specific for each species.
Theory:
Specific Heat(Cp): The specific heat capacity (symbol cp) of a substance is the heat capacity of a sample of the substance divided by the mass of the sample. It is the amount of energy that must be added, in the form of heat, to one unit of mass of the substance in order to cause an increase of one unit in temperature. The SI unit of specific heat is joule per kelvin and kilogram, J/(K kg).
For example, at a temperature of 25 °C (the specific heat capacity can vary with the temperature), the heat required to raise the temperature of 1 kg of water by 1 K (equivalent to 1 °C) is 4179.6 joules, meaning that the specific heat capacity of water is 4179.6 J·kg−1·K−1.
Enthalpy:
Enthalpy is defined as the sum of the system's internal energy and the product of its pressure and volume.It is a convenient state function standardly used in many measurements in chemical, biological, and physical systems at a constant pressure. The pressure-volume term expresses the work required to establish the system's physical dimensions, i.e. to make room for it by displacing its surroundings. As a state function, enthalpy depends only on the final configuration of internal energy, pressure, and volume, not on the path taken to achieve it.The unit of measurement for enthalpy in the International System of Units (SI) is the joule.
Entropy:
Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.The unit of measurement for enthalpy in the International System of Units (SI) is the joule per molecule (J/mol).
Objective :
-
Calculate the molecular weight of each species and display it in the command window.
-
Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
-
Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
-
The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
Equation :

Cp = Specific Heat. (j/mol*k).
H = Enthalpy. (j/mol).
S = Entropy. (j/mol*k).
R = Universal Gas Constant(8.314).
Function code for Specific heat, Enthalpy,Entropy :
Specific heat:
function [c_p] = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,locallow_temp,localmid_temp,localhigh_temp,T,R);
for i = 1:length(T);
if (T(i)>localmid_temp);
c_p(i)=(R*((a1)+(a2*T(i))+(a3*T(i).^2)+(a4*T(i).^3)+(a5*T(i).^4)));
else
c_p(i)=(R*((b3)+(b4*T(i))+(b5*T(i).^2)+(c1*T(i).^3)+(c2*T(i).^4)));
end
end
Explanation of code:
- The variable c_p is defined as the specific heat and all the 14 coefficients (a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4) of a particular species is taken into account along with the local low temperature, local medium temperature and local high temperature and the universal gas constant(R).
- Now, a for loop is created with a variable 'i' which calculates the entire length of temperature and considers 100 values.
- Then, a ('if' and 'else') condition is applied where if temperature along the length of 'i' is greater than the local medium temperature(1000.0) then it will consider the first seven coefficients (a1,a2,a3,a4,a5,b1,b2)or else it will consider last seven coefficients(b3,b4,b5,c1,c2,c3,c4).
- The formula for specific heat is coded with the given formulae.
- The for loop is ended.
Enthalpy :
function [H] = enthalpy(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,locallow_temp,localmid_temp,localhigh_temp,T,R);
for i = 1:length(T);
if T(i)>localmid_temp;
H(i) = (a1+(a2*T(i))/2+(a3*T(i).^2)/3+(a4*T(i).^3)/4+(a5*T(i).^4)/5+(b1/T(i)))*R*T(i);
else
H(i) = (b3+(b4*T(i))/2+(b5*T(i).^2)/3+(c1*T(i).^3)/4+(c2*T(i).^4)/5+(c3/T(i)))*R*T(i);
end
end
Explanation of code:
- The variable 'H' is defined as the specific heat and all the 14 coefficients (a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4) of a particular species is taken into consideration along with the local low temperature, local medium temperature and local high temperature and the universal gas constant(R).
- Now, a for loop is created with a variable 'i' which calculates the entire length of temperature and considers 100 values.
- Then, a ('if' and 'else') condition is applied where if temperature along the length of 'i' is greater than the local mid temperature(1000.0) then it will consider the first seven coefficients (a1,a2,a3,a4,a5,b1,b2)or else it will consider last seven coefficients(b3,b4,b5,c1,c2,c3,c4).
- The formula for enthalpy is coded with the given formulae.
- The for loop is ended.
Entropy :
function [S] = entropy(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,locallow_temp,localmid_temp,localhigh_temp,T,R);
for i = 1:length(T);
if(T(i)>localmid_temp);
S(i) = (a1*log(T(i))+(a2*T(i)+(a3*T(i).^2)/2+(a4*T(i).^3)/3+(a5*T(i).^4)/4+ b2))*R;
else
S(i) = (b3*log(T(i))+(b4*T(i)+(b5*T(i).^2)/2+(c1*T(i).^3)/3+(c2*T(i).^4)/4+ c4))*R;
end
end
Explanation of code:
- A variable 'S' is considered for the function of entropy, where all the forteen coefficients are defined along with local low temperature,local medium temperature and local high temperature, and the temperature(T) array and the universal gas constant(R).
- Now a for loop is created with a variable 'i' which calculates the entire length of 'T' for 100 values.
- Then, a ('if' and 'else') condition is applied where if temperature along the length of 'i' is greater than the local medium temperature then it will consider the first seven coefficients(a1,a2,a3,a4,a5,b1,b2) or else it will consider last seven coefficients(b3,b4,b5,c1,c2,c3,c4) .
- The formula for Entropy is coded with the given formulae.
- The for loop is ended.
Molecular weight :
function [m] = molecular_weight(species_name);
elements = ['H','O','C','N','A'];
atomic_mass = [1,16,12,14,18];
m = 0;
for i = 1:length(species_name);
for j = 1:length(elements);
if strcmpi(species_name(i),elements(j));
m = m + (atomic_mass(j));
z = j;
end
end
n = str2double(species_name(i));
if n>1
m = m + (atomic_mass(z)*(n-1));
end
end
Explanation of code:
- The function 'm' is coded for the calculation of molecular weight for the each species in the data sheet.
- The elements considered are ['H,O,C,N,A'].
- And the atomic mass are [1,16,12,14,18].
- The value for m is taken as 0.
- A for loop is created with a variable 'i' for the entire length of all the species.
- And a variable 'j' is created to read the entire length of all the elements.
- The 'strcmpi' compares 'species_name(i)' and 'elements(j)', ignoring any differences in letter case.
- The function returns
1(true) if the two are identical and0(false) otherwise. - Text is considered identical if the size and content of each are the same, aside from case.
- m = m + (atomic_mass(j)) is the formula for the molecular_weight calculation for the all iteration of length 'j'.
- n = str2double(species_name(i))
- 'str2double' converts the text to double precision values. str contains text that represents real or complex numeric values.
- str can be a character vector, a cell array of character vectors, or a string array.
- If the variable is n >1
- Then it will run the iteration more times and perform the maths of (n-1)until it gets all the values of the elements.
- Gradually it gives all the values of the molecular weight of the species respectively.
- with the formula m = m + (atomic_mass(x)*(n-1)).
MAIN PROGRAM:
clear all
close all
clc
f1 = fopen('THERMO.dat','r');
filename = fgetl(f1); %to read line from file, removing newline characters
%global temperatures values
global_temp_line = fgetl(f1);
A1 = strsplit(global_temp_line,' '); %splits str at the delimiters specified by delimiter.
global_low_temp = str2num(A1{2}); %converts a character array or string scalar to a numeric matrix.
global_med_temp = str2num(A1{3});
global_high_temp =str2num(A1{4});
%read comments in the thermodynamic data
comment_1 = fgetl(f1);
comment_2 = fgetl(f1);
comment_3 = fgetl(f1);
for i = 1:53;
tline = fgetl(f1);
O = strsplit(tline,' ');
species_name = O{1};
z = length(O);
locallow_temp = str2double(O{z-3});
localhigh_temp = str2double(O{z-2});
localmid_temp = str2double(O{z-1});
tline1 = fgetl(f1);
a = findstr(tline1,'E');
%first coefficient
a1 = tline1(1:a(1)+3);
a1 = str2num(a1);
%second coefficient
a2 = tline1(a(1)+4:a(2)+3);
a2 = str2num(a2);
%third coefficient
a3 = tline1(a(2)+4:a(3)+3);
a3 = str2num(a3);
%forth coefficient
a4 = tline1(a(3)+4:a(4)+3);
a4 = str2num(a4);
%fifth coefficient
a5 = tline1(a(4)+5:a(5)+3);
a5 = str2num(a5);
tline2 = fgetl(f1);
b = findstr(tline2,'E');
%sixth coefficient
b1 = tline2(1:b+3);
b1 = str2num(b1);
%seventh coefficient
b2 = tline2(b(1)+4:b(2)+3);
b2 = str2num(b2);
%eighth coefficient
b3 = tline2(b(2)+4:b(3)+3);
b3 = str2num(b3);
%ninth coefficient
b4 = tline2(b(3)+4:b(4)+3);
b4 = str2num(b4);
%tenth coefficient
b5 = tline2(b(4)+4:b(5)+3);
b5 = str2num(b5);
tline3 = fgetl(f1);
c = findstr(tline3,'E');
%eleventh coefficient
c1 = tline3(1:c(1)+3);
c1 = str2num(c1);
%twelvth coefficient
c2 = tline3(c(1)+4:c(2)+3);
c2 = str2num(c2);
%thirteenth coefficient
c3 = tline3(c(2)+4:c(3)+3);
c3 = str2num(c3);
%forteenth coefficient
c4 = tline3(c(3)+4:c(4)+4);
c4 = str2num(c4);
%universal gas constant
R = 8.314;
T = linspace(locallow_temp,localhigh_temp,100);
% creating folders
mkdir(['C:UsersKUKKALA SAGARDocumentsMATLABNASAzz',species_name]);
cd(['C:UsersKUKKALA SAGARDocumentsMATLABNASAzz',species_name]);
%To calculate the specific_heat of the data
[c_p] = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,locallow_temp,localmid_temp,localhigh_temp,T,R);
%To calculate the enthalpy of data
[H] = enthalpy(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,locallow_temp,localmid_temp,localhigh_temp,T,R);
%To calculate the entropy of data
[S] = entropy(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,locallow_temp,localmid_temp,localhigh_temp,T,R);
%Plotting for specific heat vs temperature
figure(1);
plot(T,c_p);
grid on
data1 = strcat(species_name,'Specific Heat vs Temperature');
xlabel("Temperature (K)");
ylabel("Specific Heat(J/mol*K)");
title(strcat('Temperature vs Specific heat of---',species_name));
f = fullfile(cd);
cd(f);
saveas(gcf,[pwd '/Specific Heat vs Temperature.jpg']);
cd(f);
%plotting for enthalpy vs temprature
figure(2);
plot(T,H);
grid on
data1 = strcat(species_name,'Enthalpy vs Temperature');
xlabel("Temperature (K)");
ylabel("Enthalpy(J/mol*K)");
title(strcat('Temperature vs Enthalpy of---',species_name));
f = fullfile(cd);
cd(f);
saveas(gcf,[pwd '/Enthalpy vs temperature.jpg']);
cd(f);
%plotting for temperature vs entropy
figure(3);
plot(T,S);
grid on
data1 = strcat(species_name,'Entropy vs Temperature');
xlabel("Temperature (K)");
ylabel("Entropy(J/mol)");
title(strcat('Temperature vs Entropy of---',species_name));
f = fullfile(cd);
cd(f);
saveas(gcf,[pwd '/Entropy vs Temperature.jpg']);
cd(f);
%Calculating the Molecular weight
m = molecular_weight(species_name);
fprintf('%s mol = %f n',species_name, m);
title(species_name);
end
Explanation of the code:
- clear all - clear all command is used to clear all the running functions in the workspace.
- close all - close all command is used to close all the plots and graphs running at the background.
- clc - clc command is used to clear the workspace.
- fopen('THERMO.dat'") opens the file,'THERMO.dat', for binary read access, and returns an integer file identifier equal to or greater than 3.
- The command 'r' is used to read the data file.
- Now,we call the 'fgetl' command for further line and assigning it as global temperature line.
- The 'splitstr' command is used to split the global temperature line with the delimiters specified by delimiter and assigning as A1 .
- The global low temperature, global medium temperature, global high temperature are assigned and converting them string to number with 'str2num' command.
- The comments are called with fgetl command.
- We created the for loop for a variable 'i' for an entire length of 1:53 species.
- 'tline = fgetl(f1)' considers the next line every time from the data text and reads the text.
- species_name is mentioned for the first species 'o'.
- A variable (o) is considered for the entire species of element 'o'.
- The local low temperature is coded with the command 'str2double'.
- {z-3} command is used to calculate the perfect position of local low temperature.
- 'str2double' converts the text to double precision values. str contains text that represents real or complex numeric values.
- str can be a character vector, a cell array of character vectors, or a string array.
- The local high temperature is defined with a positon of {z-2}, for getting the temperature value.
- The local high temperature is defined in the same way with 'str2double' command with the position of {z-1}
- 'tline1 = fget1(f1)' command is used to take us to the consecutive line and extract the data.
- 'a = findstr(tline1,'E')' findstr command is used to find the letter 'E' from the tline1 and 'E' is used as the base point for the location of every coefficients in the species.
- 'a1 = tline1(1:a(1)+3)'
- First coefficient is read using the command tline.
- From 1 to the length of the coefficient till the letter E and adding a more 3 characters to get the value of whole coefficient.
- 'a1 = str2num(a1).'string2num' converts a character array or string scalar to a numeric matrix.
- The input can include spaces, commas, and semicolons to indicate separate elements.
- If str2num cannot parse the input as numeric values, then it returns an empty matrix.
- Hence we repeat the step for a consecutive 14 steps.And with each iteration adding the value to the earlier iteration. Hence, we get all the coefficient.
- We plot figure(1) to have the temperature vs enthalpy graph.
- 'strcat' horizontally concatenates each value
. Each input argument can be a character array, a cell array of character vectors, or a string array. - xlabel 'Temperature' is defined.ylabel 'Enthalpy' is defined.
- 'fullfile' builds a full file specification from the specified folder and file names.
- 'fullfile' inserts platform-dependent file separators where necessary.
- 'saveas(enthalpy vs temperature)' saves the figure block diagram specified by figure to filename.
- Specify the file name as a character vector or string that includes a file extension.
- The file extension defines the file format. If we do not specify an extension, then saveas saves the figure to a FIG-file. To save the current figure, we need to specify
fig as gcf. - We plot figure(2) to have the temperature vs entropy graph.
- 'strcat' horizontally concatenates each value
. Each input argument can be a character array, a cell array of character vectors, or a string array. - xlabel 'Temperature' is defined.ylabel 'Entropy' is defined.
- 'fullfile' builds a full file specification from the specified folder and file names.
- 'fullfile' inserts platform-dependent file separators where necessary.
- 'saveas(entropy vs temperature)' saves the figure block diagram specified by figure to filename.
- Specify the file name as a character vector or string that includes a file extension.
- The file extension defines the file format. If we do not specify an extension, then saveas saves the figure to a FIG-file.
- To save the current figure, we need to specify
fig as gcf. - The molecular weight is defined by the variable 'm' for the entire species of the data.
- fprintf applies the formatspec to all elements of arrays in column order, and writes the data to a text file.
- fprintf uses the encoding scheme specified in the call to fopen
.Each species name is given a title.
Molecular values :

Plots :
1.plot for "CO2" :-
Temperature vs Specific heat

Temperature vs Enthalpy

Temperature vs Entropy

2.plot for ''O2" :-
Temperature Vs specific heat

Temperature vs Enthalpy

Temperature vs Entropy

Plots for "N2" :-
Temperature vs specific heat

Temperature vs Enthalpy

Temperature vs Entropy

Output saved for all the species in a folder (File parsing) :-

ERRORS :

- VARIABLE error :
The variable ''UNIVERSAL GAS CONSTANT (R)'' is not entered in the program.
we have to mention the R value, the above error is corrected by mentioning the R value.

- Enthalpy error :
I just forgot to mention the paranthesis symbola and the array for in the Enthalpy function.
The error shown above is corrected by placing the paranthesis close symbol and array we have to mention because it has draw for all th species.
Conclusion:
Understand the Plot between the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species and how to save them.