Challenge

Project 4 - MATLAB - File Parsing - NASA thermodynamic data.

AIM: Make a Mat Lab code to calculate thermodynamic properties of various gas species & file parse of NASA thermodynamics data.

OBJECTIVE:

Making the function to calculate the enthalpy, entropy and specific heat for all the species in the data are given. Calculating molecular weight of each species and display in command window. Plotting Cp, enthalpy and entropy for a local temperature range saving the plots and images all should be generated automatically screenshot of window of all folders saved. Plots for 02, n2 and Co2 species.

File Parsing:

Parsing file means reading data stream, sorting & building in memory data. Parsing, syntax analysis or syntactic analysis is the process of analysing the string of symbol, in natural language, computer or data structures, conforming to the rules of a formal grammar. Some parsing may generate a parse forest or list of parse for input.

NASA thermodynamics file:

NASA thermodynamics file which is THERMO.dat file which are temperature range and 53 species are given dada and each spe. Contaminates 14 coefficient and temperature range we have to parse these 14 coeff. To find specific heat, entropy and enthalpy using the formula Where R is the universal gas constant, T is the temperature, CP is specific heat (KJ), S is entropy (KJ/mol-k), H is enthalpy ( KJ/mol), a(i) is the coefficient where i = 1 to 14.

Specific heat:

The specific heat is the amount of heat per unit mass required to rise the temperature by one degree Celsius. The relationship between heat and temperature change is defined by c is the specific heat

Cp = R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4))

Entropy:

A measure of the unavailable energy in a closed thermodynamic system that is also usually considered to be a measure of the system's disorder, that is a property of the system's state, and that varies directly with any reversible change in heat in the system and inversely with the temperature of the system.

S = R*(a1.*log(T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(t).^4)/4)+a7)

Enthalpy:

When a process takes place at constant pressure, the heat absorbed or released is equal to the Enthalpy change. Enthalpy is sometimes known as “heat content”, but “enthalpy” is an interesting and unusual word, so most people like to use it. Etymologically, the word “entropy” is derived from the Greek, meaning “turning” and “enthalpy” is derived from the Greek meaning “warming”. As for pronunciation, Entropy is usually stressed on its first syllable, while enthalpy is usually stressed on the second.

