Project 1 - Parsing NASA thermodynamic data
Aim and its Objective:
Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data. Calculate the molecular weight of each species. Plot the Cp, Enthalpy, and Entropy for each species' local temperature range (low temperature: high temperature) specific. Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All these should be generated automatically. Plots for o2, N2, and Co2 species.
Theory:
Specific heat (cp):
The amount of energy required to increase a substance's temperature by one degree Celsius (°C) per gram is known as its specific heat (cp). Since water has a high specific heat capacity, which we'll just refer to as "heat capacity," it requires more energy to raise its temperature in comparison to other materials.

Entropy (S):
Entropy is the amount of thermal energy per unit of temperature in a system that cannot be used for productive work. Since work is produced by ordered molecular motion, entropy also serves as a proxy for a system's molecular disorder or unpredictability.

Enthalpy (H):
Enthalpy, a characteristic of thermodynamic systems, is calculated as the sum of the internal energy of the system and the product of its pressure and volume. It is an easy-to-use state function that is regularly employed in many measurements in chemical, biological, and physical systems under constant pressure.

Code:
clear all
close all
clc
f1 = fopen('THERMO.dat','r');
fgetl(f1);
G_temp = str2num(fgetl(f1));
%Global temparture
G_low = G_temp(1);
G_mid = G_temp(2);
G_high= G_temp(3);
for i = 1:3
fgetl(f1);
end
R = 8.3145; %J/Mol*Km (units)
for j = 1:53
line = fgetl(f1);
A = strsplit(line,' ');
% species
species =(A{1});
% Local_temparture
Low = str2num(A{end-3});
High = str2num(A{end-2});
Mid = str2num(A{end-1});
%co-efficients
a_line = fgetl(f1);
a = findstr(a_line,'E'); %#ok<*FSTR>
A1 = a_line(1:a(1)+3);
A1 = str2num(A1); %#ok<*ST2NM>
A2 = a_line(a(1)+4:a(2)+3);
A2 = str2num(A2);
A3 = a_line(a(2)+4:a(3)+3);
A3 = str2num(A3);
A4 = a_line(a(3)+4:a(4)+3);
A4 = str2num(A4);
A5 = a_line(a(4)+4:a(5)+3);
A5 = str2num(A5);
b_line = fgetl(f1);
b = findstr(b_line,'E');
A6 = b_line(1:b(1)+3);
A6 = str2num(A6);
A7 = b_line(b(1)+4:b(2)+3);
A7 = str2num(A7);
A8 = b_line(b(2)+4:b(3)+3);
A8 = str2num(A8);
A9 = b_line(b(3)+4:b(4)+3);
A9 = str2num(A9);
A10 = b_line(b(4)+4:b(5)+3);
A10 = str2num(A10);
c_line = fgetl(f1);
c = findstr(c_line,'E');
A11 = c_line(1:c(1)+3);
A11 = str2num(A11);
A12 = c_line(c(1)+4:c(2)+3);
A12 = str2num(A12);
A13 = c_line(c(2)+4:c(3)+3);
A13 = str2num(A13);
A14 = c_line(c(3)+4:c(4)+3);
A14 = str2num(A14);
%Specific heat, Enthalpy, Entropy @ High temparture
T_H = linspace(Low,Mid,100);
cp = (A1+(A2.*T_H)+(A3.*T_H.^2)+(A4.*T_H.^3)+(A5.*T_H.^4))*R;
H = (A1+(A2.*T_H/2)+(A3.*(T_H.^2)/3)+(A4.*(T_H.^3)/4)+(A5.*(T_H.^4)/5)+A6.*T_H)*R;
S = (A1*log(T_H)+(A2.*T_H)+(A3.*(T_H.^2)/2)+(A4.*(T_H.^3)/3)+(A5.*(T_H.^4)/4)+A7)*R;
%Specific heat, Enthalpy, Entropy @ Low temparture
T_L = linspace(Mid,High,100);
cp1 = R*(A8+(A9.*T_L)+(A10.*T_L.^2)+(A11.*T_L.^3)+(A12.*T_L.^4));
H1 = R*(A8+(A9.*T_L/2)+(A10.*(T_L.^2)/3)+(A11.*(T_L.^3)/4)+(A12.*(T_L.^4)/5)+A13.*T_L);
S1 = R*(A8*log(T_L)+(A9.*T_L)+(A10.*(T_L.^2)/2)+(A11.*(T_L.^3)/3)+(A12.*(T_L.^4)/4)+A14);
% To create a folder
str = sprintf('D:\\virat\\Nasa thermodynamics data\\%s',species);
mkdir(str);
cd(str);
figure()
plot(T_H,cp)
hold on
plot(T_L,cp1)
hold off
xlabel('Temparature')
ylabel('Specific heat')
legend('High temparture','low Temparature')
saveas (gcf,'Specific heat')
figure()
plot(T_H,H)
hold on
plot(T_L,H1)
hold off
xlabel('Temparature')
ylabel('Enthalpy')
legend('High temparture','low Temparature')
saveas(gcf,'Enthalpy')
figure()
plot(T_H,S)
hold on
plot(T_L,S1)
hold off
xlabel('Temparature')
ylabel('Entropy')
legend('High temparture','low Temparature')
saveas(gcf,'Entropy')
cd ..
%Calculating Molecular weight
elements = ['H','C','N','O','AR','(S)'];
Atomic_weight = [1,12,14,16,40,0,0,0,0];
molecular_wt = 0;
for k = 1:length(species)
[num,status] = str2num(species(k));
if status == 0
ind = find(elements == species(k));
molecular_wt(k) = Atomic_weight(ind);
else
molecular_wt(k-1) = num*molecular_wt(k-1);
end
end
t_M=sum(molecular_wt);
K = sprintf('Molecular weight of %s is %f',species,t_M);
disp(K)
end
Code explanation:
- Starting with clear all, close all and clc
- By using fopen command we are opening the file of THERMO.dat and 'r' repersents to read the file
fgetl: is used to read the line in the file of which we have given and for every fgetl it will be reading the next line in the file So the fgetl is used to read the line in the file
- By using fgetl we will be reading the first line of the file which is THERMO and for next fgetl we ill be getting the second line which is the Global_temperature and we are assinging to the G_temp
- G_low = G_temp(1)
G_mid = G_temp(2)
G_high= G_temp(3) - And we are skipping the next three lines by creating the for loop because those are the comments
- And then in the 4TH line we have species name in the first position and then the local_temperature and we are assinging this line to the line
- By using the strsplit we are splitting the line with the reference of the 'space'
-
strsplit : Split string or character vector at specified delimiter
Delimiting characters, specified as a character vector, a
1-by-ncell array of character vectors, or a1-by-nstring array. Text specified indelimiterdoes not appear in the output c.Specify multiple delimiters in a cell array or a string array. The
strsplitfunction splits stron the elements ofdelimiter. The order in which delimiters appear indelimiterdoes not matter unless multiple delimiters begin a match at the same character instr. In that casestrsplitsplits on the first matching delimiter.- Strsplit is used to split the line and it is assigned to the variable A so the A(1) is the species name and for the local_temp we ill be taking from the end
- Low = str2num(A{end-3})
High = str2num(A{end-2})
Mid = str2num(A{end-1})
str2num: Convert character array or string to numeric array
- And then we have to read the co-efficients which are from the next line and then by using fgetl read the next line and assigned to a variable
- To assign each value to a variable we used findstr
findstr: Find strings within other strings
- We ill be finding the E in the string (line) and by refering that we ill be taking the separate value and assign to a variable as shown
-
a_line = fgetl(f1);
a = findstr(a_line,'E')A1 = a_line(1:a(1)+3)
- Here a will be getting the position of the E in the line and by using the position and number of numbers to left and right we ill be getting the co_efficiants and for the second number we have to start from the first number position as shown
A2 = a_line(a(1)+4:a(2)+3)
- lly to all co_efficiants as shown
a_line = fgetl(f1);
a = findstr(a_line,'E'); %#ok<*FSTR>
A1 = a_line(1:a(1)+3);
A1 = str2num(A1); %#ok<*ST2NM>
A2 = a_line(a(1)+4:a(2)+3);
A2 = str2num(A2);
A3 = a_line(a(2)+4:a(3)+3);
A3 = str2num(A3);
A4 = a_line(a(3)+4:a(4)+3);
A4 = str2num(A4);
A5 = a_line(a(4)+4:a(5)+3);
A5 = str2num(A5);
b_line = fgetl(f1);
b = findstr(b_line,'E');
A6 = b_line(1:b(1)+3);
A6 = str2num(A6);
A7 = b_line(b(1)+4:b(2)+3);
A7 = str2num(A7);
A8 = b_line(b(2)+4:b(3)+3);
A8 = str2num(A8);
A9 = b_line(b(3)+4:b(4)+3);
A9 = str2num(A9);
A10 = b_line(b(4)+4:b(5)+3);
A10 = str2num(A10);
c_line = fgetl(f1);
c = findstr(c_line,'E');
A11 = c_line(1:c(1)+3);
A11 = str2num(A11);
A12 = c_line(c(1)+4:c(2)+3);
A12 = str2num(A12);
A13 = c_line(c(2)+4:c(3)+3);
A13 = str2num(A13);
A14 = c_line(c(3)+4:c(4)+3);
A14 = str2num(A14);
- We have to write a formula for low and high temperatures The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
%Specific heat, Enthalpy, Entropy @ High temparture
T_H = linspace(Low,mid,100);
cp = (A1+(A2.*T_H)+(A3.*T_H.^2)+(A4.*T_H.^3)+(A5.*T_H.^4))*R;
H = (A1+(A2.*T_H/2)+(A3.*(T_H.^2)/3)+(A4.*(T_H.^3)/4)+(A5.*(T_H.^4)/5)+A6.*T_H)*R;
S = (A1*log(T_H)+(A2.*T_H)+(A3.*(T_H.^2)/2)+(A4.*(T_H.^3)/3)+(A5.*(T_H.^4)/4)+A7)*R;
%Specific heat, Enthalpy, Entropy @ Low temparture
T_L = linspace(mid,High,100);
cp1 = R*(A8+(A9.*T_L)+(A10.*T_L.^2)+(A11.*T_L.^3)+(A12.*T_L.^4));
H1 = R*(A8+(A9.*T_L/2)+(A10.*(T_L.^2)/3)+(A11.*(T_L.^3)/4)+(A12.*(T_L.^4)/5)+A13.*T_L);
S1 = R*(A8*log(T_L)+(A9.*T_L)+(A10.*(T_L.^2)/2)+(A11.*(T_L.^3)/3)+(A12.*(T_L.^4)/4)+A14);
- And then we have to create a folder to save the plots in thier respective folders to do that we will be using the mkdir
mkdir: To create a folder
cd: Change current folder
- str = sprintf('D:viratNasa thermodynamics data%s',Spices);
mkdir(str);
cd(str); - plotting the enthalpy, entropy and specific heats for the low and high temperatures co_efficiants
figure()
plot(T_H,cp)
hold on
plot(T_L,cp1)
hold off
xlabel('Temparature')
ylabel('Specific heat')
legend('High temparture','low Temparature')
saveas (gcf,'Specific heat')
figure()
plot(T_H,H)
hold on
plot(T_L,H1)
hold off
xlabel('Temparature')
ylabel('Enthalpy')
legend('High temparture','low Temparature')
saveas(gcf,'Enthalpy')
figure()
plot(T_H,S)
hold on
plot(T_L,S1)
hold off
xlabel('Temparature')
ylabel('Entropy')
legend('High temparture','low Temparature')
saveas(gcf,'Entropy')
- after every plot saveas will save the plot as fig in the respective folder
saveas: Save figure to specific file format gcf is the handle to save the plot as image
- And the we have to give the cd.. command to come out from the particular folder
cd.. : Change current folder
- Calculating the molecular weight
%Calculating Molecular weight
elements = ['H','C','N','O','AR','(S)'];
Atomic_weight = [1,12,14,16,40,0,0,0,0];
for k = 1:length(species)
[num,status] = str2num(species(k));
if status == 0
ind = find(elements == species(k));
molecular_wt(k) = Atomic_weight(ind);
else
molecular_wt(k-1) = num*molecular_wt(k-1);
end
end
t_M=sum(molecular_wt)
K = sprintf('Molecular weight of %s is %f',species,t_M);
disp(K)
- Create two strings of atom symbol and the atomic weight as shown
- elements = ['H','C','N','O','AR','(S)'];
Atomic_weight = [1,12,14,16,40,0,0,0,0]; - Create a for loop to the length of the species convert the species to number by using str2num command with 2 results
- In species if it is number status will be 1 it is true if it is an letter(element) the status will be 0
- By applying if condition status == 0
- ind = find(elements == species(k)) we are finding the elements in species in the elements array by using find command
- Then the molecular_wt = atomic_weight(ind) else
- molecular_wt(k-1) is it will take the previous position of the species equals to number in the species multiply by the molecular_wt(k-1) so that the previous position to the number in the species will be changed
- And then display the molecular_wt
Results and outputs:
- Molecular weight of each species is displayed in the command window.

- Plots for o2, N2 , and Co2 species
PLOTS OF O2
- Entropy

- Specific heat

- Enthalpy

PLOTS OF CO2
- Enthalpy

- Entropy

- Specific heat
