Project 1 - Parsing NASA thermodynamic data
Explaining the code
1.start with clear all close all clc and clear the windows
2. using the fopen command with r to read the data file
3.using the fgetl command we extract the header and the comment lines
4.using str2num convert the strings to number
5.using the for loop the 53 species names and their coefficients are extracted
6.the strfind command is used to get the coefficient E which is a referrence for coefficient values
7.mkdir is used to create a folder for saving the plots
8.A range of temperatures are assumed to calculate entropy enthalpy and specific heat
9.plot command is used to plot the calculated parameters are plotted against the correspomding value of temperature
10.file directory is changed and saveas command is used to save these files
11.then the molecular mass is calculated
13.then the file is closed
errors faced
1.error while converting string to number
2.inproper use of parentheses
3.not defining molecular mass function
4.problem while returning back to directory
steps taken to solve these errors are
1.used appropriate keyword to define the command
2.used parentheses when required
3.defined molecular mass function
4.used cd command to return to the required directory
function M=molecular_mass(species_name)
species=upper(species_name);
constituents=['H','C','N','O','A'];
atomic_weight=[1.00794 12.0107 14.0067 15.9994 39.948]
M=0;
for i=1:length(species)
for j=1:length(constituents)
if strcmp(species(i),constituents(j))
M=M+atomic_weight(j);
u=j;
end
end
n=str2num(species(i));
if n>1
M=M+atomic_weight(u)*(n-1);
end
end
end
clear all
close all
clc
%open and reading the given data
file=fopen('thermo.dat','r')
%headings and global temperatures value
line1=fgetl(file)
line2=fgetl(file)
%three comment lines are included in the file
comment_1=fgetl(file)
comment_2=fgetl(file)
comment_3=fgetl(file)
A=strsplit(line2,' ');
global_low_temperature=str2num(A{2})
global_mid_temperature=str2num(A{3})
global_high_temperature=str2num(A{4})
%species information
for i=1:53
species=fgetl(file)
species_name=species(1:6)
line_1=fgetl(file);
l1=strfind(line_1,'E');
c1=str2num(line_1(1:(l1(1)+3)));
c2=str2num(line_1((l1(1)+4):(l1(2)+3)));
c3=str2num(line_1((l1(2)+4):(l1(3)+3)));
c4=str2num(line_1((l1(3)+4):(l1(4)+3)));
c5=str2num(line_1((l1(4)+4):(l1(5)+3)));
line_2=fgetl(file);
l2=strfind(line_2,'E');
c6=str2num(line_2(1:(l2(1)+3)));
c7=str2num(line_2((l2(1)+4):(l2(2)+3)));
c8=str2num(line_2((l2(2)+4):(l2(3)+3)));
c9=str2num(line_2((l2(3)+4):(l2(4)+3)));
c10=str2num(line_2((l2(4)+4):(l2(5)+3)));
line_3=fgetl(file);
l3=strfind(line_3,'E');
c11=str2num(line_3(1:(l3(1)+3)));
c12=str2num(line_3((l3(1)+4):(l3(2)+3)));
c13=str2num(line_3((l3(2)+4):(l3(3)+3)));
c14=str2num(line_3((l3(3)+4):(l3(4)+3)));
%creating folder for each species
mkdir(['E:\MAT LAB course\file parsing NASA thermodat\species\',species_name]);
%assuming temperatures for calculations
temperature=linspace(300,5000,100);
%universal gas constant
R=8.134;
%calculating properties based on upper and lower temperatures
for j=1:length(temperature);
if temperature(j)<3000
Cp=(c1+c2.*temperature+c3.*temperature.^2+c4.*temperature.^3+c5.*temperature.^4).*R;
H=(c1+(c2.*temperature)./2+(c3.*temperature.^2)/3+(c4.*temperature.^3)/4+(c5.*temperature.^4/5)+c6./temperature).*temperature.*R;
S=(c1*log(temperature)+c2.*temperature+(c3.*temperature.^2)/2+(c4.*temperature.^3)/3+(c5.*temperature.^4)/4+c7).*R;
else
Cp=(c8+c9.*temperature+c10.*temperature.^2+c11.*temperature.^3+c12.*temperature.^4).*R;
H=(c8+(c9.*temperature)./2+(c10.*temperature.^2)/3+(c11.*temperature.^3)/4+(c12.*temperature.^4/5)+c13./temperature).*temperature.*R;
S=(c8*log(temperature)+c9.*temperature+(c10.*temperature.^2)/2+(c11.*temperature.^3)/3+(c12.*temperature.^4)/4+c14).*R;
end
end
%plotting
%temperature vs entropy
figure(1)
plot(temperature,S,'linewidth',4,'color','b')
title('species_name')
xlabel('temperature')
ylabel('entropy')
pause (0.1)
%plotting
%temperature vs entropy
figure(2)
plot(temperature,H,'linewidth',4,'color','k')
title('species_name')
xlabel('temperature')
ylabel('enthalpy')
pause (0.1)
%temperature vs apecific heat
figure(3)
plot(temperature,Cp,'linewidth',4,'color','r')
title('species_name')
xlabel('temperature')
ylabel('specific heat')
pause (0.1)
%changing the file plots to save the plots
directory=(['E:\MAT LAB course\file parsing NASA thermodat\species\',species_name]);
cd(directory)
%saving the plots
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific Heat.jpg');
%switching back to read thermo.dat file to calculate molecular mass calculation
cd('E:\MAT LAB course\file parsing NASA thermodat\')
M=molecular_mass(species_name)
end
fclose(file)