Project 1 - Parsing NASA thermodynamic data
Code for Calculating the specific heat :
creating the function :
% Function for the specific heat
function cp = specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)
if Temp<=global_medium_temp
cp=R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(Temp).^3) + (a12*(Temp).^4));
else
cp=R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(Temp).^3) + (a5*(Temp).^4));
end
Explaination :
used the formula that is given in the formula and coded the function as it is mentioned .

specificheat() is a function with input variables a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,gloabl_medium_temp .
where a1 ,a2---- a12 are the coeff extracted from the THERMO.dat file that is present in the challenge , R is the universal gas constant .
by using if else loop we are setting the condition for the temp coeff. to be functional when a condition is fulfilled ie. if the Temp <= gloabl_medium_temp then it will follow the mentioned formula else
will follow the other formula as mentioned ( just coeff are changed ).
Code for the calculation of the enthalpy :
Creating the function :
function h= Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp)
if Temp <=global_medium_temp
h=R*Temp.*(a8 + (a9*Temp)/2) + ((a10*(Temp).^2)/3) +((a11*(Temp).^3)/4) +((a2*(Temp).^4)/5) + ((a13./Temp));
else
h=R*Temp.*(a1 + (a2*Temp)/2) + ((a3*(Temp).^2)/3) +((a4*(Temp).^3)/4) +((a5*(Temp).^4)/5) + ((a6./Temp));
end
Explaination :
used the formula that is given in the formula and coded the function as it is mentioned .

Enthalpy() is a function with input variables a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,a13,Temp,R,gloabl_medium_temp .
where a1 ,a2---- a13 are the coeff extracted from the THERMO.dat file that is present in the challenge , R is the universal gas constant .
by using if else loop we are setting the condition for the temp coeff. to be functional when a condition is fulfilled ie. if the Temp <= gloabl_medium_temp then it will follow the mentioned formula else
will follow the other formula as mentioned ( just coeff are changed ).
Code for the calculation of the entropy :
Creating the function :
function s = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp)
if Temp<=global_medium_temp
s =R.*(a8.log(Temp) + (a9.*Temp) + (a10)*((Temp).^2/2) + (a11)*((Temp).^3/3) + (a12)*((Temp).^4/4) + a14 );
else
s =R.*(a1.*log(Temp) + (a2.*Temp) + (a3)*((Temp).^2/2) + (a4)*((Temp).^3/3) + (a5)*((Temp).^4/4) + a7);
end
Explaination :
used the formula that is given in the formula and coded the function as it is mentioned .

