Challenge

Project 1 - Parsing NASA thermodynamic data

1. 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. The formulae are shown below.

Here R is the universal gas constant, T is the temperature

PROGRAM TO CALCULATE THE ABOVE GIVEN PARAMETERS:

clear all 
close all
clc

f1= fopen('THERMO.dat');
for i=1:5
fgetl(f1);
end


for i=1:53
    
X=fgetl(f1);
Y= strsplit(X,' '); %splitting the line 
n= length(Y); 
species_name= Y{1}; % invoking the elements
local_low_temp= str2num(Y{n-3});%converting string to number for temperature

local_mid_temp= str2num(Y{n-1});%converting string to number for temperature

local_high_temp= str2num(Y{n-2});%converting string to number for temperature

%extracting the coefficient from the given data
A= fgetl(f1);
a= findstr(A,'E');
a1= A(1:a(1)+3);
a1= str2num(a1);
a2= A(a(1)+4:a(2)+3);
a2= str2num(a2);
a3= A(a(2)+4:a(3)+3);
a3= str2num(a3);
a4= A(a(3)+4:a(4)+3);
a4= str2num(a4);
a5= A(a(4)+4:a(5)+3);
a5= str2num(a5);

B= fgetl(f1);
b= findstr(B,'E');
a6= A(1:b(1)+3);
a6= str2num(a6)
a7= A(b(1)+4:b(2)+3);
a7= str2num(a7);
a8= A(b(2)+4:b(3)+3);
a8= str2num(a8);
a9= A(b(3)+4:b(4)+3);
a9= str2num(a9);
a10= A(b(4)+4:b(5)+3);
a10= str2num(a10);
 
C= fgetl(f1);
c= findstr(C,'E');
a11= A(1:b(1)+3);
a11= str2num(a11);
a12= A(b(1)+4:b(2)+3);
a12= str2num(a12);
a13= A(b(2)+4:b(3)+3);
a13= str2num(a13);
a14= A(b(3)+4:b(4)+3);
a14= str2num(a14);

R= 8.314; %universal gas constant

temp= linspace(local_low_temp,local_high_temp,150);
cp= specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,R,temp,local_mid_temp);
H= enthalphy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,R,local_mid_temp);
S= entrophy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,R,local_mid_temp);

%invoking the function to calculate molecular weight
M= molecular_trial(species_name);
sprintf('molecular_weight of %s = %d',species_name,M)

mkdir(species_name) % made the directory
cd(species_name)    % entering the created directory

figure(1)
plot(temp,cp,'linewidth',4)
xlabel('TEMPERATURE')
ylabel('SPECIFIC HEAT')
title('TEMPERATURE vs SPECIFICHEAT')
saveas(1,'temp_vs_cp','jpg')  % saving the figures

figure(2)
plot(temp,H,'linewidth',4, 'Color','g')
xlabel('TEMPERATURE')
ylabel('ENTHALPHY')
title('TEMPERATURE vs ENTHALPHY')
saveas(2,'temp_vs_H','jpg')

figure(3)
plot(temp,S,'linewidth',4,'Color','r')
xlabel('TEMPERATURE')
ylabel('ENTROPHY')
title('TEMPERATURE vs ENTROPHY')
saveas(3,'temp_vs_S','jpg')

cd ..  %used for returning back to the main folder
end

fclose(f1)



DETAILS OF FUNCTIONS USED IN THE PROGRAM :

FUNCTION TO CALCULATE SPECIFIC HEAT:

function cp= specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,R,temp,local_mid_temp)
if temp < local_mid_temp
cp= (a1+(a2*temp)+(a3*temp.^2)+(a4*temp.^3)+(a5*temp.^4))*R;
else
    cp= (a6+(a7*temp)+(a8*temp.^2)+(a9*temp.^3)+(a10*temp.^4))*R;
end


end

FUNCTION TO CALCULATE ENTROPHY:

function S= entrophy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a13,a14,temp,R,local_mid_temp)
if temp < local_mid_temp
S= (a1*log(temp))+(a2*temp)+(a3*(temp.^2)/2)+(a4*(temp.^2)/2)+(a5*(temp.^2)/2)+a7;
else

S= (a8*log(temp))+(a9*temp)+(a10*(temp.^2)/2)+(a11*(temp.^2)/2)+(a13*(temp.^2)/2)+a14;

end

FUNCTION TO CALCULATE ENTHALPHY:

function H= enthalphy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,R,local_mid_temp)

if temp < local_mid_temp
H= R*temp.*(a1+(a2*(temp/2)+ (a3*(temp.^2)/3)+ (a4*(temp.^3)/4)+ (a5*(temp.^4)/5)+ (a6./temp)));

else
H= R*temp.*(a7+(a8*(temp/2)+ (a9*(temp.^2)/3)+ (a10*(temp.^3)/4)+ (a11*(temp.^4)/5)+ (a12./temp)));

end

PLOTS:

O2

N2

CO2

CALCULATE MOLECULAR WEIGHT OF EACH SPECIES

function:

function W= molecular_trial(species_name)
atomic_names= ['O','H','N','A','C']; %providing the elements involved in the species

