Program to Prase the NASA thermodynamic file and calculating required thermodynamic properties l Matlab l
This is the program to prase the NASA thermodynamic file and calculate the required thermodynamic properties.
In main function,at first I am reading the file using fopen and then reading the first string and similarly reading the temperatures using fgetl command and converting it to numbers using str2num command,Since there are 53 species in order to read the properties of all the speices we need to create a for loop with 53 iteration's.Then I am using strsplit to split the string and the empty space\'s.To read the name of the species,and to calculate the properties for each species I am creating a loop as shown in the program.And I am then calculating the required thermodynamic properties and then I am creating a function call for molecular weight and for the plot.Some of the command functions I have explained in the program directly..
Main function:
clear all
close all
clc
%fileopen
f1 = fopen(\'thermo30.txt\',\'r\');
%Header
fgetl(f1);
temp = fgetl(f1);
split=strsplit(temp,\' \');
%global temperatures
global_min_temperature = str2num(split{2})
global_mid_temperature = str2num(split{3})
global_max_temperature = str2num(split{4})
%neglecting comments
for i = 1:3
comment = fgetl(f1);
end
%carry out 53 iterations for 53 species
for j = 1:53
read = fgetl(f1);
splitname=strsplit(read,\' \')
name = splitname{1}
species = strcat(splitname{1},\'species\');
for k = 1:3
line1= fgetl(f1);
VE = findstr(line1,\'E\');
%for the first coeff
f=1;
%since the are 5 values in first row
d=5;
if(k==3)
d=4;
end
%reading temp of each line
for t=1:d
t1 = line1(f:VE(t)+3);
%to convert string to numbers
coef(t) = str2num(t1);
f = VE(t)+4;
end
for l =1:d
%assiging each value to the array
a(l+5*(k-1)) = coef(l);
end
end
t =[global_min_temperature,global_mid_temperature,global_max_temperature]
R = 8.314
%for low temp
r=7;
if(t==global_max_temperature)
r=0;
end
%Calculation
%specific heat
cp = (a(r+1)+a(r+2).*t + a(r+3).*t.^2 + a(r+4).*t.^3 + a(r+5)*t.^4).*R
%entalpy
H = (a(r+1) + a(r+2).*t/2 + a(r+3).*(t.^2)/3 + a(r+4).*(t.^3)/4 + a(r+5).*(t.^4)/5+a(r+6)./t).*R.*t
%entropy
S = (a(r+1).*log(t) + a(r+2).*t + a(r+3).*(t.^2)/2 + a(r+4).*(t.^3)/3 + a(r+5).*(t.^4)/4 + a(r+7))*R
% mole_wt function to calculate the molrcular weight of the species
molecular_weight = mole_wt(name)
% plotting function to plot the graphs
plotting(t,S,H,cp,name,species)
end
Program to calculate molecular weight:
This is the function defination of the molecular weight,
Since we already know the at.num of C H O N AR which are constants W.R.T , I am caluclating the mol.wt for all the species as shown,and I am using STRCMP to compare between two strings with the if statement as shown i nte program,
function m_wt = mole_wt(name)
elements = [\'H\' \'O\' \'C\' \'N\' \'AR\'];
% atomic weights of respective elements
at_wt = [1 16 12 14 40];
m=0;
for i=1:length(name)
for j =1:5
if strcmp(name(i),elements(j))
m=m+at_wt(j);
r=at_wt(j);
end
end
s = name(i);
% in case of numbers in the strings
if ( s==\'2\'||s==\'3\'|| s==\'4\'||s==\'5\'||s==\'6\'||s==\'7\'||s==\'8\'||s==\'9\')
b = str2num(s); % converting num string to number
m=m+r*(b-1);
end
end
% molecular weight of the species
m_wt = m;
end
Program for plotting the values:
This is the function defination of the plot,
By using mkdir I am creating a new folder by specifying the required directory as shown,
% function code of plotting graphs
function plotting(t,S,H,cp,name,species)
% creating the directory and path and to save the plot images
directory=sprintf(\'%s\',name);
mkdir(directory);
% ploting cp vs t
figure(1)
plot(t,cp,\'linewidth\',2,\'color\',\'r\')
xlabel(\'temperature\')
ylabel(\'specific heat\')
title(species)
%plotting enthalphy vs t
figure(2)
plot(t,H,\'linewidth\',2,\'color\',\'y\')
xlabel(\'temperature\')
ylabel(\'enthalphy\')
title(species)
% plotting entropy vs t
figure(3)
plot(t,S,\'linewidth\',2,\'color\',\'b\')
xlabel(\'temperature\')
ylabel(\'entropy\')
title(species)
wd = pwd;
cd(directory);
%saving the images under the mentioned names in jpeg format
saveas(figure(1), \'cp.jpeg\');
saveas(figure(2), \'H.jpeg\');
saveas(figure(3), \'S.jpeg\');
cd(wd);
end
For CO2:
Specific heat vs Temperature
Enthaply vs Temperature
Entropy vs Temperature
For O2:
Specific heat vs Temperature
Enthaply vs Temperature
Entropy vs Temperature
For NO2:
Specific heat vs temperature
Enthaply vs Temperature
Entropy vs Temperature