Project 1 - Parsing NASA thermodynamic data
Aim:
To write a program for prasing the NASA's thermodynamic data and also to plot the various graph on various chemical components like oxygen, nitrogen, and carbon di-oxide,etc., also to find the molecular weight of each of the components.
Solution:
By using the below governing equation the blow codes are made, which is given in the challenge itself.
Step by step explaination of code:
1. First as usual close all variables in the workspace of other codings and also to clear the command window
2. Then by using a command 'fopen' open the THERMO.dat and assined in a variable as thermal_data from this the parsing the NASA thermodynamic data starts.
3. Then Assigning the glogla data like global temperature range, global low temperature, global high temperature, and global medium temperature by using input file lines.
4. Then by using fgetl (whch is used for Read line from file, removing newline characters) read the lines from the THERMO.dat and assigned in a variable in k from the range of 1 to 3
5. Then by using the same thermal data extracting them by line by line in the for of high to low wih a range of 1:53
6. Here we are going to find the 14 co-efficient and also to calculate the Entropy, enthalpy and specifi heat from the given data.
7. Then by assigning the data by with a range from the previous calculation is done and they are assigned in different variables.
8. Then by using plot command the results are ploted and spintf and saveas command the figure captured is saved in their corresponding file.
9. Then by using two different function we are going to calculate the molecular weight and entropy and enthalpy and also specific weight also from the function.
10. Then also the results are attached as required in the challenge.
Code 1: which shows the function for calculation on specific heat, Enthalpy and Entorpy
%calculation for specific heat, Enthalpy and Entropy
function [c_p,H,S] = property(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,glo_mid_T)
R = 8.314;
for q = 1:length(T)
if T(q) >= glo_mid_T
c_p =(A1 + (A2.*T)+(A3.*(T.^2))+(A4.*(T.^3))+(A5.*(T.^4))).*R ;
H = (A1+(A2.*(T./2))+(A3.*(T.^2./3))+(A4.*(T.^3./4))+(A5.*(T.^4./5))+(A6./T)).*(R.*T);
S = (A1.*log(T)+(A2.*T)+(A3.*(T.^2./2))+(A4.*(T.^3./3))+(A5.*(T.^4./4))+ A7).*R;
else
c_p =(a1 + (a2.*T)+(a3.*(T.^2))+(a4.*(T.^3))+(a5.*(T.^4))).*R ;
H = (a1+(a2.*(T./2))+(a3.*(T.^2./3))+(a4.*(T.^3./4))+(a5.*(T.^4./5))+(a6./T)).*(R.*T);
S = (a1.*log(T)+(a2.*T)+(a3.*(T.^2./2))+(a4.*(T.^3./3))+(a5.*(T.^4./4))+ a7).*R;
end
end
end
Code 2: which shows the function for calculation on Molecuar weight
%calculation for Molecular weight
function MW = molecular_weight(species)
atoms = {'H','N','O','C','A'};
atomic_weight = [1 14 16 12 40];
MW = 0 ;
for i = 1:length(species)
for j = 1:length(atomic_weight)
if (strcmp(species(i),atoms(j))==1)
MW = MW+atomic_weight(j);
rank(j);
end
end
m = str2num(species(i));
if m<1
n = m-1;
MW = MW + atomic_weight(rank)*(n);
end
end
end
Code 3: which shows the over all code to find the plot on different components
close all
clc
%Parsing thermodynamic data - importing data from data file
thermal_data = fopen('THERMO.dat','r');
f_name = fgetl(thermal_data);
f_line = fgetl(thermal_data);
%global data input
global_temp_range = strsplit(f_line,' ');
g_l_t = str2num(global_temp_range{2});
g_m_t = str2num(global_temp_range{3});
g_h_t = str2num(global_temp_range{4});
for i = 1:3
k = fgetl(thermal_data);
end
%importing all the thermal data and creating plot from the data
for j = 1:53
line_1 = fgetl(thermal_data);
L1 = strsplit(line_1,' ');
species = (L1{1});
G = strfind(line_1,'G ');
LlT = str2num(line_1(G+3:G+10)); %extraction of temperature [high medium low ans also a range]
LhT = str2num(line_1(G+12:G+20));
LmT = str2num(line_1(G+22:G+30));
T = linspace(LlT,LhT,500);
line_2 = fgetl(thermal_data); %assigning the constant like A1,A2,A3 etc., from the thermo.data
L2 = strfind(line_2,'E');
A1 = str2num(line_2(1:L2(1)+3));
A2 = str2num(line_2(L2(1)+4:L2(2)+3));
A3 = str2num(line_2(L2(2)+4:L2(3)+3));
A4 = str2num(line_2(L2(3)+4:L2(4)+3));
A5 = str2num(line_2(L2(4)+4:L2(5)+3));
line_3 = fgetl(thermal_data);
L3 = strfind(line_3,'E');
A6 = str2num(line_3(1:L3(1)+3));
A7 = str2num(line_2(L3(1)+4:L3(2)+3));
a1 = str2num(line_2(L3(2)+4:L3(3)+3));
a2 = str2num(line_2(L3(3)+4:L3(4)+3));
a3 = str2num(line_2(L3(4)+4:L3(5)+3));
line_4 = fgetl(thermal_data);
L4 = strfind(line_4,'E');
a4 = str2num(line_4(1:L4(1)+3));
a5 = str2num(line_4(L4(1)+4:L4(2)+3));
a6 = str2num(line_4(L4(2)+4:L4(3)+3));
a7 = str2num(line_4(L4(3)+4:L4(4)+3));
%function that calculates [c_p,H,S]
%c_p = Specific heat
%H = Enthalpy
%S = Entropy
[c_p,H,S] = property(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,G_mid_T);
Folder_name = sprintf('%d) [%s]',j, species);
current_dir = pwd ;
mkdir(Folder_name);
cd(Folder_name);
%creating plot and saving them in corresponding seperate file
figure (1)
plot(T,c_p,'r','linewidth',3)
grid on
xlabel(' Local Temperature');
ylabel(' Specific Heat')
title(' Specific Heat(Cp) Vs Temperature(T) ')
IMAGE1 = sprintf(" Cp Vs T of [%s].png",species);
saveas(gca,IMAGE1)
figure (2)
plot(T,H,'b','linewidth',3)
grid on
xlabel(' Local Temperature');
ylabel(' Enthalpy')
title(' Enthalpy(H) Vs Temperature(T) ')
IMAGE2 = sprintf(" H Vs T of [%s].png",species);
saveas(gca,IMAGE2)
figure (3)
plot(T,S,'g','linewidth',3)
grid on
xlabel(' Local Temperature');
ylabel(' Entropy')
title(' Entropy(S) Vs Temperature(T) ')
IMAGE3 = sprintf(" S Vs T of [%s].png",species);
saveas(gca,IMAGE3)
%function that calculate molecular weight of the chemical component
cd(current_dir);
MW = molecular_weight(species);
fprintf(' %d) Molecular Weight of [%s] is %d.',j,species,MW)
fprintf('n n')
end
Error Faced:
While coding the main code i see a red line in str2num that it shows to change that command to str2double and just i tried out it and while running this i faced a error that there is an undefined function on fgetl in double then i go back to the command str2num and commands are explained as follow, also the error is shown below.
Plot on oxygen (O2)
Plot 1
Plot 2
Plot 3
Plot on Nitrogen(N2)
Plot 1
Plot 2
Plot 3
Plot on Carbon di oxide(CO2)
Plot 1
Plot 2
Plot 3
Ploted graphs are stored in different files and their location are shown as screen short as follow.
Commands and their explaination:
Str2num --Convert character array or string to numeric array
Str2Double--Convert strings to double precision values
strcmp -- Compare string
fopen-- Open file
fprintf-- Write a data to a text file
fgetl--Read line from file, removing newline characters
Strsplit--Split string or character vector at specified delimiter
Strfind--Find strings within other strings
Result:
Finally by using matlab the given data file is parsed and the given specified variables are calculated and plotes are made and they are presented.