H = R*T.*(a1+((a2*T)/2)+((a3*(T).^2)/3)+((a4*(T).^3)/4)+((a5*(T).^4/5)+((a6./T)))

SOLUTION:

Firstly, we will input parameter R and will open the file THERMO.dat and reading this file using fopen command. For reading the THERMO using fget command and then read temperature file and we will spilt the temp. Using strsplit command to convert in cell array and then convert this string to number using str2num command to find low, high and mid temperature.

Then will assign temp. Range by linspace command. Then run loop for skip command lines the data using fgetl command inside the loop. To find the 53 nos. values in file by assigning the fget command. Splitting in the cell array to did species. Run the loop of the temperature in the species line. To read new line, for coefficient in the data file we find first E string using strafing command to find before and after number of the coefficient Then we will find remaining the 14 confident then convert in to number. From that the first 7 coefficient are for higher temperature range and later 7 are the lower temperature range.

Now we have to define the entropy S, enthalpy H, specific heat Cp function s to find these parameters using these 14 coefficient at low and high temp. Then we got the molecular weight W function for finding the molecular weight by fclose command. Then we change the directory folder by cd command and create folder where we have to save the all plots. Now for plotting the plots of specific heat, entropy, enthalpy, temperature for different species. We will use scanf command to in title command to create different species life, finally use the save as command for save the plots in the different folder.

The main program –

clear all

close all

clc

% FOR READING DATA FILE NASA THERMODYNAMICS

% TO OPEN AFILE AND 'r' FOR OPEN THE FILE READING

f1=fopen('THERMO.dat','r')

gt=fgetl(f1);

% OBTAIN TEMPERATURE (BY READING HEADER INFO.)

global_temp=fgetl(f1);

% SPILITNG

t=strsplit(global_temp);

% CONVERTING STRING CELLS TO NUMBER BY - str2num,

global_low_temp=str2num(t{2});

global_medium_temp=str2num(t{3});

global_high_temp=str2num(t{4});

% FOR TEMPERATURE AND GAS CONSTANT

% TEMPERATURE RANG RESUMED

Temp=linspace(global_low_temp,global_high_temp,1000);

%UNIVERSAL GAS CONSTANT J/(mol-K)

R = 8.314;

% FOR SKIPPING THREE COMMENT LINES FROM FILE

for i = 1:3

gt=fgetl(f1);

end

for i = 1:53

gt=fgetl(f1);

A=strsplit(gt,' ');

Species_SP=(A{1})

line_1=fgetl(f1) ;

a=strfind(line_1,'E');

%EXTRACTING HIGHER AND LOWER TEMPERATURE COEFFICIENTS

%FOR EXTRACTING FIRST 7 COEFF. (HIGH TEMPERATURE COEFF.)

a1=line_1(1:a(1)+3);

a1=str2num(a1); %1

a2=line_1(a(1)+4:a(2)+3);

a2=str2num(a2) ; %2

a3=line_1(a(2)+4:a(3)+3);

a3=str2num(a3) ; %3

a4=line_1(a(3)+4:a(4)+3);

a4=str2num(a4); %4

a5=line_1(a(4)+4:a(5)+3);

a5=str2num(a5) ; %5

line_2=fgetl(f1);

b=strfind(line_1,'E');;

a6=line_2(1:b(1)+3);

a6=str2num(a6) ; %6

a7=line_2(a(1)+4:b(2)+3);

a7=str2num(a7); %7

% EXTRACTING SECOND 7 COEFF (LOW TEMP. COEFF.)

a8=line_2(b(2)+4:b(3)+3);

a8=str2num(a8); %8

a9=line_2(b(3)+4:b(4)+3);

a9=str2num(a9) ; %9

a10=line_2(b(4)+4:b(5)+3);

a10=str2num(a10) ; %10

line_3=fgetl(f1);

c=strfind(line_1,'E');

a11=line_3(1:c(1)+3);

a11=str2num(a11); %11

a12=line_3(c(1)+4:c(2)+3);

a12=str2num(a12); %12

a13=line_3(c(2)+4:c(3)+3);

a13=str2num(a13); %13

a14=line_3(c(3)+4:c(4)+3);

a14=str2num(a14); %14

% FOR SPECIFIC HEAT CALCULATION

Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp);

% FOR ENTHALPHY CALCULATION

H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp);

% FOR ENTROPHY CALCULATION

S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp);

% FOR CALCULATING MOLECULAR WEIGHT

Molecularweight(i) = Molecular_weight(Species_SP);

% TO WRITHE AND READ MOLECULAR LIST

S

Molecular_list = fopen('Molecular weight.txt','a+');

fprintf( Molecular_list,'Molecular weight of %s is %d n',Species_SP,Molecularweight);

fclose(Molecular_list);

% FOR PLOTING(CP,H,S,Temp,Species_SP)

% SAVING AS A FOLDER

cur_dir=pwd;

mkdir(Species_SP);

cd(Species_SP);

% THE PLOT 1 - SPECIFIC HEAT (CP) VS TEMPERATURE RANGE (TEMP)

figure(1)

plot(Temp,Cp,'linewidth',2,'color','r');

xlabel('Temperature [K]');

ylabel('Specific Heat [kJ]');

title(sprintf('Varying Specific Heat with Temperature Range of %s',Species_SP));

grid on

saveas(figure(1),'Specific Heat.jpg');

% THE PLOT 2, ENTHALPY (H)VS TEMPERATURE RANGE (TEMP)

figure(2)

plot(Temp,H,'linewidth',2,'color','g');

xlabel('Temperature [K]');

ylabel('Enthalpy in [kJ/(mol)]');

