Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: to write a function to extract the 14 coefficients from NASA thermodynamic data.and calculate enthalpy, entropy and specific heat for all species. also calculate the molecular weight of each species.

Given equation

`(Cp)/R = a_1+a_2*T+a_3*T^2+a_4*T^3+a_5*T^4`

`H/(RT) = a_1+(a_2*T)/2+(a_3*T^2)/3+(a_4*T^3)/4+(a_5*T^4)/5+a_6/T`

`S/R = a_1*lnT+a_2*T+(a_3*T^2)/2+(a_4*T^3)/3+(a_5*T^4)/4+a_7`

Matlab Code with explaination in comment

clear all 
close all
clc
% reading the file
f = fopen('THERMO.dat','r');%opening the file in reading mode

%reading header data
for j = 1:5 %for loop for runnig fgetl command for 5 times
    fgetl(f);%calling data line by line for first 5 lines in nasa thermo file
end%end of for loop

%extracting data from species block
for k = 1:53% running for loop from 1 to 53 for each 53 species
line1 =  fgetl(f);%for calling first line of species 
a = strsplit(line1,' ');% splitting each string of first line where ever they are saperated by space

% to extract species name
species_name = a{1};% in first line of each species first string is species name we are extrating that in this line

% to extract local temperatures
local_temp_low = a{length(a)-3};%extracting low temperature string from first line of species
local_high_temp = a{length(a)-2};%extracting high temperature string from first line of species
local_mid_temp = a{length(a)-1};%extracting mid temperature string from first line of species

%converting local temp to integer
local_temp_low = str2num(local_temp_low);%converting low temperature string data into numerical value
local_high_temp = str2num(local_high_temp);%converting high temperature string data into numerical value
local_mid_temp = str2num(local_mid_temp);%converting mid temperature string data into numerical value

%extraction of coeff
coeff_line_1 = fgetl(f);%calling second line of sepecies to extract coefficients

%reading position of E in coefficient line
posE = strfind(coeff_line_1,'E');%finding position of E in line 2 of species data

%extracting coefficient from line 1
a1 = str2num(coeff_line_1(1:posE(1)+3));%extracting first coefficient from line 2 of species and and storing in a1
a2 = str2num(coeff_line_1(posE(1)+4:posE(2)+3));%extracting second coefficient from line 2 of species and and storing in a2
a3 = str2num(coeff_line_1(posE(2)+4:posE(3)+3));%extracting third coefficient from line 2 of species and and storing in a3
a4 = str2num(coeff_line_1(posE(3)+4:posE(4)+3));%extracting fourth coefficient from line 2 of species and and storing in a4
a5 = str2num(coeff_line_1(posE(4)+4:posE(5)+3));%extracting fifth coefficient from line 2 of species and and storing in a5

%extracting coefficient from line 2
coeff_line_2 = fgetl(f);%calling third line of sepecies to extract coefficients
posE2 = strfind(coeff_line_2,'E');%finding position of E in line 3 of species data
a6 = str2num(coeff_line_2(1:posE(1)+3));%extracting 6th coefficient from line 3 of species and and storing in a6
a7 = str2num(coeff_line_2(posE(1)+4:posE(2)+3));%extracting 7th coefficient from line 3 of species and storing in a7
a8 = str2num(coeff_line_2(posE(2)+4:posE(3)+3));%extracting 8th coefficient from line 3 of species and and storing in a8
a9 = str2num(coeff_line_2(posE(3)+4:posE(4)+3));%extracting 9th coefficient from line 3 of species and and storing in a9
a10 = str2num(coeff_line_2(posE(4)+4:posE(5)+3));%extracting 1th coefficient from line 3 of species and and storing in a10

%extracting coefficient from line 3
coeff_line_3 = fgetl(f);%calling fourth line of sepecies to extract coefficients
posE2 = strfind(coeff_line_3,'E');%finding position of E in line 4 of species data
a11 = str2num(coeff_line_3(1:posE(1)+3));%extracting 11th coefficient from line 4 of species and and storing in a11
a12 = str2num(coeff_line_3(posE(1)+4:posE(2)+3));%extracting 12th coefficient from line 4 of species and and storing in a12
a13 = str2num(coeff_line_3(posE(2)+4:posE(3)+3));%extracting 13th coefficient from line 4 of species and and storing in a13
a14 = str2num(coeff_line_3(posE(3)+4:posE(4)+3));%extracting 14th coefficient from line 4 of species and and storing in a14