atomic_weights=[16,1,14,40,12]; %providing the corresponding atomic number for each element
W=0;
for i=1:length(species_name)
    for j=1:length(atomic_names)
        if species_name(i)==atomic_names(j)
            W=W+ atomic_weights(j);
            position=j;
        end
    end
    n=str2num(species_name(i));
    if n>=1
        W= W+atomic_weights(position)*(n-1);
    end
end

OUTPUT(from command window)

a6 =

2.5694


ans =

'molecular_weight of O = 16'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.2825


ans =

'molecular_weight of O2 = 32'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.5000


ans =

'molecular_weight of H = 1'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.3373


ans =

'molecular_weight of H2 = 2'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.0929


ans =

'molecular_weight of OH = 17'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.0340


ans =

'molecular_weight of H2O = 18'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

4.0172


ans =

'molecular_weight of HO2 = 33'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

4.1650


ans =

'molecular_weight of H2O2 = 34'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.4927


ans =

'molecular_weight of C = 12'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.8785


ans =

'molecular_weight of CH = 13'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.8741


ans =

'molecular_weight of CH2 = 14'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.2920


ans =

'molecular_weight of CH2(S) = 14'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.2857


ans =

'molecular_weight of CH3 = 15'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

0.0749


ans =

'molecular_weight of CH4 = 16'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.7152


ans =

'molecular_weight of CO = 28'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.8575


ans =

'molecular_weight of CO2 = 44'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.7722


ans =

'molecular_weight of HCO = 29'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

1.7607


ans =

'molecular_weight of CH2O = 30'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.6927


ans =

'molecular_weight of CH2OH = 31'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.7708


ans =

'molecular_weight of CH3O = 31'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

1.7897


ans =

'molecular_weight of CH3OH = 32'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.1678


ans =

'molecular_weight of C2H = 25'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

4.1476


ans =

'molecular_weight of C2H2 = 26'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.0167


ans =

'molecular_weight of C2H3 = 27'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.0361


ans =

'molecular_weight of C2H4 = 28'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

1.9547


ans =

'molecular_weight of C2H5 = 29'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

1.0719


ans =

'molecular_weight of C2H6 = 30'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

4.5113


ans =

'molecular_weight of CH2CO = 42'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.6282


ans =

'molecular_weight of HCCO = 41'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.9238


ans =

'molecular_weight of HCCOH = 42'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.2097


ans =

'molecular_weight of H2CN = 28'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.8022


ans =

'molecular_weight of HCN = 27'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.9793


ans =

'molecular_weight of HNO = 31'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.4159


ans =

'molecular_weight of N = 14'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.7668


ans =

'molecular_weight of NNH = 29'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

4.8231


ans =

'molecular_weight of N2O = 44'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.7837


ans =

'molecular_weight of NH = 15'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.8347


ans =

'molecular_weight of NH2 = 16'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.6345


ans =

'molecular_weight of NH3 = 17'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.2606


ans =

'molecular_weight of NO = 30'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

4.8848


ans =

'molecular_weight of NO2 = 46'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

6.5986


ans =

'molecular_weight of HCNO = 43'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.8978


ans =

'molecular_weight of HOCN = 43'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

6.2240


ans =

'molecular_weight of HNCO = 43'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.1522


ans =

'molecular_weight of NCO = 42'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

3.7460


ans =

'molecular_weight of CN = 26'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.8946


ans =

'molecular_weight of HCNN = 41'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.9266


ans =

'molecular_weight of N2 = 28'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

2.5000


ans =

'molecular_weight of AR = 40'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

7.5341


ans =

'molecular_weight of C3H8 = 44'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

7.7027


ans =

'molecular_weight of C3H7 = 43'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.4041


ans =

'molecular_weight of CH3CHO = 44'

Warning: Directory already exists.
> In NASA_parsing (line 72)

a6 =

5.9757


ans =

'molecular_weight of CH2CHO = 43'

IMPORTANT COMMAND USED IN THE PROJECT IN MATLAB:

1. fopen() = to open the data file.

2. fgetl() = to read the line from the file.

3. strsplit()= to split the given line.

4. str2num()= converting string to number.

5. findstr() = searching the strings involved in the sentence.

6. linspace() = to provide the temperature range.

7. mkdir() = to make a directory.

8. cd() = entering into the created directory.
9. cd.. = used for returning back to the main folder.
10. function() = to create a function to calculate the given parameters.
Explanation of coding logic and logic behind choosing aspects to parse out of the file
1.To create a program for parsing the data we have to call the data file by fopen().
2.To skip first 5 line use for loop and read the line by fgetl()
3. Extract the local temperature from the line.
4.To extract the coefficient read the line and the split it by strsplit() and then change the string into a number by str2num() inside a for loop.
5. Create a local temperature range by using linspace command.
6. Calculate the specific heat, enthalphy and entrophy by creating the function.
7. Make a directory by using mkdir(), and enter the directory by using cd().
8.Plot the required graph for temperature v/s the given parameters.
9. save it the directory using saveas().
10. Calculate the molecular weight by creating a function.
11. provide the elements involved in the species into a variable also provide atomic number for the corresponding elements.
12. Create a for loop with if condition to calculate the molecular weight.
NOTE: App store not working in lap so had to take a photo through phone
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.