title(sprintf('Varying Enthalpy with Temperature Range of %s',Species_SP));

grid on

saveas(figure(2),'Ethalpy.jpg');

% THE PLOT 3, ENTROPY VS TEMPERATURE RANGE (TEMP)

figure(3)

plot(Temp,S,'linewidth',2,'color','b');

xlabel('Temperature [K]');

ylabel('Entropy in [kJ/(mol-K)]');

title(sprintf('Varying Entropy with Temperature Range of %s',Species_SP));

grid on

saveas(figure(3),'Entropy.jpg');

cd(cur_dir);

% MOLECULAR WEIGHT

M = Molecular_weight(Species_SP)

f2 = fopen('molecular mass','w');

fprintf('%s n %s n %f n','species','Molecular mass',M)

fclose(f2)

end

The function created –

To find the specific heat

To find the specific heat by the function and define it with related formula to find 14 coefficient, gas constant, temperature, mid temperature. Create the condition by use of the loop to make temperature less than and equal to mid temperature to define low temp. Coefficient. It will use the condition to define coefficient of higher temperature coeff. In the formula. Then this function find the specific heat.

The specific heat is the amount of heat per unit mass required to rise the temperature by one degree Celsius. The relationship between heat and temperature change is defined by c is the specific heat

Cp = R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4))

% FUNCTION FOR SPECIFIC HEAT CALCULATION

function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)

% COMPARING WITH GLOBAL TEMPERATURE

% IF LESS THAN GLOBAL TEMPERATURE - MINIMUM COEFFICIENTS ARE TAKEN

if Temp <= global_medium_temp

Cp = R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(Temp).^3) + (a12*(Temp).^4));

else

Cp = R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(Temp).^3) + (a5*(Temp).^4));

% ELSE MAXIMUM COEFFICIEWNT ARE TAKEN

end

To find the Entropy

To find entropy creating the function and assigning its related formula coefficient, gas constant, temperature, mid temperature. Create the condition by use of the loop to make temperature less than and equal to mid temperature to define low temp. Coefficient. It will use the condition to define coefficient of higher temperature coeff. In the formula. Then this function find the entropy.

A measure of the unavailable energy in a closed thermodynamic system that is also usually considered to be a measure of the system's disorder, that is a property of the system's state, and that varies directly with any reversible change in heat in the system and inversely with the temperature of the system.

S = R*(a1.*log(T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(t).^4)/4)+a7)

% FUNCTION FOR ENTROPY CALCULATION

function S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp)

% COMPARING WITH GLOBAL TEMPERATURE

% IF LESS THAN GLOBAL TEMPERATURE - MINIMUM COEFFICIENTS ARE TAKEN

if Temp <= global_medium_temp

S = R.*(a8.*log(Temp) + (a9.*Temp) + (a10*((Temp).^2)/2) + (a11*((Temp).^3)/3) + (a12*((Temp).^4)/4) + a14);

else

% ELSE MAXIMUM COEFFICIEWNT ARE TAKEN

S = R.*(a1.*log(Temp) + (a2.*Temp) + (a3*((Temp).^2)/2) + (a4*((Temp).^3)/3) + (a5*((Temp).^4)/4) + a7);

end

To find the Enthalpy

To find enthalpy creating the function and assigning its related formula coefficient, gas constant, temperature, mid temperature. Create the condition by use of the loop to make temperature less than and equal to mid temperature to define low temp. Coefficient. It will use the condition to define coefficient of higher temperature coeff. In the formula. Then this function find the entropy.

A measure of the unavailable energy in a closed thermodynamic system that is also usually considered to be a measure of the system's disorder, that is a property of the system's state, and that varies directly with any reversible change in heat in the system and inversely with the temperature of the system.

