Aim = write code in matlab to parse the nasa thermodynamic data file and then calculate thermodynamics properties of various species.
Objective = . Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the nasa thermodynamic data file.
Calculation =
Specific heat =
Specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree.
Cp = R*(a1 + (a2*T) + (a3*(T.^2)) + (a4*(T.^3)) + (a5*(T.^4)))
Enthaply =
Enthalpy, the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. ... In symbols, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system: H = E + PV.
H = R*T.*(a1 + (a2*T/2) + (a3*(T.^2)/3) + (a4*(T.^3)/4) + (a5*(T.^4)/5) + (a6*(T)))
Entropy =
entropy, the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
S = R*((a1*log(T)) + (a2*T) + (a3*(T.^2)/2) + (a4*(T.^3)/3) + (a5*(T.^4)/4) + a7)
Syntax =
fopen('THERMO.dat') = fopen is used to open the file ('THERMO.dat').
fgetl(f1) = 'fgetl' reads one text line from (f1).
strsplit(global_temp,' ') = 'strsplit' separates each and every elements from given line(global_temp) by using dlimiter(' ').
strfind(tline2,'E') = 'strfind' is find out exact position of element('E') from the given line('tline2').
str2num(a1) = 'str2num' converts string(a1) into number.
sprintf(%s,species) = 'sprintf' is used to put data on (t)
mkdir(t) = 'mkdir' is used to create file for(t)
cd(t) = cd is function used to entering into file(t)
plot = plot is used to plot the graph (temp,Cp),(temp,H),(temp,S)
cd.. = cd.. is used close the file(t)
function molecular_weight = molmass(species)
elements = ['H','C','O','N','A','S'];
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948 32.065];
mol_mass = 0;
for l = 1:length(species)
for m = 1:length(atomic_weight)
if strcmp(species(l), elements(m))
molecular_weight = mol_mass + atomic_weight(m);
position = m;
end
end
end
end
clear all
close all
clc
f1 = fopen('THERMO.dat','r');
ft = fgetl(f1);
% global_temperature
global_temp = fgetl(f1);
t = strsplit(global_temp,' ');
%converting string cells into numbers by using str2num
global_low_temp = str2num(t{2});
global_medium_temp = str2num(t{3});
global_high_temp = str2num(t{4});
%skipping next three lines from the file
for i = 1:3
s = fgetl(f1);
end
for i = 1:53
tline1 = fgetl(f1);
n = strsplit(tline1,' ');
species = (n{1});
q = strfind(tline1,'.');
local_low_temperature = str2num(tline1(q(1)-3:q(1)+3));
local_high_temperature = str2num(tline1(q(2)-4:q(2)+3));
local_medium_temperature = str2num(tline1(q(3)-4:q(3)+4));
%temprature
T = (local_low_temperature:local_high_temperature);
%low temperature coefficent
tline2 = fgetl(f1);
a = strfind(tline2,'E');
a1 = tline2(1:a(1)+3);
a1 = str2num(a1);
a2 = str2num(tline2(a(1)+4:a(2)+3))
a3 = str2num(tline2(a(2)+4:a(3)+3));
a4 = str2num(tline2(a(3)+4:a(4)+3));
a5 = str2num(tline2(a(4)+4:a(5)+3));
tline3 = fgetl(f1);
b = strfind(tline3,'E');
a6 = tline3(1:b(1)+3);
a6 = str2num(a6);
a7 = str2num(tline3(b(1)+4:b(2)+3));
%low temperature coefficent
a8 = str2num(tline3(b(2)+4:b(3)+3));
a9 = str2num(tline3(b(3)+4:b(4)+3));
a10 = str2num(tline3(b(4)+4:b(5)+3));
tline4 = fgetl(f1);
c = strfind(tline4,'E');
a11 = tline4(1:c(1)+3);
a11 = str2num(a11);
a12 = str2num(tline4(c(1)+4:c(2)+3));
a13 = str2num(tline4(c(2)+4:c(3)+3));
a14 = str2num(tline4(c(3)+4:c(4)+3));
%Universal gas constant(J/K mol)
R = 8.314;
%Defining T
T = (local_low_temperature:local_high_temperature);
%Obtaining the values for Cp, enthalpy and entropy using the coefficients
if (T>local_medium_temperature)
%specific heat
Cp = R*(a1 + (a2*T) + (a3*(T.^2)) + (a4*(T.^3)) + (a5*(T.^4)));
%Enthaply
H = R*T.*(a1 + (a2*T/2) + (a3*(T.^2)/3) + (a4*(T.^3)/4) + (a5*(T.^4)/5) + (a6*(T)));
%Entropy
S = R*((a1*log(T)) + (a2*T) + (a3*(T.^2)/2) + (a4*(T.^3)/3) + (a5*(T.^4)/4) + a7);
else
%specific heat
Cp = R*(a8 + (a9*T) + (a10*(T.^2)) + (a11*(T.^3)) + (a12*(T.^4)));
%Enthaply
H = R*T.*(a8 + (a9*T/2) + (a10*(T.^2)/3) + (a11*(T.^3)/4) + (a12*(T.^4)/5) + (a13*(T)));
%Entropy
S = R*((a8*log(T)) + (a9*T) + (a10*(T.^2)/2) + (a11*(T.^3)/3) + (a12*(T.^4)/4) + a14);
end
%molecular_weight
molecular_weight = molmass(species)
%Creating the folder for the species
t = sprintf('%s', species);
mkdir(t);
%Going in to the folder of the species to save the plots
cd(t)
%Plotting the curves
%Cp v temperature plot
figure(1)
plot(Cp,T, 'b');
xlabel('specific heat')
ylabel('Temperature')
Cp_text = sprintf('Specific heat v temperature for %s', species);
title(Cp_text);
saveas(figure(1), 'Cp.jpg');
%Enthalpy v temperature plot
figure(2)
plot(H, T, 'r');
xlabel('Enthalpy')
ylabel('Temperature')
H_text = sprintf('Enthalpy v temperature for %s', species);
title(H_text);
saveas(figure(2), 'H.jpg');
%Entropy v temperature plot
figure(3)
plot(S, T, 'g');
xlabel('Entropy')
ylabel('Temperature')
S_text = sprintf('Entropy v temperature for %s', species);
title(S_text);
saveas(figure(3), 'S.png');
%Going back to the previous directory
cd ..;
end
steps =
1.write clear all,close all and clc
2.open the nasa thermodynamic data file given inquestion by using 'fopen' key.
3.'fgetl' used get one text line from data file.
4.with help of 'strsplit' determine global_temp.
5.skip the next three line using 'for loop'.
6.then use for loop for creating plot of S H and Cp with temp of all 53 species.
7.inside for loop calculate each and every coefficient of species using keys like( str2num, strfind)
8. after that using this coefficient calculate H S and Cp value for all 53 species with the help of 'if else end' key.
9 create file using 'mkdir' key and use'cd' key to enter into file.
10. In that file using 'plot' key plot the graph of H S and Cp for all 53 species.
11. use 'cd..' key to close the file.
12. These keys create inidividual files for individual species.
Error =
1 Error is showing because comma is missing between 'THERMO.dat' and 'r'.
steps to overcome error
1. Don't forget put comma between 'THERMO.dat' and 'r'.
reference
https://www.mathworks.com/help/matlab/matlab_prog/command-vs-function-syntax.html
Conclusion = With help of matlab keys like (strsplit,strfind,str2num, mkdir, cd ,cd..) we able to find out coefficent for all 53 species and plot graph of H S Cp for all 53 species.