Challenge

Project 1 - Parsing NASA thermodynamic data

%Project 1 - Parsing NASA thermodynamic data

clear all

close all

clc

%Getting data file

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

fgetl(f);

%Reading header information

tline = fgetl(f);

A = str2num(tline);

global_low_temp = A(:,1);

global_mid_temp = A(:,2);

global_high_temp = A(:,3);

%Skipping comment lines

for i = 1:3

fgetl(f)

end

%Reading species data

for i = 1:53

line1 = fgetl(f);

l = strsplit(line1)

m = length(l);

sp = l(1);

species = char(sp);

atom = ['H','C','O','N','Ar','S'];

atomic_weight = [1.008 12.011 15.999 14.007 39.948 32.076];

low_temp = str2double(l(end-3));

mid_temp = str2double(l(end-1));

high_temp = str2double(l(end-2));

%Storing the value of second line

line2 = fgetl(f);

a = strfind(line2,'E');

a1 = str2double(line2(1:(a+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 value of 3rd line

line3 = fgetl(f);

b = strfind(line3,'E');

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

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

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

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

a10 = str2double(line3((b(4)+4):(a(5)+3)));

%Storing the value of 4th line

line4 = fgetl(f);

c = strfind(line3,'E');

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

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

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

a14 = str2double(line4((c(3)+4):(a(4)+3)));

%Creating temp values for lower and higher temp

TL = linspace(low_temp,mid_temp,1000);

TH = linspace(mid_temp,high_temp,1000);

%Value of universal gas constant

R = 8.314;

%Calc specific heat, enthalpy and entropy for lower and higher temp

for k = 1:length(TL)

%for lower temp

cp_low(k) = (a8 + a9*TL(k) + a10*TL(k)^2 + a11*TL(k)^3 + a12*TL(k)^4)*R;

h_low(k) = (a8 + a9*TL(k)/2 + a10*(TL(k)^2)/3 + a11*(TL(k)^3)/4 + a12*(TL(k)^4)/5 + a13/TL(k))*R*TL(k);

s_low(k) = (a8*log(TL(k)) + a9*TL(k) + a10*(TL(k)^2)/2 + a11*(TL(k)^3)/3 + a12*(TL(k)^4)/4 + a13*(TL(k)^5)/5 + a14*(TL(k)^6)/6)*R*TL(k);

%for higher temp

cp_high(k) = (a1 + a2*TH(k) + a3*TH(k)^2 + a4*TH(k)^3 + a5*TH(k)^4)*R;

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

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

end

%Creating folder

nasa_file = species;

mkdir(nasa_file);

current = pwd;

cd(nasa_file);

%Creating figure of specific heat

figure(1)

plot(TL,cp_low,'LineWidth',2,'Color','r');

hold on

plot(TH,cp_high,'LineWidth',2,'Color','g');

hold off

title(['Temp Vs Specific heat,species']);

xlabel('Temp');

ylabel('Specific heat');

legend('Low Temp','High Temp');

%Saving .png file of figure

fig = sprintf('Specific heat of %s.png',species);

saveas(gca,fig);

%Creating figure of enthalpy

figure(2)

plot(TL,cp_low,'LineWidth',2,'Color','r');

hold on

plot(TH,h_high,'LineWidth',2,'Color','g');

hold off

title(['Temp VS Enthalphy,species']);

xlabel('Temp');

ylabel('Enthalphy')

legend('Low Temp','High Temp');

%Saving .pmg file of figure

fig = sprintf('Enthalphy of %s.png', species);

saveas(gca,fig);

%Creating figure of entropy

figure(3)

plot(TL,s_low,'LineWidth',2,'Color','r');

hold on

plot(TH,s_high,'LineWidth',2,'Color','r');

hold off

title(['Temp VS Entropy,species']);

xlabel('Temp');

ylabel('Entropy')

legend('Low Temp','High Temp');

%Saving .png file of figure

fig = sprintf('Entropy of %s.png',species);

saveas(gca,fig);

cd(current);

%Calculation of molecular weight

atom = ['H','C','O','N','Ar','S'];

atomic_weight = [1.008 12.011 15.999 14.007 39.948 32.076];

mw = 0;

for p = 1 : length(species)

for q = 1 :length(atomic_weight)

if strcmp(species(p),atom(q))

mw = mw + atomic_weight(q);

position = q;

end

end

%Used for multiplication into the nu,ber of atoms

n = str2double(species(p));

if n>1

mw = mw + atomic_weight(position) * (n-1);

end

end

%Display molecular weight

fprintf('molecular weight of %s : %f\n',species,mw);

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.