% Parsing the Nasa file
% Opening and reading the thermo data file
f1 = fopen('Thermo.dat','r');
fgetl(f1);
tline1 = fgetl(f1); % for reading the header information
tline1 = strsplit(tline1,' ');
global_low_temp = str2double(tline1{2});
global_mid_temp = str2double(tline1{3});
global_high_temp = str2double(tline1{4});
T1 = global_low_temp;
T2 = global_mid_temp;
T3 = global_high_temp;
for i = 1:3
fgetl(f1); % for skipping three consecutive lines
end
% Reading the species of gases from 1 to 53
for i = 1:53
header = fgetl(f1);
h = strsplit(header,' ');
species_name = (h{1});
x = length(h);
local_low_temp = str2double(h{x-3});
local_mid_temp = str2double(h{x-2});
local_high_temp = str2double(h{x-1});
t1 = local_low_temp;
t2 = local_mid_temp;
t3 = local_high_temp;
% now for getting the co-efficients
%line 1(a1-a5)
line1 = fgetl(f1);
L1 = strfind(line1,'E');
a1 = str2double(line1(1:L1(1)+3));
a2 = str2double(line1(L1(1)+4:L1(2)+3));
a3 = str2double(line1(L1(2)+4:L1(3)+3));
a4 = str2double(line1(L1(3)+4:L1(4)+3));
a5 = str2double(line1(L1(4)+4:L1(5)+3));
%line 2 ( a6-a10)
line2 = fgetl(f1);
L2 = strfind(line2,'E');
a6 = str2double(line2(1:L2(1)+3));
a7 = str2double(line2(L2(1)+4:L2(2)+3));
a8 = str2double(line2(L2(2)+4 :L2(3)+3));
a9 = str2double(line2(L2(3)+4:L2(4)+3));
a10 = str2double(line2(L2(4)+4 : L2(5)+3));
% line 3 (a11-a14)
line3 = fgetl(f1);
L3 = strfind(line3,'E');
a11 = str2double(line3(1:L3(1)+3));
a12 = str2double(line3(L3(1)+4:L3(2)+3));
a13 = str2double(line3(L3(2)+4 :L3(3)+3));
a14 = str2double(line3(L3(3)+4:L3(4)+3));
R = 8.314; % Universal gas constant
t = linspace(t1,t3,1000);
% For creating directory where the plot is to be saved
mkdir('C:\Users\Harsh\OneDrive\Desktop\Matlab\Matlab challenges\project 1.2',species_name);
% Assigning the values for specific heat,enthalpy,entropy and
% molecular weight
Cp = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,t,t1,t2,t3);
h = enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,t,t1,t2,t3);
S = entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,t,t1,t2,t3);
[M] = molecular_weight(species_name);
k = sprintf('Molecular weight of %s is %f',species_name,M);
disp(k);
%Plotting the graphs
% for Temp vs Specific heat
figure(1);
plot(t,Cp);
grid on;
title(species_name);
xlabel('Temperature');
ylabel('Specific Heat');
%For Temp vs Enthalpy
figure(2);
plot(t,h);
grid on
title( species_name);
xlabel('Temperature');
ylabel('Enthalpy');
% for Temp vs Entropy
figure(3);
plot(t,S);
grid on
title(species_name);
xlabel('Temperature');
ylabel('Entropy');
cd(species_name);
saveas(figure(1),'specific heat','jpg');
saveas(figure(2),'enthalpy','jpg');
saveas(figure(3),'entropy','jpg');
cd('C:\Users\Harsh\OneDrive\Desktop\Matlab\Matlab challenges\project 1.2')
end
% Creating the function for calculating specific heat
function[Cp] = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,t,t1,t2,t3)
if(t>t2)
Cp = (a1+a2.*t+a3.*t^2+a4.*t.^3+a5.*t.^4)*R;
else
Cp =(a8+a9.*t+a10.*t.^2+a11.*t.^3+a12.*t.^4)*R;
end
% Creating function for enthalpy
function[h] = enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,t,t1,t2,t3)
if(t>t2)
h = (a1+(a2.*t)/2+(a3.*t.^2)/3+(a4.*t.^3)/4+(a5.*t.^4)/5+(a6./t))*R.*t;
else
h = (a8+(a9.*t)/2+(a10.*t.^2)/3+(a11.*t.^3)/4+(a12.*t.^4)/5+(a13./t))*R.*t;
end
% creating function for creating entropy
function [S] = entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,t,t1,t2,t3)
if (t>t2)
S = a1.*log(t)+a2.*t+(a3.*t.^2)/2+(a4.*t.^3)/3+(a5.*t.^4)/4+a7;
else
S = a8.*log(t) +a9.*t+(a10.*t.^2)/2+(a11.*t.^3)/3+(a12.*t.^3)/3+(a12.*t.^4)/4+a14;
end
%function code for molecular weight
function[M] = molecular_weight(species_name)
elements = ['H','C','O','N','Ar'];
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948];%molecular mass
mol_wt=0;
for i = 1:length(species_name)
for j= 1:length(atomic_weight)
if strcmp(species_name(i),elements(j))
mol_wt = mol_wt+atomic_weight(j);
x = j;
end
end
n= str2double(species_name(i));
if n>1
mol_wt = mol_wt +atomic_weight(x)*(n-1);
end
end
M = mol_wt;
end