Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

To parse(file handling) NASA's thermodynamic data and display plots of each species using the data.

Main code:

close all
clear all
clc

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

% Defining temperature values
templ = str2num(fgetl(f1));
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

for i=1:53

line1 = fgetl(f1);
l = strsplit(line1, ' ');
m = length(l);
species = l{1};

% Storing the values of the local temperature values

low_temp = str2double(l(m-3));
high_temp = str2double(l(m-2));
mid_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(4)+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)));
a10 = 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(3)+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

% lower temperature values for cp,h,s calculations i.e  low to middle temp values
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+(a13/lower_temp(k)))*R*lower_temp(k);

s_low(k)=((a8*log(lower_temp(k))+(a9*lower_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 i.e mid to high temp values
cp_high(k)=(a1+(a2*higher_temp(k))+(a3*(higher_temp(k)^2)+(a4*(higher_temp(k))^3)+a5*(higher_temp(k))^4))*R;

h_high(k)=(a1+(a2*higher_temp(k))/2+(a3*higher_temp(k)^2)/3+(a4*higher_temp(k)^3)/4+(a5*(higher_temp(k)^4))/5+(a6/higher_temp(k)))*R*higher_temp(k);

s_high(k)=((a1*log(higher_temp(k))+a2*higher_temp(k)+((a3*higher_temp(k)^2)/2)+((a4*higher_temp(k)^3)/3)+((a5*higher_temp(k)^4)/4)+a7)*R);

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)
plot(lower_temp,cp_low,'linewidth',4,'color','k');
hold on
plot(higher_temp,cp_high,'linewidth',4,'color','k');
hold off
title(['Temperature vs Specific Heat',species]);
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',4,'color','g');
hold on
plot(higher_temp,h_high,'linewidth',4,'color','g');
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,'color','b');
hold on
plot(higher_temp,s_high,'linewidth',2,'color','b');
hold off
title(['Temperature vs Entropy',species]);
xlabel('Temperature');
ylabel('Entropy');
legend('low temperature','high temperature');
fig = sprintf('Entropy of %s.png',species);
saveas(gca,fig);
cd(current)
end

%To print molecular weights in command window
molecular_weight_o = 16

molecular_weight_o2 = 32

molecular_weight_h = 1

molecular_weight_h2 = 2

molecular_weight_oh = 17

molecular_weight_h2o = 18

molecular_weight_ho2 = 33

molecular_weight_h2o2 = 34

molecular_weight_c = 12

molecular_weight_ch = 13

molecular_weight_ch2 = 14

molecular_weight_ch2s = 46

molecular_weight_ch3 = 15

molecular_weight_ch4 = 16

molecular_weight_co = 28

molecular_weight_co2 = 44

molecular_weight_hco = 29

molecular_weight_ch2o = 30

molecular_weight_ch2oh = 31

molecular_weight_ch3o = 31

molecular_weight_ch3oh = 32

molecular_weight_c2h = 25

molecular_weight_c2h2 = 14

molecular_weight_c = 16

molecular_weight_c2h3 = 27

molecular_weight_c2h5 = 29

molecular_weight_c2h6 = 30

molecular_weight_ch2co = 42

molecular_weight_hcco = 41

molecular_weight_hccoh = 42

molecular_weight_h2cn = 28

molecular_weight_hcn = 27

molecular_weight_hno = 31

molecular_weight_n = 14

molecular_weight_nnh = 29

molecular_weight_n2o = 44

molecular_weight_nh = 15

molecular_weight_nh2 = 16

molecular_weight_nh3 = 17

molecular_weight_no = 30

molecular_weight_no2 = 46

molecular_weight_hcno = 43

molecular_weight_hocn = 43

molecular_weight_hnco = 43

molecular_weight_nco = 42

molecular_weight_cn = 26

molecular_weight_hcnn = 41

molecular_weight_n2 = 28

molecular_weight_ar = 40

molecular_weight_c3h8 = 44

molecular_weight_c3h7 = 43

molecular_weight_ch3cho = 44

molecular_weight_ch2cho = 43

Plots of O2:

Plots of N2:

Plots of CO2:

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.