When a process takes place at constant pressure, the heat absorbed or released is equal to the Enthalpy change. Enthalpy is sometimes known as “heat content”, but “enthalpy” is an interesting and unusual word, so most people like to use it. Etymologically, the word “entropy” is derived from the Greek, meaning “turning” and “enthalpy” is derived from the Greek meaning “warming”. As for pronunciation, Entropy is usually stressed on its first syllable, while enthalpy is usually stressed on the second.

H = R*T.*(a1+((a2*T)/2)+((a3*(T).^2)/3)+((a4*(T).^3)/4)+((a5*(T).^4/5)+((a6./T)))

% FUNCTION FOR ENTHALPY CALCULATION

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp)

% COMPARING WITH GLOBAL TEMPERATURE

% IF LESS THAN GLOBAL TEMPERATURE - MINIMUM COEFFICIENTS ARE TAKEN

if Temp <= global_medium_temp

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

else

% ELSE MAXIMUM COEFFICIEWNT ARE TAKEN

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

end

To find the Molecular weight of the different species

First we create the Molecular_weight function W and assigning species input that, defining the 53 species in an array atomic_name that’s are the ‘H’,’C’,’O’,’N’,’Ar’. Then by taking above element will create there atomic_weight array, it it easily available on internet ten we define the molecular starting range W to zero and then create and for loop condition which runs to the length. By using if condition command we create condition.

Using strcmp command for comparing the species and atomic name for calculating the molecular masses if both string found same. Now defining the str2num command assign to ‘n’. Now we run loop to find that the species contains more than one element. If there is more than 1 element that is n>1 then will increment the atom weight by the multiplication of position and n-1 to the atomic weight array.

Then to display it in command window, using disp command by following fprint command.

% FUNCTION FOR MOLECULAR WEIGHT CALCULATION

function Molecularweight = Molecular_weight(Species_SP)

% STANDERD AUTOMIC NAME

atomic_name = ['H','C','O','N','A'];

%HYDROGEN,CARBAN,OXYGEN,NITROGEN,ARGON

fprintf('Molecular Weight of ');

fprintf(Species_SP);

fprintf(' : ');

% STANDERD AUTOMIC NUMBER

Atomic_Weight = [1.008 12.011 15.999 14.007 39.948];

% STARTING RANGE MOLECULE

Molecularweight = 0;

% ADDING AUTOMIC NUMBER TO FIND PERTICULAR VALUE

for i = 1:length(Species_SP)

for j = 1:length(atomic_name)

if strcmpi(Species_SP(i),atomic_name(j))

Molecularweight = Molecularweight +Atomic_Weight(j);

position = j;

end

end

n = str2num(Species_SP(i));

if n>1

Molecularweight = Molecularweight + Atomic_Weight(position)*(n-1);

end

end

fprintf('molecular weight %s :',Species_SP);

fprintf('%d',Molecularweight);

disp(' ');

end

Results & output graph for O2 –

Results & output graph for N2 –

Results & output graph for CO2 –

Molecular weight in command window screenshot’s –

53 Folders Results & output graph screenshot –

Error occurs while programing screenshot –

Molecular weight in command window screenshot –

*Remaining molecular weight will be attached in attachments…with Note-pad file.

AND ATTACHED BELOW IN AN TEXT....

Molecular weight of O 15.999000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of O 15.999000

M =

15.9990

species n Molecular mass n 15.999000 n
ans =

0


Species_SP =

'O2'

Molecular weight of O2 15.999000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of O2 15.999000

M =

15.9990

species n Molecular mass n 15.999000 n
ans =

0


Species_SP =

'H'

Molecular weight of H 1.008000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of H 1.008000

M =

1.0080

species n Molecular mass n 1.008000 n
ans =

0


Species_SP =

'H2'

Molecular weight of H2 1.008000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of H2 1.008000

M =

1.0080

species n Molecular mass n 1.008000 n
ans =

0


Species_SP =

'OH'

Molecular weight of OH 17.007000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of OH 17.007000

M =

17.0070

species n Molecular mass n 17.007000 n
ans =

0


Species_SP =

'H2O'

Molecular weight of H2O 17.007000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of H2O 17.007000

