Challenge

Project 1 - Parsing NASA thermodynamic data

clc

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

fgetl(f1);

% Defining temperature values templ = str2num (fgetl(f1)); %#ok<ST2NM>

global_low_temp = templ(1);

global_mid_temp = templ(2);

global_high_temp = templ(3);

For skipping unwanted lines

for i = 1:3

fgetl(f1);

end

% Loop creation to read the values and plot molecular weight

for i = 1:4:212 
    line1 = fgetl(f1);

l = strsplit(line1, '');

m = length(1);

species 1{1};

% Storing the values of the local temperature values low_temp = str2double (l(m-3));

mid_temp = str2double (l(m-2));
high_temp = str2double (l(m-1)); % Check this later.

Storing the values of the second line of variables

line2= fgetl(f1);

Finding the position of E

a = strfind(line2, 'E');

Reading all the other variables

a1 = str2double (line2(1: (a(1)+3))); a2 = str2double(line2((a(1)+4): (a(2)+3)));

a3 = str2double (line2((a(2)+4): (a(3)+3)));

a4 = str2double (line2((a(3)+4): (a(4)+3)));

a5 = str2double (line2((a(3)+4): (a(5)+3)));

Storing the values of the 3rd line

Line3 fgetl(f1); Finding the position of E

b= strfind(line3, 'E');

Storing the values for the next set of variables

a6= str2double(line3 (1:b(1)+3));

a7= str2double(line3((b(1)+4): (b(2)+3)));

a8= str2double(line3((b(2)+4): (b(3)+3))); a9= str2double (line3((b(3)+4): (b(4)+3)));

a18 = str2double (line3 ((b(4)+4): (b(5)+3)));
% Storing ther values of the 4th line

line4 fgetl(f1);

% Finding the position of E

C=strfind(line4, 'E');

% Storing the values of variables in line 4

a11 = str2double (line4 (1: c(1)+3));

a12 str2double (line4 ((c(1)+4): (c(2)+3)));

a13 = str2double (line4 ((c(2)+4): (c(3)+3)));

a14 str2double (line4 ((c(2)+4): (c (4)+3)));

% Creating Temperature values for lower and higher temperature

lower_temp = linspace (low_temp, mid_temp, 100);

higher_temp = linspace (mid_temp, high_temp, 100);

%% Value of the universal has constant in j/mol*K R 8.314; % Universal Gas Constant

% Calculating the values for cp, H and S using the given formulas

for k = 1:100
    % Calculating the values for cp, H and S using the given formulas for k = 1:100

% lower temperature values for cp,h,s calculations

cp_low(k)=(a8+(a9*lower_temp(k))+(a10*(lower_temp(k)^2)+(a11*(lower_temp(k))^3)+a12*(lower_temp(k))^4))*R;
h_low(k)=(a8+ (a9*lower_temp (k))/2+(a10*lower_temp(k)^2)/3+(a11*lower_temp(k)^3)/4+a12*(lower_temp(k)^4/5+9a13/lower_temp(k)))*R*lower_temp(k);
s_low(k)=((a8*log (lower_temp (k))+(a9xlower_temp(k))+((a10*lower_temp(k)^2)/2)+((a11*lower_temp(k)^3)/3)+((a12*lower_temp(k)^4)/4)+a14)*R);

% higher temperature values for cp,h,s calculations