T = linspace(local_temp_low,local_high_temp,500);%creating temperature array
R = 8.314;%gas constant

%calculation of H,S, Cp
for i = 1:length(T) %for loop for temperature array
    if T(i)<=local_mid_temp%if condition for choosing coefficient from low to high coefficient(first 7 high temp coeff last 7 low temp coeff)
        cp(i) = (a8+a9*T(i)+a10*T(i).^2+a11*T(i).^3+a12*T(i).^4)*R;%formula for calculating cp for low temperature coeff
        H(i) = (a8+(a9*T(i))/2+(a10*T(i).^2)/3+(a11*T(i).^3)/4+(a12*T(i).^4)/5+(a13/T(i)))*R*T(i);%formula for calculating H for low temperature coeff
        S(i) = (a8*log(T(i))+a9*T(i)+(a10*T(i).^2)/2+(a11*T(i).^3)/3+(a12*T(i).^4)/4+a14)*R;%formula for calculating S for low temperature coeff
    else
        cp(i) = (a1+a2*T(i)+a3*T(i).^2+a4*T(i).^3+a5*T(i).^4)*R;%formula for calculating cp for high temperature coeff
        H(i) = (a1+(a2*T(i))/2+(a3*T(i).^2)/3+(a4*T(i).^3)/4+(a5*T(i).^4)/5+(a6/T(i)))*R*T(i);%formula for calculating H for high temperature coeff
        S(i) = (a1*log(T(i))+a2*T(i)+(a3*T(i).^2)/2+(a4*T(i).^3)/3+(a5*T(i).^4)/4+a7)*R;%formula for calculating S for High temperature coeff
    end%end of if else condition
end%end of temperature array loop

cd('E:\MATLAB\MATLAB PROGRAM\NASA_file_parsing')%changing directory
mkdir(species_name)%creating folder with each species name
cd(species_name)%changing directory to species folder


%plotting ans saving file
figure(1)%plotting figure 1
plot(T,cp)%plotting T vs cp
xlabel('Temperature')%x-label
ylabel('specific heat capacity')%y-label
title(sprintf('variation of specific heat capacity with temperature for %s',species_name))%title of the plot
saveas(figure(1),'variation of cp wrt T.jpg')%saving figure 1 in species folder
figure(2)%plotting figure 2
plot(T,H)%plotting T vs H
xlabel('Temperature')%x-label
ylabel('enthalpy')%y-label
title(sprintf('variation of enthalpy with temperature for %s',species_name))%title of the plot
saveas(figure(2),'variation of H wrt T.jpg')%saving figure 2 in species folder
figure(3)%plotting figure 3
plot(T,S)%plotting T vs S
xlabel('Temperature')%x-label
ylabel('entropy')%y-label
title(sprintf('variation of entropy with temperature for %s',species_name))%title of the plot
saveas(figure(3),'variation of S wrt T.jpg')%saving figure 3 in species folder


%calculation of Molecular weight
element = ['H','C','N','O','A'];%creating element character array
atomic_weight = [1,12,14,16,40];%creating atomic weight array
molecular_weight = 0;%initialization of molecular weight
for m = 1:length(species_name)%for loop for species name 1 to length of species name
    for n = 1:length(element)%for loop for element 1 to length of element
        if strcmp(species_name(m),element(n))%comparing characters of species name with element array
            molecular_weight = molecular_weight+atomic_weight(n);%calculating molecular weight if elments matches
            position=n;%assigning position of numeric characters in species name          
            end%end of if condition
    end%end of element loop
      z = str2num(species_name(m));%converting numeric characters from species name to numerical value
      if(z>1)%if condition       
      molecular_weight=molecular_weight+atomic_weight(position)*(z-1);% calculation of molecular weight for numerical position
      end%end of if condition    
end%end of species name loop
fprintf('molecular weight of %s is %f',species_name,molecular_weight)%printing name and molecular weight of the species
disp(' ')%to print line 95 in new line in command window
end%end of for loop from 1 to 53

Windows screenshot of saved file

Molecular weight of species

Output plot for `O_2`

Specific heat

Enthalpy

Entropy

Output plot for `CO_2`

Specific heat

Enthalpy

Entropy

Output plot for `N_2`

Specific heat

Enthalpy

Entropy

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.