M =

17.0070

species n Molecular mass n 17.007000 n
ans =

0


Species_SP =

'HO2'

Molecular weight of HO2 17.007000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HO2 17.007000

M =

17.0070

species n Molecular mass n 17.007000 n
ans =

0


Species_SP =

'H2O2'

Molecular weight of H2O2 17.007000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of H2O2 17.007000

M =

17.0070

species n Molecular mass n 17.007000 n
ans =

0


Species_SP =

'C'

Molecular weight of C 12.011000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C 12.011000

M =

12.0110

species n Molecular mass n 12.011000 n
ans =

0


Species_SP =

'CH'

Molecular weight of CH 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'CH2'

Molecular weight of CH2 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH2 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'CH2(S)'

Molecular weight of CH2(S) 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH2(S) 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'CH3'

Molecular weight of CH3 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH3 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'CH4'

Molecular weight of CH4 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH4 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'CO'

Molecular weight of CO 28.010000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CO 28.010000

M =

28.0100

species n Molecular mass n 28.010000 n
ans =

0


Species_SP =

'CO2'

Molecular weight of CO2 28.010000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CO2 28.010000

M =

28.0100

species n Molecular mass n 28.010000 n
ans =

0


Species_SP =

'HCO'

Molecular weight of HCO 29.018000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HCO 29.018000

M =

29.0180

species n Molecular mass n 29.018000 n
ans =

0


Species_SP =

'CH2O'

Molecular weight of CH2O 29.018000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH2O 29.018000

M =

29.0180

species n Molecular mass n 29.018000 n
ans =

0


Species_SP =

'CH2OH'

Molecular weight of CH2OH 30.026000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH2OH 30.026000

M =

30.0260

species n Molecular mass n 30.026000 n
ans =

0


Species_SP =

'CH3O'

Molecular weight of CH3O 29.018000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH3O 29.018000

M =

29.0180

species n Molecular mass n 29.018000 n
ans =

0


Species_SP =

'CH3OH'

Molecular weight of CH3OH 30.026000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH3OH 30.026000

M =

30.0260

species n Molecular mass n 30.026000 n
ans =

0


Species_SP =

'C2H'

Molecular weight of C2H 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C2H 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'C2H2'

Molecular weight of C2H2 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C2H2 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'C2H3'

Molecular weight of C2H3 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C2H3 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'C2H4'

Molecular weight of C2H4 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C2H4 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'C2H5'

Molecular weight of C2H5 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C2H5 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'C2H6'

Molecular weight of C2H6 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C2H6 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'CH2CO'

Molecular weight of CH2CO 41.029000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH2CO 41.029000

M =

41.0290

species n Molecular mass n 41.029000 n
ans =

0


Species_SP =

'HCCO'

Molecular weight of HCCO 41.029000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HCCO 41.029000

M =

41.0290

species n Molecular mass n 41.029000 n
ans =

0


Species_SP =

'HCCOH'

Molecular weight of HCCOH 42.037000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HCCOH 42.037000

M =

42.0370

species n Molecular mass n 42.037000 n
ans =

0


Species_SP =

'H2CN'

Molecular weight of H2CN 27.026000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of H2CN 27.026000

M =

27.0260

species n Molecular mass n 27.026000 n
ans =

0


Species_SP =

'HCN'

Molecular weight of HCN 27.026000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HCN 27.026000

M =

27.0260

species n Molecular mass n 27.026000 n
ans =

0


Species_SP =

'HNO'

Molecular weight of HNO 31.014000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HNO 31.014000

M =

31.0140

species n Molecular mass n 31.014000 n
ans =

0


Species_SP =

'N'

Molecular weight of N 14.007000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of N 14.007000

M =

14.0070

species n Molecular mass n 14.007000 n
ans =

0


Species_SP =

'NNH'

Molecular weight of NNH 29.022000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of NNH 29.022000

M =

29.0220

species n Molecular mass n 29.022000 n
ans =

