AIM:
To write a code in MATLAP to parse the NASA data file and then we calculate the thermodynamics properties of various gas species
OBJECTIVES:
* Write the fuction of that extracts then 14 coefficents and calculate the enthalpy,entropy and specific heat for all the species in the data file
*caluculating the molecular weight of each species
*plotting the specific heat,enthalphy,entropy for the local temp range
*saving the plots as image with valid names and save them in seperate folder for each species
FORMULA USING:
SPECIFIC HEAT:
cp/R = a1+a2 T+ a3 T^2+ a4 T^3+ a5 T^4
ENTHALPY:
H/RT = a1+a2 T/2+ a3 T^2/3+a4 T^3/4+a5 T^4/5+a6/T
ENTROPY:
a1 lnT+a2 T +a3 T^2/2+ a4 T^3/3+a5 T^4/4+ a7
T = local temperature
a1-a7 =low temperature
a8-a14 =high temperature
R = gas constant(8.314)
MAIN PROGRAMME:
close all
clear all
clc
%reading the thermodynamics data file
f1 = fopen ('THERMO.dat','r')
fgetl(f1);
t_line = fgetl(f1);
A = strsplit(t_line,' ');
% defining the temperature extracted from the data file
low_temp = str2double(A{2});
mid_temp = str2double(A{3});
high_temp = str2double(A{4});
for i = (1:3)
comments = fgetl(f1);
end
for i = (1:53)
%extracting species name
species = fgetl(f1);
species_name = species(1:6);
end
% extracting coefficient from first line
line_1 = fgetl(f1);
l1 = strfind(line_1,'E');
a1 = str2double(line_1(1:(l1(1)+3)));
a2 = str2double(line_1((l1(1)+4):(l1(2)+3)));
a3 = str2double(line_1((l1(2)+4):(l1(3)+3)));
a4 = str2double(line_1((l1(3)+4):(l1(4)+3)));
a5 = str2double(line_1((l1(4)+4):(l1(5)+3)));
%extracting coefficients from second line
line_2 = fgetl(f1);
l2 = strfind(line_2,'E');
a6 = str2double(line_2(1:(l2(1)+3)));
a7 = str2double(line_2((l2(1)+4):(l2(2)+3)));
a8 = str2double(line_2((l2(2)+4):(l2(3)+3)));
a9 = str2double(line_2((l2(3)+4):(l2(4)+3)));
a10 = str2double(line_2((l2(4)+4):(l2(5)+3)));
% extracting the coefficients from third line
line_3 = fgetl(f1);
l3 = strfind(line_3,'E');
a11 = str2double(line_3(1:(l3(1)+3)));
a12 = str2double(line_3((l3(1)+4):(l3(2)+3)));
a13 = str2double(line_3((l3(2)+4):(l3(3)+3)));
a14 = str2double(line_3((l3(3)+4):(l3(4)+3)));
T = linspace(low_temp, high_temp,500);
R = 8.314;
%specific heat, Enthalpy and Entrophy calculation
for i=1:length(T)
if T(i)>mid_temp
cp(i)= (a1+a2.*T(i)+a3.*T(i).^2+a4.*T(i).^3+a5.*T(i).^4)*R;
H(i)= R.*T(i)*(a1+a2.*(T(i))./2+a3.*(T(i))./3+a4.*(T(i))./4+a5.*(T(i))./5+a6./T(i));
S(i)= (a1.*log(T(i))+a2.*T(i)+a3.*(T(i)^2)/2+a4.*(T(i)^3)/3+a5.*(T(i)^4)/4+a7)*R;
else
cp(i)= (a8+a9.*T(i)+a10.*T(i).^2+a11.*T(i).^3+a12.*T(i).^4)*R;
H(i)= R*T(i).*(a8+((a9*T(i)/2))+((a10*T(i).^2/3)+((a11*T(i).^3)/4)+((a12*T(i).^4)/5)+((a13./T(i)))));
S(i)= (a8.*log(T(i))+a9.*T(i)+a10.*(T(i)^2)/2+a11.*(T(i)^3)/3+a12.*(T(i)^4)/4+a14)*R;
end
end
% Saving the folder
cd('C:\Users\Jeyaprakash\Documents\MATLAB\New folder\species') % cd=change directory
mkdir(species); % mkdir=make directory/folder
cd(species);
% To plot varation of cp with T
figure(1)
plot(T, cp, 'linewidth',2,'Color','g')
xlabel('Temperature in k')
ylabel('specific heat in kJ/kg.k')
grid on
title(sprintf('variation of specific heat with temperature for %s', species))
saveas(figure(1),'cp.png');
%To plot the variation of enthalphy with temperature
figure(2)
plot(T, H, 'linewidth',2,'Color','r')
xlabel('Temperature in k')
ylabel('Enthalpy in kJ/kg')
grid on
title(sprintf('variation of Enthalpy with temperature for %s', species))
saveas(figure(2),'H.png');
% To plot the variation of entrophy with temperature
figure(3)
plot(T, S, 'linewidth',2,'Color','b')
xlabel('Temperature in k')
ylabel('Entropy in kJ/kg')
grid on
title(sprintf('variation of Entropy with temperature for %s', species))
saveas(figure(3), 's.png');
cd('C:\Users\Jeyaprakash\Documents\MATLAB\New folder\species')
% to calculate molecular weight
elements = {'H','C','O','N','A','S'};
atomic_weight = [1,12,16,14,39,32];
molecular_weight = 0;
for i = 1:length(species)
for l = 1:length(elements)
if strcmp(species(i),elements(l))
molecular_weight = molecular_weight+atomic_weight(1);
position = 1;
end
end
n = str2double(species(i));
if(n>1)
molecular_weight = molecular_weight+(atomic_weight(position)*(n-1));
end
end
disp(molecular_weight)
sprintf('the molecular weight of %s is %d',species,molecular_weight)
% ploting for temperature |specific heat|enthalpy|entropy
figure (1)
plot(T,Cp)
xlabel('Temperature [k]')
ylabel('specific heat[J/Kg.K]')
title('Temperature vs specific heat')
%plotting for temperature enthalpy
figure(2)
plot(T,H)
xlabel('Temperature [K]')
ylabel('Enthalpy [KJ]')
title('temperature vs Enthalpy')
%plottingfor temperature entropy
figure(3)
plot(T,S)
xlabel('Temperature [K]')
ylabel('Entropy (J/K]')
title('Temperature vs Entropy')
%COMMAND USED
*fopen = the command used to open the file
*to read the data we used fgetl command and read every next line by using this and we save every line in the particular variables
*strcmp = to comapare the all the lines to get end character in the data sheet
*strsplit = to split the seprate the quantity which can be stored in variables
*findstr = find the location of particluar character from the string
*strsnum = this command is used to convert the string to numbers
*mkdir = to create a folder we required to create a folder so we can save the all images of the plot to specific folder
*saveas = used to save the plot as jpg format
PROCEDURE
* first to open the Thermo data file using fopen command and save it line in specific variables i use this command in taht way that it comapre all the line by line to get end character so the more data added to file the programme can run for it
and i calculate the number of species and save it varaiables
*in oreder to extract the data for the global (min,mid,high)temperature then we used to split command to split into the separate quantity and save it into variables
after use fgetl command to skip the command line
*same process of splitting i use in next line to split the line foe the local minimum ,local medium to local high temperature then i use linspace command to get more values betwen local temp to high temp range
*next we should extract the coefficints of species i use to extract the coefficient of species is , i exmaine the data and reach a point that exponential (e) is scnitific character and is avilable in every coefficient so i locate the position of (e) in coefficient by using findstr command
*after calculation we should put the values to plot the graph
*Temperature vs specific heat
*Temperature vs Enthalphy
*Temperature vs Entropy
*and save the plots
*next we calculate for molecular weight of species
*to create two array first save elements name [elements = {'H','C','O','N','A','S'};] and second put the name is atomic weight [atomic_weight = [1,12,16,14,39,32];]
*and we assume molecular weight = 0
*next to create the two forloop first is total length of species and second one is total length of atomic weight
*the i use strcmp command to compare the each character of species with the atom array
*if command is condition that it calculate the molecular mass by adding molecular mass (intialise as 0)
*atomic weight formula (molecular_weight+ atomic_weight(n)
*while comparing if we get numeric value in species name and we save it different variables x
*in order to calculate the molecular weight we use if command so that values is greate then 1,it will take the 1'stcharacter to add multiple times as that numeric values in atomic weight
*after getting molecular weight we use
sprintf('the molecular weight of %s is %d',species,molecular_weight)
*RESULTS
*we extracted the 14 coefficients of each species
*molecular weight can be calculate