Entropy() is a function with input variables a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,gloabl_medium_temp .
where a1 ,a2---- a14 are the coeff extracted from the THERMO.dat file that is present in the challenge , R is the universal gas constant .
by using if else loop we are setting the condition for the temp coeff. to be functional when a condition is fulfilled ie. if the Temp <= gloabl_medium_temp then it will follow the mentioned formula else
will follow the other formula as mentioned ( just coeff are changed ).
Code for the calculation of the entropy :
Creating the function :
function m = molecular_weight(species_SP)
atomic_name = ['H','C','O','N','A'];
fprintf('Molecular weight of ');
fprintf('species_SP');
fprintf(':');
Atomic_weight = [1.008 12.011 15.99 14.007 39.948];
m=0;
for i=1: length(species_SP)
for j= 1:length(atomic_name)
if strcmpi(species_SP(i),atomic_name(j))
m=m + Atomic_weight(j);
position = j;
end
end
n=str2num(species_SP(i));
if n>1
m= m + Atomic_weight(position)*(n-1);
end
end
fprintf('molecularweight %s :',species_SP);
fprintf('%d',m);
disp(' ');
end
The main code for the calculation of coeff., plotting the graphs required ( cp,s,h VS temprature ),saving the folders , also calculating the molecular weight :
clear all
close all
clc
mkdir 'C:UsersAdminDesktopNASA file Parsing'
f1= fopen('THERMO.dat','r');
gt=fgetl(f1);
global_temp = fgetl(f1);
t=strsplit(global_temp);
global_low_temp =str2num(t{2});
global_medium_temp =str2num(t{3});
global_high_temp =str2num(t{4});
Temp= linspace(global_low_temp,global_high_temp,1000);
R=8.314;
% ignoring the 3 comments in the file
for i=1:3
gt=fgetl(f1);
end
for i=1:53
gt=fgetl(f1);
A=strsplit(gt,' ');
species_SP = (A{1})
line_1=fgetl(f1);
a=strfind(line_1,'E');
% Extracting the first 7 higher Temprature coeff
a1=line_1(1:a(1)+3);
a1=str2num(a1);
a2=line_1(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=line_1(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=line_1(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=line_1(a(4)+4:a(5)+3);
a5=str2num(a5);
line_2=fgetl(f1);
b=strfind(line_1,'E');
a6=line_2(1:b(1)+3);
a6=str2num(a6);
a7=line_2(a(1)+4:b(2)+3);
a7=str2num(a7);
% Extracting the next 7 low Temprature coeff
a8=line_2(b(2)+4:b(3)+3);
a8=str2num(a8);
a9=line_2(b(3)+4:b(4)+3);
a9=str2num(a9);
a10=line_2(b(4)+4:b(5)+3);
a10=str2num(a10);
line_3=fgetl(f1);
C=strfind(line_1,'E');
a11=line_3(1:C(1)+3);
a11=str2num(a11);
a12=line_3(C(1)+4:C(2)+3);
a12=str2num(a12);
a13= line_3(C(2)+4:C(3)+3);
a13=str2num(a13);
a14=line_3(C(3)+4:C(4)+3);
a14=str2num(a14);
%Calculation of specific heat
cp = specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)
%Calculation of Enthalpy
h=Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp);
%Claculation of Entropy
s=Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp);
%Calculating the Molecular weight
m = molecular_weight(species_SP);
%Reading & writing Molecualr list
Molecular_list =fopen('Molecular weight.txt','at');
fprintf(Molecular_list,'Molecular weight of %s is %d n',species_SP,m);
fclose(Molecular_list);
%% Plotting (cp,h,s,Temp,species_Sp) & saving as a folder
cur_dir=pwd;
mkdir(species_SP);
cd(species_SP);
%Plot no. 1 specific heat VS Temprature
figure(1)
plot(Temp,cp,'linewidth',2,'color','b');
xlabel('Temprature in (K)');
ylabel('Specific heat in (KJ)');
title(sprintf('Specific heat variation with Temprature range of %s',species_SP));
grid on
saveas(figure(1),'Specific heat.jpg');
%Plot no. 2 Enthalpy VS Temprature range
figure(2);
plot(Temp,h,'linewidth',2,'Color','r');
xlabel('Temprature in (K)');
ylabel('Enthalpy in (KJ/mol)');
title(sprintf('Enthalpy variation with Temprature range of %s',species_SP));
grid on
saveas(figure(2),'Enthalpy.jpg');
%Plot no. 2 Entropy VS Temprature range
figure(3);
plot(Temp,s,'linewidth',2,'Color','g');
xlabel('Temprature in (K)');
ylabel('Entropy in (KJ/mol - K)');
title(sprintf('Entropy variation with Temprature range of %s',species_SP));
grid on
saveas(figure(3),'Entropy.jpg');
cd(cur_dir);
m = molecular_weight(species_SP)
f2=fopen('molecular mass ','w');
fprintf('%s n %s n %f n','species','Molecular mass',m);
fclose(f2);
end
Explaination of the code :
1. In the first 3 line command is used to clear all the current variable in workspace , to close all the figure and also clearing the command window j.
2. mkdir(foldername) this is the synatx used , it is used to create the folder
3. fopen(filename,permision,machinefmt,encodingIn) as per the code we are only giving the file name as THERMO.dat and permision to read ('r').& stroring it into f1.
4.fgetl(fileId) this is the syntax , here file Id is f1 and storing it into gt . it is used read a line from the file.
5. strsplit(string) it is used to split the string . here we need to split the global_temp as it is a string.
6. str2num(character,options) converts the string array to numeric array
7. After that we have used the linspace commmand to create the array including the starting vlaue as global_low_temprature and ending value as global_high_temprature and having 1000 values , assigned this to a variable Temp
8.R it is the universal gas constant so defining it with value 8.13
9.using the for loop to skip the 3 lines in the data
10. now here strr that using the for loops to read all the 53 species
11. strfind command is used to find the string
12.Now locating the position of the data point to extract the coeffiecients
13.here again str2num is used to convert string array to numeric array
14.repeating 12 & 13 step for claculating the 14 coeff.
15. using the respective coded functions for specific heat , entropy , enthalpy .
16.plot Command is used to plot the graph respectively
17x and y label command are used tospecify bothe axes
18. title command is used to give the specific title to the graph respectively
19. grid on command is used to on the grids in the graph
20. figure command is used to give identification to the figure .
21.saveas(figname , filename ,formattype) in the same syntax this command is used to save the figure .
22. after that crteating another file f2 , file name Molecular mass and writing in this file , and closing the file
23. END command is used to the close the for loop.
PLOTS :
1. Entropy Vs Temprature

2. Enthalpy Vs Temprature

3.Specific heat Vs Temprature

Screenshots showing all the Folders




Error while coding :
1. I faced this error because i used a wrong inbuilt function saves instead of saveas.

2. I faced this error beacuse there was an error in my function for specific heat i forgot to use the parenthesis ath the end of the function while applying the given foirmula logic.

References :
https://in.mathworks.com/help/matlab/index.html?s_tid=CRUX_lftnav
wikipedi and also youtube videos
watching the videos available in the course they are really helpfull and also exploring the mathworks help center .