cp_high(k)= (a1+(a2*higher_temp(k))+(a3*(higher_temp(k)^2)+(a4* (higher temp (k))^3)+a5+ (higher_temp(k);
h_high(k) = (a1+(a2*higher_temp (k))/2+(a3*higher_temp(k)^2)/3+(a4+higher_temp (k)^3)/4+ (a5x (higher_temp;
s_high(k)=((al log (higher_temp(k))+a2*higher_temp(k)+((a3*higher_temp(k)^2)/2)+((a4 higher_temp(k)^3;

end

%Making Folders for saving the plots

nasa graphs = species;

mkdir(nasa_graphs);

current pwd;

cd (nasa graphs);

% Plotting the graphs

% For specific heat

figure(1)

hold on

plot(lower_temp,cp_low, 'linewidth',3)
xlabel('Temperature');

ylabel('Specific Heat'); legend('low temperature', 'high temperature'); fig = sprintf('Specific Heat of %s.png', species);

saveas (gca, fig);

For Enthalpy

figure(2)

plot(lower_temp, h_low, 'linewidth',3)

hold on

plot(higher temp, h_high, 'linewidth',3)

hold off

title(['Temperature vs Enthalpy', species]) xlabel('Temperature');

ylabel('Enthalpy');

legend('low temperature', 'high temperature'); fig = sprintf('Enthalpy of %s.png', species); saveas (gca, fig);

% For Entropy

figure(3)

plot(lower_temp, s_low, 'linewidth', 2)

hold on

plot(higher temp, s_high, 'linewidth', 2)

hold off

title(['Temperature vs Entropy', species]) xlabel('Temperature');

ylabel('Entropy');

legend('low temperature', 'high temperature'); fig = sprintf('Entropy of s.png', species);
fig = sprintf( 'Entropy of %s.png', species); saveas (gca, fig);

cd(current)

end

% Calculating molecular weight

atoms = ['H', 'C', 'N', 'R', 'S']; atom_weight = [1.00784 12.0109 16.002 39.065 32.067];

mol_weight= 0;

species_check = char (species);

for i=1:length(species_check)

for j=1:length(atoms)

if (strcmp(species_check(i), atoms(j))) mol_weight mol_weight + atom_weight (j); position = j;

end

end

x = str2double (species_check(i));

if x>1

mol_weight mol_weight + (atom_weight (j)*(x-1));

fprintf('Molecular weight of %s is: %f \n', species_check, mol_weight)

end

end
AIM:a function to extract all the co-efficent and calculate the values of temperature , enthalpy an entropy foe all the species in the file.



The main program with explanation:
A file thermo.dat that is provided the code uses the values from the thermodynamic data given;



Clear all
Close all
clc
f1 = fopen('THERMO.dat', 'r');   opening of the file 
fgetl(f1);
% Defining temperature values templ = str2num (fgetl(f1)); %#ok<ST2NM>
global_low_temp = templ(1);
global_mid_temp = templ(2);
global_high_temp = templ(3);
For skipping unwanted lines
for i = 1:3
fgetl(f1);
end
% Loop creation to read the values and plot molecular weight
for i = 1:4:212
   line1 = fgetl(f1);  f get is used to skip the lines:

l = strsplit(line1, '');
m = length(1);
species 1{1};
% Storing the values of the local temperature values low_temp = str2double (l(m-3));
mid_temp = str2double (l(m-2));
high_temp = str2double (l(m-1)); % Check this later.
Storing the values of the second line of variables
line2= fgetl(f1);
Finding the position of E
a = strfind(line2, 'E');
Reading all the other variables
a1 = str2double (line2(1: (a(1)+3))); a2 = str2double(line2((a(1)+4): (a(2)+3)));
a3 = str2double (line2((a(2)+4): (a(3)+3)));
a4 = str2double (line2((a(3)+4): (a(4)+3)));
a5 = str2double (line2((a(3)+4): (a(5)+3)));
Storing the values of the 3rd line
Line3 fgetl(f1); Finding the position of E
b= strfind(line3, 'E');
Storing the values for the next set of variables
a6= str2double(line3 (1:b(1)+3));
a7= str2double(line3((b(1)+4): (b(2)+3)));
a8= str2double(line3((b(2)+4): (b(3)+3))); a9= str2double (line3((b(3)+4): (b(4)+3)));
a18 = str2double (line3 ((b(4)+4): (b(5)+3)));
% Storing ther values of the 4th line
line4 fgetl(f1);
% Finding the position of E
C=strfind(line4, 'E');
% Storing the values of variables in line 4
a11 = str2double (line4 (1: c(1)+3));
a12 str2double (line4 ((c(1)+4): (c(2)+3)));
a13 = str2double (line4 ((c(2)+4): (c(3)+3)));
a14 str2double (line4 ((c(2)+4): (c (4)+3)));
% Creating Temperature values for lower and higher temperature
lower_temp = linspace (low_temp, mid_temp, 100);
higher_temp = linspace (mid_temp, high_temp, 100);
%% Value of the universal has constant in j/mol*K R 8.314; % Universal Gas Constant
% Calculating the values for cp, H and S using the given formulas
for k = 1:100
   % Calculating the values for cp, H and S using the given formulas for k = 1:100
% lower temperature values for cp,h,s calculations
cp_low(k)=(a8+(a9*lower_temp(k))+(a10*(lower_temp(k)^2)+(a11*(lower_temp(k))^3)+a12*(lower_temp(k))^4))*R;
h_low(k)=(a8+ (a9*lower_temp (k))/2+(a10*lower_temp(k)^2)/3+(a11*lower_temp(k)^3)/4+a12*(lower_temp(k)^4/5+9a13/lower_temp(k)))*R*lower_temp(k);
s_low(k)=((a8*log (lower_temp (k))+(a9xlower_temp(k))+((a10*lower_temp(k)^2)/2)+((a11*lower_temp(k)^3)/3)+((a12*lower_temp(k)^4)/4)+a14)*R);
% higher temperature values for cp,h,s calculations
cp_high(k)= (a1+(a2*higher_temp(k))+(a3*(higher_temp(k)^2)+(a4* (higher temp (k))^3)+a5+ (higher_temp(k);
h_high(k) = (a1+(a2*higher_temp (k))/2+(a3*higher_temp(k)^2)/3+(a4+higher_temp (k)^3)/4+ (a5x (higher_temp;
s_high(k)=((al log (higher_temp(k))+a2*higher_temp(k)+((a3*higher_temp(k)^2)/2)+((a4 higher_temp(k)^3;
end
%Making Folders for saving the plots
nasa graphs = species;
mkdir(nasa_graphs);
current pwd;
cd (nasa graphs);
% Plotting the graphs
% For specific heat
figure(1)
hold on
plot(lower_temp,cp_low, 'linewidth',3)
xlabel('Temperature');
ylabel('Specific Heat'); legend('low temperature', 'high temperature'); fig = sprintf('Specific Heat of %s.png', species);
saveas (gca, fig);
For Enthalpy
figure(2)
plot(lower_temp, h_low, 'linewidth',3)
hold on
plot(higher temp, h_high, 'linewidth',3)
hold off
title(['Temperature vs Enthalpy', species]) xlabel('Temperature');
ylabel('Enthalpy');
legend('low temperature', 'high temperature'); fig = sprintf('Enthalpy of %s.png', species); saveas (gca, fig);
% For Entropy
figure(3)
plot(lower_temp, s_low, 'linewidth', 2)
hold on
plot(higher temp, s_high, 'linewidth', 2)
hold off
title(['Temperature vs Entropy', species]) xlabel('Temperature');
ylabel('Entropy');
legend('low temperature', 'high temperature'); fig = sprintf('Entropy of s.png', species);
fig = sprintf( 'Entropy of %s.png', species); saveas (gca, fig);
cd(current)
end
% Calculating molecular weight
atoms = ['H', 'C', 'N', 'R', 'S']; atom_weight = [1.00784 12.0109 16.002 39.065 32.067];
mol_weight= 0;
species_check = char (species);
for i=1:length(species_check)
for j=1:length(atoms)
if (strcmp(species_check(i), atoms(j))) mol_weight mol_weight + atom_weight (j); position = j;
end
end
x = str2double (species_check(i));
if x>1
mol_weight mol_weight + (atom_weight (j)*(x-1));
fprintf('Molecular weight of %s is: %f \n', species_check, mol_weight)
end
end
Conclutions;
The thermal data file was read and the needful operations were performed.
The co efficent values were find.

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.