Challenge

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

clear all {clear all datas in workspace}
close all {close all the plots if opened in background}
clc {clear the command window}
Then I wrote
f1 = fopen('NASA.dat','r'); {fopen is used to open file & inside bracket there is file name 'NASA.dat', "I basically renamed thermo.dat file to NASA.dat" and placed where all file are in matlab & finally stored in f1}
Then I wrote
fgetl(f1); {this cmd is used to get one line when executed, if again executed it will give next upcoming line & so on}
\\ when you look NASA.dat file the first line is header info so by this cmd I skipped it\\
Then I wrote
t_line = fgetl(f1); {as already mentioned each times this cmd is executed it will give successive line & I have stored in t_line}
\\ the next line in file is info about global temperature & we need in our code\\
Then I wrote
a = strsplit(t_line,' '); {strsplit is used to split the string & in bracket it is given t_line which is to be considered to be splitten after that I have given space which basically tells from where it is to be splitten, then stored in 'a'}
c
Low_GT = str2double(a{2}); {str2double is used to convert string to cell thatswhy for cell curly bracket is used, so here 1st element of matrix is stored in Low_GT}
Mid_GT = str2double(a{3});
High_GT = str2double(a{4});
{here the 2nd & 3rd element of matrix is stored in Mid_GT & High_GT respectively}
after that further writting
fgetl(f1);
fgetl(f1);
fgetl(f1);
{here I further skipped 3 lines of file by using above steps }
further I wrote
for i = 1:53 {I started for loop with i= 1st element to last species i.e 53th}
t_line2 = fgetl(f1) {this line I provided basically to go on the species i.e where we confine our studies for each loop}
s = strsplit(t_line2,' '); {this line I used to split line from space so basically it will split each component in matrix}
species_name = s{1}; {I have assigned 1st element of s to species_name & here I used curly bracket because it is a cell array}
further I wrote
A = strfind(t_line2,'G'); {this line find G in t_line2 and stored in matrix form in A}
temp_values = t_line2(A+4:length(t_line2)); {In this line actually I stored full local temp from file to temp_line}
B = strsplit(temp_values,' '); {further I used strsplit cmd to split the temp_values in matrix & stored in B}

further I wrote

Local_Low_GT = str2double(B{1});
Local_Mid_GT = str2double(B{2});
Local_High_GT = str2double(B{3});
t_line = fgetl(f1); {its function already discussed}
{above 3 lines are the local temp with low mid & high values of it, in above all I used str2double which converts string to number with precision and B{1} B{2} B{3} are element of cell matrix respectively}
then I wrote
b = findstr(t_line,'E'); {this cmd already discussed in this line I found E and stored in b matrix}
b1 = t_line(1:b(1)+3); {this line stores first coefficient of species}
b1 = str2num(b1); {this line converts string to number and updates 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);
\\As discussed above extraction of 1st coefficient i.e b1, similarly for all other coefficient extraction the formula is same & method is also same so the explanation is also same, just the difference is position of coefficient that is from where the coefficient lies
line2 = fgetl(f1); % Line 2 {here I have moved to next line i.e 2nd line by using fgetl cmd}
6 = 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
b11 = line3(1:b(1)+3); {here I have moved to next line i.e 3rd line by using fgetl cmd}
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);
\\ plz note that explanation of coefficient b6 to b14 is exactly same as explained earlier\\
then I wrote
R = 8.314; {R is universal gas constant whose value is already assigned to it}
t = linspace(Local_Low_GT,Local_High_GT,100); {here basically I have created 100 values of equal interval from Local_Low_GT to Local_High_GT}
then I wrote
Cp = specific_heat(b1,b2,b3,b4,b5,b8,b9,b10,b11,b12,Local_Low_GT,Local_Mid_GT,Local_High_GT,t,R);
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);
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);
{above 3 are function which is named as Cp, H, S in which coefficients which was extracted & Global temp (both), gas const are used more about function I will discuss saperately}
further I wrote
mkdir(['C:\Users\Md Zafar Hayat\OneDrive\Documents\MATLAB\Temp Files'],species_name)
cd(species_name)
{mkdir is used to make new folder the first part before comma shows the path of file and 2nd part shows name of folder & cd cmd is used to change current folder}
further I wrote
figure(1) {this cmd enables me to make saperate distinct figure of each plot}
plot(t,Cp,'linewidth',2,'color','g') {plotting the variable i.e t and cp with color green and linewidth of 2}
grid on {turning on grid line on plot}
xlabel('Temperature') {lebelling the x axis}
ylabel('Specific Heat (J/K)') {lebelling the x axis}
title('Temperature vs Specific Heat') {this cmd shows title on plots}
image = getframe(gcf); {getframe(gcf) cmd captures the axes at the same size that it appears on the screen. It does not capture tick labels or other content outside the axes outline. & 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(image.cdata,'Temperature vs Cp.png') {this whole cmd is used to name the image which is given after comma
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.}
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')
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')
\\similar explanation holds good for figure(2) and figure(3) because both contains same line as in figure(1)\\
further I wrote
saveas(figure(1),'Cp vs. T','jpg')
saveas(figure(2),'S vs. T','jpg')
saveas(figure(3),'H vs. T','jpg')
cd ..
{above 3 lines is used to save the image to folder with each figure have specific name as mentioned}
then I wrote
[M] = Mol_Wt(species_name); {it is a function which basically i have stored in matrix form more about it later}
K = sprintf('%s Weight = %f',species_name,M); {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.}
disp(K) {displaying K values}
end {end of for loop}
\\programming coding is shown below\\
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
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.