Project 1 - Parsing NASA thermodynamic data
1.Aim :
- To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file..
- Formulas used :
- Molecular mass is the sum of atomic masses of the elements present in a molecule.
- Objective of the project:
- Calculate the molecular weight of each species and display it in the command window.
- Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
- To Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
- Body of content:
- Open the given data with the help of fopen comment and fgetl to read the line
- By reading the Header Global Temperature are been noted by str2num command which changes the string to number
- For just skipping the commands read and do nothing and move to next
- Next 53 lines are where the data, that need to be extract to calculate the specific heat, enthalpy and entropy.
- Each spices consists of 4 lines , 1st line contains the name of the molecule and local temperature. 2nd,3rd and 4th line consists of the coefficients required
- By using various commands line strfind,str2num,and finding the exact location of the coefficient the each value are store in a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14 respectively
- Local Temperatures(Low and High) are used to calculate various temperature and assign the gas constant, R=8.314 and calculate specific heat, entropy, enthalpy using the function commant
- The function Program is as follows
function [cp, h, s] = cp_h_s_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,mid_t3,T,R)
for i = 1:length(T)
if T(i)>mid_t3
cp = (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)).*T.*R;
s = ((a1.*log(T))+ (a2.*T) +(a3.*T.^2)./2 + (a4.*T.^3)./3 + (a5.*T.^4)./4 + a7).*R;
else
cp = (a8 + (a9.*T) + (a10.*T.^2)+ (a11.*T.^3) + (a12.*T.^4)).*R;
h = (a8 + (a9.*T)./2 + (a10.*T.^2)./3 +(a11.*T.^3)./4+(a12.*T.^4)./5+(a13./T)).*T.*R;
s = ((a8.*log(T))+ (a9.*T) +(a10.*T.^2)./2 + (a11.*T.^3)./3 + (a12.*T.^4)./4 + a14).*R;
end
end
- With Help of function the values for Specific heat, Enthalpy, Entropy are been determined
- For the same data plotting is done for Temperature vs Specific heat, Temperature vs Enthalpy, Temperature vs Entropy.
- Create the new folder for each species to save the plotted graph
- Returns back to the parent folder
- Same process is repeated all the species and store their graph in indiduval folder
- for calculating the molecular mass formula is used with the help of if command and fprintf command is used to print the molecular mass
- the progarm is as follows
close all clear all clc % reading the data file given f1 = fopen('THERMO.dat','r'); fgetl(f1); %Reading the header file h = fgetl(f1); %Read the header line h = strsplit(h,' '); global_low = str2num(h{2}); %defining the global temperature global_mid = str2num(h{3}); global_high = str2num(h{4}); %Skipping next 3 Lines for i = 1:3 fgetl(f1); end % Reading Every line and extarct data using for loop for i = 1:53 first = fgetl(f1); %Read the indiduval data species_head = strsplit(first,' '); molecule = (species_head{1}); %Read the name of the molecules low_t1 = str2num(species_head{end-3}); %Reading local Temperature high_t2 = str2num(species_head{end-2}); mid_t3 = str2num(species_head{end-1}); %reading the co-efficient of molecule line by line coeff_l1 = fgetl(f1); a_l1 = strfind(coeff_l1,'E'); a1 = str2double (coeff_l1(1:a_l1(1)+3)); a2 = str2double (coeff_l1(a_l1(1)+4:a_l1(2)+3)); a3 = str2double (coeff_l1(a_l1(2)+4:a_l1(3)+3)); a4 = str2double (coeff_l1(a_l1(3)+4:a_l1(4)+3)); a5 = str2double (coeff_l1(a_l1(4)+4:a_l1(5)+3)); coeff_l2 = fgetl(f1); a_l2 = strfind(coeff_l2,'E'); a6 = str2num (coeff_l2(1:a_l2(1)+3)); a7 = str2num (coeff_l2(a_l2(1)+4:a_l2(2)+3)); a8 = str2num (coeff_l2(a_l2(2)+4:a_l2(3)+3)); a9 = str2num (coeff_l2(a_l2(3)+4:a_l2(4)+3)); a10 = str2num (coeff_l2(a_l2(4)+4:a_l2(5)+3)); coeff_l3 = fgetl(f1); a_l3 = strfind(coeff_l3,'E'); a11 = str2num (coeff_l3(1:a_l3(1)+3)); a12 = str2num (coeff_l2(a_l3(1)+4:a_l3(2)+3)); a13= str2num (coeff_l2(a_l3(2)+4:a_l3(3)+3)); a14= str2num (coeff_l2(a_l3(3)+4:a_l3(4)+3)); % Reading Local temperature (low and high) T = linspace(low_t1,high_t2,100); % Stating Gas Constant R = 8.314; %Calling the function for Specific Heat,Entropy & Enthalpy [cp h s] = cp_h_s_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,mid_t3,T,R); %Plotting Graph between temperature vs Specific heat, Enthalpy, & Entropy %for Each Molecules figure(1) plot(T,cp,'linewidth',3,'color','r') grid on title ('T vs Sp of',molecule) xlabel('temperature') ylabel('Specificheat') figure(2) plot(T,h,'linewidth',3,'Color','b') grid on title ('T vs H of',molecule) xlabel('temperature') ylabel('Enthalpy') figure(3) plot(T,s,'linewidth',3,'Color','g') grid on title ('T vs S of',molecule) xlabel('temperature') ylabel('Entropy') %Creating a new folder for saving the graph mkdir(['/Users/maheshwaransenthil/Desktop/Matlab Study/file parsing project/',molecule]); %Directed to the created folder save = (['/Users/maheshwaransenthil/Desktop/Matlab Study/file parsing project/',molecule]); cd(save) %Save the graph to its respective folder saveas(1,'Specific Heat.jpg') saveas(2,'Enthalpy.jpg') saveas(3,'Entropy.jpg') %Directing back to the main folder cd('/Users/maheshwaransenthil/Desktop/Matlab Study') % For Calculating the Molecular mass of each Molecule elements = ['H','C','O','N','A','S']; %Stating the atoms in the given Molecule atomic_mass = [1.00784 12.011 15.999 14.0067 39.948 32.065]; %Stating the atomic mass for the same molecular_mass = 0; %Intial input for molecular mass for i = 1:length(molecule) %Using For loop calculating the Molecular mass for j = 1:length(elements) if strcmp(molecule(i),elements(j)) %comparing the elements with molecule molecular_mass = molecular_mass+atomic_mass(j); x=j; end end n = str2num(molecule(i)); if n>1 %if loop is used to calulate homonuclear molecules molecular_mass = molecular_mass+atomic_mass(x)*(n-1); end end fprintf('The Molecular Weight of %s = %f',molecule,molecular_mass); % Printing the results fprintf('\n') end fclose(f1) RESULTS:
- The molecular weight of each species and display it in the command window and Screenshot is been attached along with this
- Graph is Plotted, Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species
- The plots are saved as images with appropriate names and dump in separate folders for each species with suitable name screenshot for the same is attached
- The graph for o2, N2 and Co2 species are attached