Project 1 - Parsing NASA thermodynamic data
Our objective:- is 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
Plan of approach:- For extracting 14 coeff of all species, my plan of action was to make code of 1st element with all the calculations like enthalpy,entropy,specific heat & mol. wt & directory making with facility of saving plots within it then placed whole code inside for loop.
So, when for loop run it will again go to 2nd species then 3rd species and so on... and for loop is executed the no. of times the species are there.
In this way we can extract all species co-eff we want.
\\ if I don't use for loop then the programme will be very much bulkier and time consuming, also too much tiredless effort would be needed because I have to write code for each individual species.\\
Explanation:-
First of all I started with
further I wrote
gcf returns the current figure handle. If a figure does not exist, then gcf creates a figure and returns its handle. You can use the figure handle to query and modify figure properties}imwrite(A,filename) writes image data A to the file specified by filename, inferring the file format from the extension. imwrite creates the new file in your current folder.}sprintf(formatSpec,A1,...,An) formats the data in arrays A1,...,An using the formatting operators specified by formatSpec and returns the resulting text in str. The sprintf function formats the values in A1,...,An in column order. If formatSpec is a string, then so is the output str. Otherwise, str is a character vector.}clear all
close all
clc
f1 = fopen('NASA.dat','r');
fgetl(f1); % Header information
t_line = fgetl(f1); % Global Temperature
a = strsplit(t_line,' ');
Low_GT = str2double(a{2});
Mid_GT = str2double(a{3});
High_GT = str2double(a{4});
fgetl(f1);
fgetl(f1);
fgetl(f1);
% Reading the species in the data file
% Line 1
for i = 1:53
t_line2 = fgetl(f1);
s = strsplit(t_line2,' ');
species_name = s{1};
A = strfind(t_line2,'G');
temp_values = t_line2(A+4:length(t_line2));
B = strsplit(temp_values,' ');
% Local Temperature
Local_Low_GT = str2double(B{1});
Local_Mid_GT = str2double(B{2});
Local_High_GT = str2double(B{3});
t_line = fgetl(f1);
b = findstr(t_line,'E');
% Coefficients 1 to 5
b1 = t_line(1:b(1)+3);
b1 = str2num(b1);
b2 = t_line(b(1)+4:b(2)+3);
b2 = str2num(b2);
b3 = t_line(b(2)+4:b(3)+3);
b3 = str2num(b3);
b4 = t_line(b(3)+4:b(4)+3);
b4 = str2num(b4);
b5 = t_line(b(4)+4:b(5)+3);
b5 = str2num(b5);
line2 = fgetl(f1); % Line 2
% Coefficients 6 to 10
b6 = line2(1:b(1)+3);
b6 = str2num(b6);
b7 = line2(b(1)+4:b(2)+3);
b7 = str2num(b7);
b8 = line2(b(2)+4:b(3)+3);
b8 = str2num(b8);
b9 = line2(b(3)+4:b(4)+3);
b9 = str2num(b9);
b10 = line2(b(4)+4:b(5)+3);
b10 = str2num(b10);
line3 = fgetl(f1); % Line 3
% Coefficients 11 to 14
b11 = line3(1:b(1)+3);
b11 = str2num(b11);
b12 = line3(b(1)+4:b(2)+3);
b12 = str2num(b12);
b13 = line3(b(2)+4:b(3)+3);
b13 = str2num(b13);
b14 = line3(b(3)+4:b(4)+3);
b14 = str2num(b14);
R = 8.314; % Universal Gas Constant
t = linspace(Local_Low_GT,Local_High_GT,100);
% Import Specific Heat Function
Cp = specific_heat(b1,b2,b3,b4,b5,b8,b9,b10,b11,b12,Local_Low_GT,Local_Mid_GT,Local_High_GT,t,R);
% Import Enthalpy Function
H = enthalpy(b1,b2,b3,b4,b5,b6,b8,b9,b10,b11,b12,b13,Local_Low_GT,Local_Mid_GT,Local_High_GT,t,R);
% Import Entropy Function
S = entropy(b1,b2,b3,b4,b5,b7,b8,b9,b10,b11,b12,b14,Local_Low_GT,Local_Mid_GT,Local_High_GT,t,R);
% Create folders for each species
mkdir(['C:\Users\Md Zafar Hayat\OneDrive\Documents\MATLAB\Temp Files'],species_name)
cd(species_name)
% T(Temperature) vs Cp(Specific Heat) plot
figure(1)
plot(t,Cp,'linewidth',2,'color','g')
grid on
xlabel('Temperature')
ylabel('Specific Heat (J/K)')
title('Temperature vs Specific Heat')
image = getframe(gcf);
imwrite(image.cdata,'Temperature vs Cp.png')
% T(Temperature) vs S(Entropy) plot
figure(2)
plot(t,S,'linewidth',2,'color','r')
grid on
xlabel('Temperature')
ylabel('Entropy (J/K)')
title('Temperature vs Entropy')
image = getframe(gcf);
imwrite(image.cdata,'Temperature vs S.png')
% T(Temperature) vs H(Enthalpy) plot
figure(3)
plot(t,H,'linewidth',2,'color','b')
grid on
xlabel('Temperature')
ylabel('Entha1py (J/Kg)')
title('Temperature vs Enthalpy')
image = getframe(gcf);
imwrite(image.cdata,'Temperature vs H.png')
saveas(figure(1),'Cp vs. T','jpg')
saveas(figure(2),'S vs. T','jpg')
saveas(figure(3),'H vs. T','jpg')
cd ..
% Molecular Weight Calculation
[M] = Mol_Wt(species_name);
K = sprintf('%s Weight = %f',species_name,M);
disp(K)
end
EXPLAINING THE ENTHALPY FUNCTION
As formula is given in question so I have basically divided into 2 categories 1st if t>Local_Mid_GT & t<Local_High_GT
for accomplishing it I have written H_Value followed by formula given accordingly here all b is coefficient t is temp R is gas constant I have written if statement for t>Local_Mid_GT & else statement for other conditions
function H_Value = enthalpy(b1,b2,b3,b4,b5,b6,b8,b9,b10,b11,b12,b13,Local_Low_GT,Local_Mid_GT,Local_High_GT,t,R)
if (t>Local_Mid_GT)
H_Value = (b1)+((b2.*t)./2)+((b3.*(t.^2))./3) +((b4.*(t.^3))./4)+((b5.*(t.^4))./5)+(b6./t).*(R*t);
else
H_Value = (b8)+((b9.*t)./2)+((b10.*(t.^2))./3) +((b11.*(t.^3))./4)+((b12.*(t.^4))./5)+(b13./t).*(R*t);
end
end
EXPLAINING THE ENTROPY & CP FUNCTION
Both are also same as enthalpy explanation because I have used same concept as in enthalpy i.e t>Local_Mid_GT and t<Local_Mid_GT, in both functions if and else statement is used same as i have used in enthalpy function just the difference is the formula of entropy & specific heat differs than enthalpy which is alredy mentioned in challenge.
function S_Value = entropy(b1,b2,b3,b4,b5,b7,b8,b9,b10,b11,b12,b14,Local_Low_GT,Local_Mid_GT,Local_High_GT,t,R)
if (t>Local_Mid_GT)
S_Value = ((b1.*log(t))+(b2.*t)+((b3.*(t.^2))/2) +((b4.*(t.^3))/3)+((b5.*(t.^4))/4)+(b7))
else
S_Value = ((b8.*log(t))+(b9.*t)+((b10.*(t.^2))/2) +((b11.*(t.^3))/3)+((b12.*(t.^4))/4)+(b14))*R;
end
end
function Cp_Value = specific_heat(b1,b2,b3,b4,b5,b8,b9,b10,b11,b12,Local_Low_GT,Local_Mid_GT,Local_High_GT,t,R)
if (t>Local_Mid_GT)
Cp_Value = R*(b1+(b2*t)+(b3*(t.^2))+(b4*(t.^3))+(b5*(t.^4)))*R;
else
Cp_Value = R*(b8+(b9*t)+(b10*(t.^2))+(b11*(t.^3))+(b12*(t.^4)))*R;
end
end
EXPLAINING THE MOL WT. FUNCTION
Here I have make function with name of Mol_Wt and input is species_name, starting with Weight=0 then I have created elements array with H C N O A, then I created mass array with 1.008 12.011 14.007 15.999 39.948, respectively after that I started for loop with i=1 to length(species_name), in that loop only I started one more for loop with j=1 to length(elements) i.e 5, so here basically i run from 1 to length of species name and at that point j loop runs from 1 to length of elements, there is one more statement i.e if statement here strcmpi cmd is there which compare two things thats what I did here, so take example i run from 1 to hydrogen then j run from 1 & at 1st position hydrogen is there so it matches weight is mass at j in example case it is 1.008 so it will return 1.008 finally j is assigned to y then j loop ends there. the 2nd statement is when i & j not matchs then it will go number variable where it will take what is the number of atom is there then again i putted if cmd where i have assigned weight adding mass(y)*(number-1) then again stores back to weight finally mole wt is weight which is basically what our aim was.
function Mole_Wt = Mol_Wt(species_name)
Weight = 0;
elements = ['H','C','N','O','A'];
mass = [1.008,12.011,14.007,15.999,39.948];
for i = 1:length(species_name)
for j = 1:length(elements)
if strcmpi(species_name(i),elements(j))
Weight = Weight+mass(j);
y = j;
end
end
number = str2double(species_name(i));
if (number>1)
Weight = Weight+(mass(y)*(number-1));
end
Mole_Wt = Weight;
end