Project 1 - Parsing NASA thermodynamic data
% MATLAB CODE FOR THE FILE PARSING DATA FROM THE NASA THERMO.DAT FILE
clear all
close all
clc
f1 = fopen('THERMO.dat','r');
for i = 1:5
fgetl(f1);
end
R = 8.314;
for i = 1:53
tline = fgetl(f1);
tline = strsplit(tline,' ');
species = tline{1};
local_low_temp = str2num(tline{end-3});
local_mid_temp = str2num(tline{end-1});
local_high_temp = str2num(tline{end-2});
T = linspace(local_low_temp,local_high_temp,500);
tline1 = fgetl(f1);
pos1 = findstr(tline1,'E');
a1 = tline1(1:pos1(1)+3);
a1 = str2num(a1);
a2 = tline1(pos1(1)+4:pos1(2)+3);
a2 = str2num(a2);;
a3 = tline1(pos1(2)+4:pos1(3)+3);
a3 = str2num(a3);
a4 = tline1(pos1(3)+4:pos1(4)+3);
a4 = str2num(a4);
a5 = tline1(pos1(4)+4:pos1(5)+3);
a5 = str2num(a5);
tline2 = fgetl(f1);
pos1 = findstr(tline2,'E');
a6 = tline2(1:pos1(1)+3);
a6 = str2num(a6);
a7 = tline2(pos1(1)+4:pos1(2)+3);
a7 = str2num(a7);
a8 = tline2(pos1(2)+4:pos1(3)+3);
a8 = str2num(a8);
a9 = tline2(pos1(3)+4:pos1(4)+3);
a9 = str2num(a9);
a10 = tline2(pos1(4)+4:pos1(5)+3);
a10 = str2num(a10);
tline3 = fgetl(f1);
pos1 = findstr(tline3,'E');
a11 = tline3(1:pos1(1)+3);
a11 = str2num(a11);
a12 = tline3(pos1(1)+4:pos1(2)+3);
a12 = str2num(a12);
a13 = tline3(pos1(2)+4:pos1(3)+3);
a13 = str2num(a13);
a14 = tline3(pos1(3)+4:pos1(4)+3);
a14 = str2num(a14);
% cp/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4
%H/RT = a1 + a2 T / 2 + a3 T^2 / 3 + a4 T^3 / 4 + a5 T^4 / 5 + a6/T
% S/R = a1 lnT + a2 T + a3 T^2 / 2 + a4 T^3 / 3 + a5 T^4 / 4 + a7
cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temp);
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temp);
S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,local_mid_temp);
% plotting for all the species
figure(1)
plot(T,cp,'LineWidth',2)
xlabel('Temperature(k)')
ylabel('specific heat(cp)')
title('cp value vs temperature')
figure(2)
plot(T,H,'LineWidth',2)
xlabel('Temperature(k)')
ylabel('Enthalpy(H)')
title('Enthalpy vs temperature')
figure(3)
plot(T,S,'LineWidth',2)
xlabel('Temperature(k)');
ylabel('Entropy(S)');
title('Entropy vs temperature')
mkdir(species)
cd (species)
saveas(1,'specific heat vs temperature','jpg')
saveas(2,'enthalpy vs temperature','jpg')
saveas(3,'entropy vs temperature','jpg'),
cd ..
mol_wt = 0;
x = length(species);
atomic_names = ['H','O','C','N','A'];
at_wt = [1,16,12,14,40];
for j = 1:x
for k = 1:length(atomic_names)
if(strcmp(species(j),atomic_names(k)))
mol_wt = mol_wt+at_wt(k);
position = k;
end
end
n = str2num(species(j));
if n>1
mol_wt = mol_wt+at_wt(position)*(n-1);
end
end
sprintf('%s species %s %f',species,'molecular weight',mol_wt)
end
% writing function for specific heat
function [cp]=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temp)
if T<=local_mid_temp
cp = (a8+a9*T+a10*T.^2+a11*T.^3+a12*T.^4)*R;
else
cp = (a1+a2*T+a3*T.^2+a4*T.^3+a5*T.^4)*R;
end
end
% writing a code for enthalpy calculation
function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temp)
if T<=local_mid_temp
H =(a8 + (a9*T/2)+(a10*T.^2/3)+(a11*T.^3/4)+(a12*T.^4/5)+(a13./T))*R.*T;
else
H =(a1 + (a2*T/2)+(a3*T.^2/3)+(a4*T.^3/4)+(a5*T.^4/5)+(a6./T))*R.*T;
end
end
% writing a function for entropy calculation
function [S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,local_mid_temp)
if T<= local_mid_temp
S = ((a8*log(T))+(a9*T)+(a10*T.^2/2)+(a11*T.^3/3)+(a12*T.^4/4)+a14)*R;
else
S = ((a1*log(T))+(a2*T)+(a3*T.^2/2)+(a4*T.^3/3)+(a5*T.^4/4)+a7)*R;
end
end
CODE EXPLANATION
1) By using syntax of f1 = fopen('THERMO.dat','r') we can read the file where r is reading mode and f1 is the pointerto the file which we can read the data to the file.f1 gives some value but should not be -ve.
2) coming to fgetl command it is used to read the data linebyline.
syntax is tline = fgetl(f1) where tline will store the content line by line
3) then we need to split the line to extract data, so we use strsplit command.
tline = strsplit(f1,' ')where ' ' is the delimiter
4) Then for converting string to number we use str2num command
local_low_temp = str2num(tline{end-3})
here we use {} because it is a cell array,cell array means which contains numbers and characters
5) Now we have to calculate CP,H ,S and MOLECULAR WEIGHT
for that using the data of NASA THERMO.dat file we can read the data.
6) there are total of 53 gases of 14 coefficients defined under there individual local temperature range.we need to extract all the data from the file and calculate the cp, h ,s and molecular weight.
7) After defining the values of 14 coeffients we need to write a function for cp, h and s calculation with given formula in challenge.
8) Then we have to open the figure window for individual plot figures of cp, h, s vs temperature.
and using labels and title makes the plot better to understand.
9) then used mkdir for creating folder with folder name and cd command is used to change directory for each species .
10) Now saveas command is used to save the file in there respective folders.
11) al last using strcmp command we can calculate the molecular weight and formula for calculation is
for j = 1:x
for k = 1:length(atomic_names)
if(strcmp(species(j),atomic_names(k)))
mol_wt = mol_wt+at_wt(k);
position = k;
end
end
n = str2num(species(j));
if n>1
mol_wt = mol_wt+at_wt(position)*(n-1);
end
by using this we can calculate mol wt and it will be displayed in command window.