Project 1 - Parsing NASA thermodynamic data
Images were attached as files and also included in the report.
function M=molecular_weight(name)
atoms=['H','C','O','N','r'];
fprintf('molecular weight of');
fprintf(name);
fprintf(':');
atomic_wt=[1 12 16 14 40];
M=0;
for i=1:length(name)
for j=1:length(atoms)
if strcmp(name(i),atoms(j)) %strcmp commands compares the two quantities
M=M+atomic_wt(j);
position=j;
end
end
n=str2num(name(i));
if n>1
M=M+atomic_wt(position*(n-1));
end
fprintf('%d',M);
disp(' ');
end
clc
clear all
R=fopen('THERMO.dat','r'); %open the data file in read mode
f_line=fgetl(R); %first line in the file is ready by fgetl command.
%we see the files contains global temperatures in 2nd line so
global_temp=fgetl(R);
%whole line is saved as a single string, so split it by
temp_range=strsplit(global_temp,' ');
%converting string and allocating the temperatures accordingly
gl_min_t=str2num(temp_range{2});
gl_med_t=str2num(temp_range{3});
gl_max_t=str2num(temp_range{4});
T=linspace(gl_min_t,gl_max_t,79);
%assigning temperature range from min to maximum with equal spaccing of 79(random number)
r=8.314; %universal gas constant
molecular_wt=0;
tic
%skiping lines
for i=3:5
comments=fgetl(R);
end
%assingning the coefficients with respect to 53 different composition of gases.
for i=1:53
l1=fgetl(R);
name=strtok(l1); %strtok command takes the first word in a line. so here it takes composition of gas.
%scanning next 3 lines;
l2=fgetl(R);
l3=fgetl(R);
l4=fgetl(R);
%finding a character to split the string
A=strfind(l2,'E');
B=strfind(l3,'E');
C=strfind(l4,'E');
%assigning the coefficients...
a1=str2num(l2(1:A(1)+3));
a2=str2num(l2(A(1)+4:A(2)+3));
a3=str2num(l2(A(2)+4:A(3)+3));
a4=str2num(l2(A(3)+4:A(4)+3));
a5=str2num(l2(A(4)+4:A(5)+3));
a6=str2num(l3(1:B(1)+3));
a7=str2num(l3(B(1)+4:B(2)+3));
%here, 1st 7 coefficients are of lower temperature and assigned by a
% 2nd 7 terms are related to high temperature and assigned by b
b1=str2num(l3(B(2)+4:B(3)+3));
b2=str2num(l3(B(3)+4:B(4)+3));
b3=str2num(l3(B(4)+4:B(5)+3));
b4=str2num(l4(1:C(1)+3));
b5=str2num(l4(C(1)+4:C(2)+3));
b6=str2num(l4(C(2)+4:B(3)+3));
b7=str2num(l4(C(3)+4:B(4)+3));
%specific heat
cp=spheat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,r,gl_med_t);
%enthalpy
h=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,r,gl_med_t);
%entropy
s=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,r,gl_med_t);
molecular_wt(i)=molecular_weight(name);
plotting(cp, h, s, T, name);
end
toc
function plotting(cp, h, s, T, name)
cd(pwd)
dir_name = name;
mkdir(dir_name);
figure(1)
plot(T,cp,'linewidth',2,'color','b');
title(sprintf('specific heat Vs temperature of %s', name));
xlabel('temperature');
ylabel('specific heat');
figure(2)
plot(T,h,'linewidth',2,'color','r');
title(sprintf('Enthalpy Vs temperature of %s', name));
xlabel('temperature');
ylabel('enthalpy');
figure(3)
plot(T,s,'linewidth',2,'color','y');
title(sprintf('entropy Vs temperature of %s', name));
xlabel('temperature');
ylabel('entropy');
cd(dir_name)
saveas(1,'cp_vs_t.png');
saveas(2,'h_vs_t.png');
saveas(3,'s_vs_t.png');
cd ..
end
function s=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,r,gl_med_t)
if T<=gl_med_t
s=r*(a1.*log(T)+ a2.*T + a3.*(T.^2) + a4.*(T.^3) + a5.*(T.^4)/4 + a7);
else
s=r*(b1.*log(T)+ b2.*T + b3.*(T.^2) + b4.*(T.^3) + b5.*(T.^4)/4 + b7);
end
function cp=spheat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,r,gl_med_t)
if T>=gl_med_t
cp=r*(a1+ a2*T + a3*T.^2+ a4*T.^3+ a5*T.^4);
else
cp=r*(b1+ b2*T + b3*T.^2+ b4*T.^3+ b5*T.^4);
end