0


Species_SP =

'N2O'

Molecular weight of N2O 30.006000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of N2O 30.006000

M =

30.0060

species n Molecular mass n 30.006000 n
ans =

0


Species_SP =

'NH'

Molecular weight of NH 15.015000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of NH 15.015000

M =

15.0150

species n Molecular mass n 15.015000 n
ans =

0


Species_SP =

'NH2'

Molecular weight of NH2 15.015000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of NH2 15.015000

M =

15.0150

species n Molecular mass n 15.015000 n
ans =

0


Species_SP =

'NH3'

Molecular weight of NH3 15.015000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of NH3 15.015000

M =

15.0150

species n Molecular mass n 15.015000 n
ans =

0


Species_SP =

'NO'

Molecular weight of NO 30.006000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of NO 30.006000

M =

30.0060

species n Molecular mass n 30.006000 n
ans =

0


Species_SP =

'NO2'

Molecular weight of NO2 30.006000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of NO2 30.006000

M =

30.0060

species n Molecular mass n 30.006000 n
ans =

0


Species_SP =

'HCNO'

Molecular weight of HCNO 43.025000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HCNO 43.025000

M =

43.0250

species n Molecular mass n 43.025000 n
ans =

0


Species_SP =

'HOCN'

Molecular weight of HOCN 43.025000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HOCN 43.025000

M =

43.0250

species n Molecular mass n 43.025000 n
ans =

0


Species_SP =

'HNCO'

Molecular weight of HNCO 43.025000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HNCO 43.025000

M =

43.0250

species n Molecular mass n 43.025000 n
ans =

0


Species_SP =

'NCO'

Molecular weight of NCO 42.017000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of NCO 42.017000

M =

42.0170

species n Molecular mass n 42.017000 n
ans =

0


Species_SP =

'CN'

Molecular weight of CN 26.018000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CN 26.018000

M =

26.0180

species n Molecular mass n 26.018000 n
ans =

0


Species_SP =

'HCNN'

Molecular weight of HCNN 41.033000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of HCNN 41.033000

M =

41.0330

species n Molecular mass n 41.033000 n
ans =

0


Species_SP =

'N2'

Molecular weight of N2 14.007000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of N2 14.007000

M =

14.0070

species n Molecular mass n 14.007000 n
ans =

0


Species_SP =

'AR'

Molecular weight of AR 39.948000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of AR 39.948000

M =

39.9480

species n Molecular mass n 39.948000 n
ans =

0


Species_SP =

'C3H8'

Molecular weight of C3H8 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C3H8 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'C3H7'

Molecular weight of C3H7 13.019000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of C3H7 13.019000

M =

13.0190

species n Molecular mass n 13.019000 n
ans =

0


Species_SP =

'CH3CHO'

Molecular weight of CH3CHO 42.037000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH3CHO 42.037000

M =

42.0370

species n Molecular mass n 42.037000 n
ans =

0


Species_SP =

'CH2CHO'

Molecular weight of CH2CHO 42.037000
Warning: Directory already exists.
> In main_code_new (line 106)
Molecular weight of CH2CHO 42.037000

M =

42.0370

species n Molecular mass n 42.037000 n
ans =

0

Conclusion –

The thermodynamic data is successfully plotted.

The thermodynamic data clearly shows the specific heat, enthalpy and entropy at various temperature range.

The project gives a good expose to parsing techniques, various commands in MatLab.

%MAIN PROGRAM


clear all
close all
clc


%  READING DATA FILE - NASA THERMODYNAMICS
% Open a file & 'r' Open the file for reading 
f1=fopen('THERMO.dat','r')
gt=fgetl(f1);


% OBTAINING TEMPERATURE 
global_temp=fgetl(f1);

% SPILTING
t=strsplit(global_temp);

% CONVERTING STRING CELL TO NUMBER USING str2num,
global_low_temp=str2num(t{2});
global_medium_temp=str2num(t{3});
global_high_temp=str2num(t{4});

