Project 1 - Parsing NASA thermodynamic data
1Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. You will be reading this file For Nasa thermodynamic data. The formulae are shown below.
Here R is the universal gas constant, T is the temeprature
if s{x}=='1'
smt(i)=str2num(s{x-1});
sht(i)=str2num(s{x-2});
slt(i)=str2num(s{x-3});
s_name(i)=string(s{1});
%s_code({i)= strcat(s{2:x-4});
xexecute the seet of command if string at the end of readed line is ‘1°
li=fgetl(file); %first line containing coeff
1l2=fgetl(file); %second line containing coeff
13=fgetl(file}); %third line containing coeff
co=[ 11 12 13];
p=findstr(co,'E');
%read values in 2nd line of 0
for k=1:length(p)
11=p(k)-113;12=p(k)+3;
coeff(i,k)=str2num(co(11:12));
end
end
else
break;
end %reading a single data end
i=i+1;
end
%finding molecular weight
for i=1:1:length(s_name)
M(i)=molecular_weight(s_name(i));
end
prompt= ‘want to create folders with plots for all species(1/0)';
in=input(prompt) 3
if (in==1)
%saving plots in seperate folders for each species
for i=1:length(M)
foldername=sprintf('%s',s_name(i));
mkdir (foldername) ;
temp=[slt(i),sht(i),smt(i)];
[T Cp En Et]=Cp_enthalpy_entropy_new(temp, coeff(i,:) );
cd(foldername);
Ttable(i,:)=T;
Cptable(i,:)=Cp;
Entable(i,:)=En;
Ettable(i,:)=Et;
figure(1);
plot(T,Cp)}
xlabel(’Temperature’); ylabel('Cp’); grid on
saveas(gcf,'TvsCp.jpg’);
close(gcf}
figure(2);
plot(T,En)
xlabel(*Temperature’); ylabel('Enthalpy’);
saveas(gcf, 'TvsEnthalpy.jpg');
close(gcf)
figure(3);
plot(T,Et)
xlabel(*Temperature’); ylabel('Entropy’);
saveas(gcf, 'TvsEntropy.jpg');
close(gcf)
cd ..
end
%to write a file with molecular weight
mol=fopen('molecular weight.txt', ‘w’)
fprintf('species Molecular weight');
for i=1:length(s_name)
fprintf(mol,'%1@st molecular weight is %15.6f n',s_name(i),M(i));
end
fclose(mol)
end
%for plottiing based on user requirement
j=1;
while j==1
prompt='enter species name n‘;
opt=input(prompt,'s’);
ss=strcmpi(s_name,opt);
if ss==@8
prompt=‘species not found n want to continue 1/@)';
op=input (prompt);
else
no=find(ss,1);
temp=[slt(no),sht(no),smt(no)];
[T,Cp, En, Et]=Cp_enthalpy_entropy_new(temp, coeff(no,:) );
%molecular weight in for the user needed species
figure(1);
Molecular_Weight = M(no)
%plotting
plot(T,Cp)}
t=sprintf(' %s molecular weight = %d°,opt,M(no));
title(t);
xlabel(*Temperature’); ylabel('Cp’); grid on;
saveas(gcf,'1.jpg’);
figure(2);
t=sprintf(' %s molecular weight = %d°,opt,M(no));
title(t);
plot(T,En)
t=sprintf(' %s molecular weight = %d’,opt,M(no));
title(t);
xlabel(*Temperature’); ylabel('Enthalpy’)}; grid on;
saveas(gcf,'2.jpg’);
figure(3);
t=sprintf(' %s molecular weight = %d°,opt,M(no));
title(t);
plot(T,Et)
t=sprintf(' %s molecular weight = %d°,opt,M(no));
title(t);
xlabel(*Temperature’); ylabel('Entropy’); grid on;
saveas(gcf,'3.jpg’);
prompt=‘want to continue 1/0)';
op=input( prompt);
end
if op==@ ‘%for ending based on user input
j=9;break;
end
end
fclose(file)
function [m]=molecular_weight(N)
%removing paranthesis from the species names
N= regexprep(N,‘(",°')5
N= regexprep(N,‘)','')5
%convert species name to char array
name=convertStringsToChars(N);
%default vales assigining and array fro string compare
[0,H,C,N,A,S]=deal(15.999,1.00784,12.0107,14.0067,39.948,32.865);
letter=[0,H,C,N,A,5];
element=['0','H',°C’,’N','A','S'];
x=length(name);
f=isletter(name);
m=Q;
i=1;
while i<=x %main loop while loop used instead of for so it will work for names such as C12H22
if f(i)==1 % executed if char in i-th position is a letter
for j=l: length(element)
if strcmpi(name(i),element(j))
at=letter(j);
end
end
m=m+at; %molecular weight
else % executed if char in i-th position is a number
k=ij;s='';
while((k<=x) && (f(k)==0) )
s = strcat(s,name(i));
if (i<x) && (f(i+1)==0)
i=i+1;
end
k=k+1;
end
num = str2num(s);
m=m+ ( at * (num-1)) 3 %molecular weight
end
i=i+1;
end
end
OUTPUT:
PLOTS: