Project 1 - Parsing NASA thermodynamic data
clear all
close all
clc
f1 = fopen('thermo1.dat','r');
first_line = fget(f1);% reading the 1st line from data
% obtaining global max,min and medium temp
global_temp = fget1(f1) % reading 2nd line from data
temp_range = strsplit(global_temp,' ') % splitting 2nd line using blank space as delimiter
% obtaining temp values from splitted data
global_min_temp = str2num(temp_range{2})
global_med_temp = str2num(temp_range{3})
global_max_temp = str2num(temp_range{4})
% create a vector of 400 evenly spaced temp points
T = linspace (global_min_temp,global_max_temp,400);
R =8.314; % universal gas constant
Comments1 = fget(f1); % reading 3rd line from data
Comments2 = fget(f1); % reading 4th line from data
Comments3 = fget(f1); % reading 5th line from data
for i = 1:53
line1 = fget1(f1);
element_name = stroke(line1);
line2 = fget(f1);
A = strfind(line2,'E");
line3 = fget(f1);
B= strfind(line3,'E");
line4 = fget(f1);
C = strfind(line4,'E");
a1 =str2num(line2(1:A(1)+3));
a2= str2num(line2(A(1)+4;A(2)+3));
a3= str2num(line2(A(2)+4;A(3)+3));
a4= str2num(line2(A(3)+4;A(4)+3));
a5= str2num(line2(A(4)+4;A(5)+3));
a6 =str2num(line3(1:B(1)+3));
a7= str2num(line3(B(1)+4;B(2)+3));
b1= str2num(line3(B(2)+4;B(3)+3));
b2= str2num(line3(B(3)+4;B(4)+3));
b3= str2num(line3(B(4)+4;B(5)+3));
b4 =str2num(line4(1:C(1)+3));
b5= str2num(line4(c(1)+4;c(2)+3));
b6= str2num(line4(c(2)+4;c(3)+3));
b7= str2num(line4(c(3)+4;c(4)+3));
% specific heat
cp = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_med_temp);
% enthalpy
h= enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_med_temp);
%entropy
s= entropy_calc(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_med_temp);
%calculating molecular weight of species
Molecular_wt(i) = Molecular _weight(element_name);
% saving and plotting the data
cd('c : users919960Desktopmatlab project NASAnaaaaaspecies')
dir_name = element_name;
mkdir(dir_name)
figure(1)
plot(T,Cp,'linewidth',2,'color','b')
title(Sprintf('specific heat vs temp for %s', element_name))
xlabel('temperature')
ylabel('specific heat')
grid on
figure(2)
plot(T,h,'linewidth',2,'color','b')
title(Sprintf('enthalpy vs temp for %s', element_name))
xlabel('temperature')
ylabel('enthalpy')
grid on
figure(3)
plot(T,s,'linewidth',2,'color','b')
title(Sprintf('entropy vs temp for %s', element_name))
xlabel('temperature')
ylabel('entropy')
grid on
% pause the loop so that you can see each species plot in real time
pause(0.0001)
cd(dir_name);
saveas(1,'specific heat.png');
saveas(2,'enthalpy.png');
saveas(3,'entropy.png');
end
fclose(f1);
% function to calculate Molecular weight
function MW = Molecular_weight(element_name)
atoms= ['H','C','N','O','A'];
fprintf('Molecular weight of');
fprintf(element_name);
fprintf(' : ');
atomic_weight = [1 12 14 15 39];
MW = 0;
for i = 1; length(element_name)
for j =1; length(atoms)
if strcmp(element_name(i),atoms(j))
MW = MW+atomic_weight(j);
position =j;
end
end
n = str2num(element_name(i))
if n>1
MW = MW + atomic_weight(position)*(n-1);
end
end
fprintf('%d',MW);
disp(' ');
end
% program to specific heat
function cp = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_med_temp)
%comparing with global gtemp
if T>global_med_temp
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
% program to calculate enthalpy
function h = enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_med_temp)
if T>global_med_temp
cp = R.*(a1.*T+(a2.*T^2)./2+(a3.*T^3)./3+(a4.*T^4)./4+(a5.*T^5)./5+a6);
else
cp = R.*(b1.*T+(b2.*T^2)./2+(b3.*T^3)./3+(b4.*T^4)./4+(b5.*T^5)/5+b6);
end
%program to calculate entropy
function h = entropy_calc(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b6,T,R,global_med_temp)
if T<=global_med_temp
s = R.*(a1.*log(T)+(a2.*T)+(a3.*T.^2)./2+(a4.*T.^3)./3+(a5.*T.^4)./4+a7);
else
s = R*(b1.*log(T)+b2.*(T)+b3.*(T.^2)./2+b4.*(T.^3)./3+b5.*(T.^4)./4+b7);
end