% TEMPERATURE & GAS CONSTANT
    % TEMPERATURE RANGE ASSUMED
Temp=linspace(global_low_temp,global_high_temp,1000);
% UNIVERSAL GAS CONSTANT J/(mol-K)
    R = 8.314;   
    
% SKIPPING THREE LINES IN THE FILE
for i = 1:3
    gt=fgetl(f1);
end

 for i = 1:53
gt=fgetl(f1);
A=strsplit(gt,' ');
Species_SP=(A{1})


line_1=fgetl(f1)  ;        
a=strfind(line_1,'E');

% EXTRACCTING HIGHER AND LOWWER TEMPERATURE COEFFICIENT 

%  EXTRACCTING FIRST 7 COEFFICENT I.E HIGH - TEMPERATURE COEFFICIENT 
a1=line_1(1:a(1)+3);
a1=str2num(a1);              %1
a2=line_1(a(1)+4:a(2)+3);
a2=str2num(a2) ;             %2
a3=line_1(a(2)+4:a(3)+3);
a3=str2num(a3)  ;            %3
a4=line_1(a(3)+4:a(4)+3);
a4=str2num(a4);              %4
a5=line_1(a(4)+4:a(5)+3);
a5=str2num(a5)  ;            %5

line_2=fgetl(f1);
b=strfind(line_1,'E');;
a6=line_2(1:b(1)+3);
a6=str2num(a6)  ;            %6
a7=line_2(a(1)+4:b(2)+3);
a7=str2num(a7);              %7

 % Extracting SECOND 7 COEFFICIENT LOW-TEMPERATURE COEFFICIENT
a8=line_2(b(2)+4:b(3)+3);         
a8=str2num(a8);              %8
a9=line_2(b(3)+4:b(4)+3);
a9=str2num(a9) ;             %9
a10=line_2(b(4)+4:b(5)+3);
a10=str2num(a10) ;           %10


line_3=fgetl(f1);
c=strfind(line_1,'E');

a11=line_3(1:c(1)+3);
a11=str2num(a11);               %11
a12=line_3(c(1)+4:c(2)+3);
a12=str2num(a12);               %12
a13=line_3(c(2)+4:c(3)+3);
a13=str2num(a13);               %13
a14=line_3(c(3)+4:c(4)+3);
a14=str2num(a14);               %14

% SPECIFIC HEAT CALCULATION
    Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp);
 
    % ENTHALPHY CALCULATION
    H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp);
 
    % ENTROPY CALCULATION
    S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp);

    % CALCULATING MOLECUALR WEIGHT OF SPECIES
    Molecularweight(i) = Molecular_weight(Species_SP);
    
    % READ AND WRITE MOLECUALR LISTS
     Molecular_list = fopen('Molecular weight.txt','a+');
         fprintf( Molecular_list,'Molecular weight of %s is %d n',Species_SP,Molecularweight);
         fclose(Molecular_list);

     % plotting(CP,H,S,Temp,Species_SP)
        % SAVE AS THE FOLDER 
 
           cur_dir=pwd;
           mkdir(Species_SP);
           cd(Species_SP);
 
        % PLOT 1, SPECIFIC HEAT (CP) VS TEMPERATURE RANGE (Temp)
 
        figure(1)
        plot(Temp,Cp,'linewidth',2,'color','r');
        xlabel('Temperature  [K]');
        ylabel('Specific Heat [kJ]');
        title(sprintf('Varying Specific Heat with Temperature Range of %s',Species_SP));
        grid on
        saveas(figure(1),'Specific Heat.jpg');
 
        % PLOT 2,ENTHALPY(H) VS TEMPERATURE RANGE (Temp)
 
        figure(2)
        plot(Temp,H,'linewidth',2,'color','g');
        xlabel('Temperature [K]');
        ylabel('Enthalpy in [kJ/(mol)]');
        title(sprintf('Varying Enthalpy with Temperature Range of %s',Species_SP));
        grid on
        saveas(figure(2),'Ethalpy.jpg');
 
        % PLOT 3,ENTROPY (S) VS TEMPERATURE RANGE (Temp)
 
        figure(3)
        plot(Temp,S,'linewidth',2,'color','b');
        xlabel('Temperature [K]');
        ylabel('Entropy in [kJ/(mol-K)]');
        title(sprintf('Varying Entropy with Temperature Range of %s',Species_SP));
        grid on
        saveas(figure(3),'Entropy.jpg');
 
        cd(cur_dir);            
 
        % MOLECULAR WEIGHT
        
        M = Molecular_weight(Species_SP)
        f2 = fopen('molecular mass','w');
        fprintf('%s n %s n %f n','species','Molecular mass',M)
        fclose(f2)
