Project 1 - Parsing NASA thermodynamic data
%clear all the previous data or stores values
clear all
close all
clc
%Open the the THERMO.dat file f1-fopen('THERMO.dat', 'r')
%skip the first line of the file
fgetl(f1);
%reading the tempersture values distingushed as local, average and high temperatures
line=fgetl(f1);
a=strsplit(line,' ');
global_low_temp=str2num (a{2});
global_average_temp-str2num(a{3}); global_high_temp-str2num (a{4});
fgetl(f1);
fgetl(f1);
fgetl(f1);
%extracting data for each gas
for k=1:53
%step7
line=fgetl(f1);
a=strsplit(line,' ');
Gas=a(1);
b=findstr(line, '.');
local_low_temp=str2num (line (b(1)-4:b(1)+4)); local_high_temp=str2num (line (b(2)-4: b (2)+4));
local_average_temp=str2num(line (b(3)-4:b(3)+4));
%extracting all 14 coefficient
ct=1;
for j=1:3
line-fgetl(f1);
b=strfind(line, 'E');
for i=1:length(b)
if ct<=7
Ha(ct)=str2num (line (b(i)-11: b(i)+3));
ct=ct+1;
else
La(ct-7)=str2num (line (b(i)-11: b(i)+3));
ct=ct+1;
end
end
end
%initializing specific heat, entropy and enthalpy values to zero
cp=0;
H=0;
S=0;
%calling the function where cp, H and S are calculated
[Cp,H,S] = coefficient (local_low_temp, local_average_temp, local_high_temp, La,Ha);
%molecular weight calculating function M=Weight (Gas)
%plotting
GAS=char(Gas);
T=local_low_temp:local_high_temp;
current=pwd;
mkdir (GAS);
cd (GAS)
figure(1);
plot(T, Cp, 'color', 'g', 'linewidth', 2);
egend (Gas);
txt1=['Molecular weight: num2str(M)]; text (500, max(Cp)*0.98, txt1, 'color', 'r');
grid on
xlabel('temperature (K)');
ylabel('Specific heat (KJ/mol)');
title('specific heat vs temperrature');
figure(2);
plot (T,H, 'color', 'r', 'linewidth',2);
legend (Gas);
txt1=['molecular weight: num2str(M)]; text (500, max (H)*0.98, txt1, 'color', 'b');
grid on
xlabel('temperrature (K)');
ylabel('enthalpy (kj/mol)'); title('enthalpy vs temperature');
figure(3);
plot (T,S, 'color', 'b', 'linewidth',2);
legend (Gas);
txt1=['Molecular weight: num2str(M)]; text (500, max (S)*0.98, txt1, 'color', 'b');
grid on
xlabel('temperature(K)');
ylabel('entropy (KJ/mol)'); title('entropy vs temperature');
saveas (figure(1), 'Cp.jpg');
saveas (figure(2), 'H.jpg'); saveas (figure(3), 'S.jpg');
cd (current);
end
%function to calculate molecular weight function Mo-Weight (Gas)
Weight [1 12 14 16 32 0 40]; Gas1=['H' 'C' 'N' 'O' 'S' 'A' 'R'];
number=[1 2 3 4];
M=0;
GAS=char(Gas);
for i=1:length(GAS)
for j=1:length(Gas1)
if GAS (i)==Gas1(j)
M(i) Weight (j);
end
for j=1:length(number)
if str2double (GAS (i)) ==number(j) M(i)=M(i-1)* (number(j)-1);
end
end
end end
Mo=sum(M);
end
%function to calculate the Cp, H, S.
function [Cp,H,S]=coefficient (local_low_temp, local_average_temp, local_high_temp, La, Ha) for i=1:local_high_temp-local_low_temp+1
R=8.34;
T-i+local_low_temp-1;
%for lower temperature values
if T<= local_average_temp
Cp(i)=R* (La(1)+La (2) *T+La(3) *T^2+La (4) *T^3+La(5)*T^4);
H(i)=R*T* (La (1)+(La (2) T)/2+(La (3) *T^2)/3+ (La (4) *T^3)/4+ (La (5) *T^4)/5+La(6)/T); S(i)=R (La (1) *log (T)+(La (2) *T)+((La (3) *T^2)/2)+((La (4) *T^3)/3) + ((La (5) *T^4)/4)+(La(7)));
%for higher temperature values
else
Cp(i)=R* (Ha (1) +Ha (2) T+Ha (3) *T^2+Ha (4) *T^3+Ha(5)*T^4); H(i)=R*T(Ha(1)+((Ha (2) T)/2)+(Ha (3) *T^2)/3+ (Ha (4) *T^3)/4+ (Ha (5) *T^4)/5+(Ha(6)/T)); S(i)=R (Ha (1) *log(T)+(Ha (2) *T)+(Ha (3) *T^2)/2+(Ha (4) *T^3)/3+ (Ha(5)*T^4)/4+ (Ha (7)));
end
end
end
The fist step in the code clears all the previously stored data on the command window.
The 'fopen' command opens the 'THERMO.dat' file and 'r' here indicated the 'read only' command. We use 'fget!' command to read the data file line by line. The first line of the file is omitted and the next line is stores in a variable 'line' and the line is split using 'strsplit' command and stored in a variable 'a'. Different temperature values are assigned to read the 53 gases mentioned in the data file we use for loop then we further read the lines in the file using fgetl command.
When all the global and local temperatures are read we call functions which are written to calculate molecular weight, specific heat(Cp),enthalpy(H),Entropy(s).
Calculating the value of Cp,H & S for local temperature range. Initial values of Cp, H & S are defined as zero, Plotting the graphs & saving them in the folder. It is standard procedure to store in file in specific location, by using many commands (pwd, mkdir, cd, saveas) written in this program.
We are using formulae to calculate Cp, H & S. We are using for loop to calculate each value at local temperature range. We are also using if & else command to use low & high temperature coefficient as per average temperature. If Temperature is less than average temperature then use low temperature co-efficient & If Temperature is more than average temperature then use hign temperature co-efficient
Molecular weight is calculated in a different function which is later called in the main function.




