Project 1 - Parsing NASA thermodynamic data
Aim : To extract the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
To calculate enthalapy, entropy and specific heat i have made different functions and calling them from the main function.
Specific heat code :
%%creating Specific heat function
function cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,temp,mid_temp)
%using if condition as there are two temperature co-efficients
if temp >= mid_temp
cp= (a1+a2*temp+a3*temp.^2+a4*temp.^3+a5*temp.^4)*R;
else
cp= (a8+a9*temp+a10*temp.^2+a11*temp.^3+a12*temp.^4)*R;
end
end
Enthaply Code :
%%enthalapy
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R,temp,mid_temp)
if temp >= mid_temp
H = (a1+a2*temp./2+a3*temp.^2./3+a4*temp.^3./4+a5*temp.^4./5+a6./temp)*R.*temp;
else
H =( a8+a9*temp./2+a10*temp.^2./3+a11*temp.^3./4+a12*temp.^4./5+a13./temp)*R.*temp;
end
end
Entropy Code :
%%creating entropy function
function S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,temp,mid_temp)
if temp >=mid_temp
S = (a1*log(temp)+a2*temp+a3*temp.^2./2+a4*temp.^3./3+a5*temp.^4./4+a7)*R;
else
S = (a8*log(temp)+a9*temp+a10*temp.^2./2+a11*temp.^3./3+a12*temp.^4./4+a14)*R;
end
end
Note: The molecular weight code is incorporated in Main code
Main code:
clear all
close all
clc
F=fopen('THERMO.dat','r');%opening termo file for reading
fgetl(F);%getting the first statement from file
t=fgetl(F);%assigining the global temperatures line to a variable
tg = strsplit(t,' ');%spliting the srting which contains gloabl temperatures
%% assigning the temperatures to different variables
low_temp = str2num(tg{2});
mid_temp = str2num(tg{3});
high_temp = str2num(tg{4});
%Skipping next three lines
for j = 1:3
fgetl(F);
end
%%creating for loop for the elements
%% entering into elemenets level
for e=1:53;
name = fgetl(F);
elements_name = strtok(name);%getting the name of element
Foldername = elements_name;%giving the element name as foldername while creating
%extracting the data from next three lines after element name
c1=fgetl(F);%getting data from next line
ex=strfind(c1,'E');%breaking the srting using exponent E
a1=str2num(c1(1:12+3));
a2=str2num(c1(12+4:27+3));
a3=str2num(c1(27+4:42+3));
a4=str2num(c1(42+4:57+3));
a5=str2num(c1(57+4:72+3));
c2=fgetl(F);%getting data from next line
ex2=strfind(c2,'E');%breaking the srting using exponent E
a6=str2num(c2(1:12+3));
a7=str2num(c2(12+4:27+3));
a8=str2num(c2(27+4:42+3));
a9=str2num(c2(42+4:57+3));
a10=str2num(c2(57+4:72+3));
c3=fgetl(F);%getting data from next line
ex3=strfind(c3,'E');%breaking the srting using exponent E
a11=str2num(c3(1:12+3));
a12=str2num(c3(12+4:27+3));
a13=str2num(c3(27+4:42+3));
a14=str2num(c3(42+4:57+3));
%% calling all the functions created
%creating
temp = linspace(low_temp,high_temp,1000);
R=8.314;
%making folder
%calling specific heat function
cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,temp,mid_temp);
%calling enthalpy
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R,temp,high_temp);
%calling entropy functions
S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,temp,mid_temp);
%created a folder called elements and storing entire folders in it
cd C:\Users\navee\Elements
mkdir(Foldername);%creating the folder
figure(1);
plot(temp,cp);
xlabel('Temperature')
ylabel('Specific heat')
title(Foldername)
figure(2);
plot(temp,H);
xlabel('Temperature')
ylabel('Enthalpy')
title(Foldername)
figure(3);
plot(temp,S);
xlabel('Temperature')
ylabel('Entropy')
title(Foldername)
%making a directiry so to store the saved images inside the folder
directory =(['C:\Users\navee\Elements\',Foldername]);
cd(directory)
saveas(figure(1),'specific heat.jpg');
saveas(figure(2),'enthalpy.jpg');
saveas(figure(3),'entropy.jpg');
cd C:\Users\navee;%place where the program lies
%% molecular weight calculation, Molecular weight is equal to sum of all the
%atoms present in a molecule
Atoms = ['O','S','N','H','C','A'];
Atomic_weight = [15.9994,32.065,14.00674,1.007844,12.0107,39.948];%atomic weights of atoms in data
Molecular_weight = 0;
for i =1:length(elements_name);
for j = 1:length(Atomic_weight);
if strcmp(elements_name(i),Atoms(j));%comparing Elements name with know atoms name
Molecular_weight = Molecular_weight + Atomic_weight(j);
pos = j;%saving the position
end
end
n = str2num(elements_name(i));
if n >1;
Molecular_weight = Molecular_weight + Atomic_weight(pos)* (n-1);
end
end
fprintf('The Molecular weight of the %s is %d u',elements_name,Molecular_weight)%printing the molecular weight
disp(' ')
end
Specific heat: O2

Entropy: O2

Enthalpy: O2

Specific Heat: N2

Entropy: N2

Enthalpy: N2

Specific Heat: CO2

Entropy: CO2

Enthalpy: CO2

Screennshot Of Folder Automatically generated :

Molecular weights Displayed in Command window :


References :
1.https://in.mathworks.com/help/matlab/ref/strtok.html
2.https://in.mathworks.com/help/matlab/ref/str2num.html
3.https://in.mathworks.com/help/matlab/ref/mkdir.html
4.https://in.mathworks.com/help/matlab/ref/cd.html