end





% FUNCTION FOR ENTHALPY CALCULATION 

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp)  

% COMPARING WITH GLOBAL TEMPERATURE
% IF LESS THAN GLOBAL TEMPERATURE - MINIMUM COEFFICIENTS ARE TAKEN 
 
       if Temp <= global_medium_temp
        H = R*Temp.*(a8 + ((a9*Temp)/2) + ((a10*(Temp).^2)/3) + ((a11*(Temp).^3)/4) + ((a12*(Temp).^4)/5) +((a13./Temp)));
       else
           
% ELSE MAXIMUM COEFFICIEWNT ARE TAKEN 
           
        H = R*Temp.*(a1 + ((a2*Temp)/2) + ((a3*(Temp).^2)/3) + ((a4*(Temp).^3)/4) + ((a5*(Temp).^4)/5) +((a6./Temp)));
end




% FUNCTION FOR ENTROPY  CALCULATION

function S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp)  

% COMPARING WITH GLOBAL TEMPERATURE
% IF LESS THAN GLOBAL TEMPERATURE - MINIMUM COEFFICIENTS ARE TAKEN 
 
    if Temp <= global_medium_temp
        S = R.*(a8.*log(Temp) + (a9.*Temp) + (a10*((Temp).^2)/2) + (a11*((Temp).^3)/3) + (a12*((Temp).^4)/4) + a14);
    else
 
% ELSE MAXIMUM COEFFICIEWNT ARE TAKEN 

        S = R.*(a1.*log(Temp) + (a2.*Temp) + (a3*((Temp).^2)/2) + (a4*((Temp).^3)/3) + (a5*((Temp).^4)/4) + a7);
end




% FUNCTION FOR SPECIFIC HEAT CALCULATION
 
function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)   

% COMPARING WITH GLOBAL TEMPERATURE
% IF LESS THAN GLOBAL TEMPERATURE - MINIMUM COEFFICIENTS ARE TAKEN 

if  Temp <= global_medium_temp
Cp = R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(Temp).^3) + (a12*(Temp).^4)); 
else
Cp = R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(Temp).^3) + (a5*(Temp).^4));

% ELSE MAXIMUM COEFFICIEWNT ARE TAKEN 

end





% FUNCTION FOR MOLECULAR WEIGHT CALCULATION

function W = Molecular_weight(species)

% STANDERD AUTOMIC NAME 
         atomic_name = ['H','C','O','N','A'];   
         
         % STANDERD AUTOMIC MASSES         
         atomic_weight = [1.008 12.011 15.999 14.007 39.948];   
         
         W=0;     % Molecular weight starting range      
        
         % ADDING AUTOMIC NUMBER TO FIND PERTICULAR VALUE
        for i = 1:length(species)
                for j = 1:length(atomic_name)                                                                                                                                                         
                    if strcmp(species(i),atomic_name(j))
                   W = W + atomic_weight(j);                   
                    position = j;
                    end
                end
             n = str2double(species);
            
            if n>1
            
               W = W + atomic_weight(position)*(n-1); 
            
            end
        end
            fprintf('Molecular weight of %s ',species);
            fprintf('%f',W);
            disp(' ');      
            
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.