Challenge

Project 1 - Parsing NASA thermodynamic data

 Project 1 - Parsing Nasa Thermodynamic Data 

Function Codes

% Enthalpy Calculation function

function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_mid_temp,R,T)

    % finding enthalpy with low and temperature variables
    if T<=local_mid_temp
        H = (a8+(a9*T/2)+(a10*(T.^2)/3)+(a11*(T.^3)/4)+(a12*(T.^4)/5)+(a13./T))*R.*T;
    else
        H = (a1+(a2*T/2)+(a3*(T.^2)/3)+(a4*(T.^3)/4)+(a5*(T.^4)/5)+(a6./T))*R.*T;
    end
end
% Entropy Calculation function

function [S] = entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_mid_temp,T,R)

    % finding entropy with low and temperature variables
    if T<=local_mid_temp
        S = ((a8*log(T))+(a9*T)+(a10*(T.^2)/2)+(a11*(T.^3)/3)+(a12*(T.^4)/4)+a14)*R;
    else
        S = ((a1*log(T))+(a2*T)+(a3*(T.^2)/2)+(a4*(T.^3)/3)+(a5*(T.^4)/4)+a7)*R;
    end
end
% Molecular Weight Calculation function

function molecularweight = molecular_weight(species_name)
         % Standard - Atomic Name
         atomic_name = ['H','C','O','N','A'];             
         % Hydrogen, Carbon, Oxygen, Nitrogen, Argon
         
         fprintf('Molecular Weight of ');
         fprintf(species_name);
         fprintf(' : ');
         % Standard Atomic Number
         atomic_Weight = [1.008 12.011 15.999 14.007 39.948];     
         % Starting Range of Molecule           
         molecularweight = 0;                                   
        
         % Adding Atomic number to obtain particular value
        for i = 1:length(species_name)
            for j = 1:length(atomic_name)
                if strcmpi(species_name(i),atomic_name(j))
                        molecularweight = molecularweight +atomic_Weight(j);
                    position = j;
                end
            end
            n = str2num(species_name(i));
            
            if n>1
            
                molecularweight = molecularweight + atomic_Weight(position)*(n-1);
            
            end
        end
            fprintf('molecular weight %s :',species_name);
            fprintf('%d',molecularweight);
            disp(' ');
end
% Specific heat calculation function

function [cp] = specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temp,T,R)

    % finding specific heat with low and temperature variables
    if T<=local_mid_temp
        cp = (a8+(a9*T)+(a10*T.^2)+(a11*T.^3)+(a12*T.^4))*R;
    else
        cp = (a1+(a2*T)+(a3*T.^2)+(a4*T.^3)+(a5*T.^4))*R;
    end
end

MAIN CODE 

% NASA File Parsing 
close all
clear all
clc

% Reading the file
f = fopen('THERMO.dat','r');

% Skipping 5 comments in the file 
for i = 1:5
    fgetl(f)
end

for j = 1:53  
line_1= fgetl(f)
a = strsplit(line_1,' ');

% Reading species name 
species_name = a{1};
len = length(a);

% Reading local temp
local_mid_temp = str2num(a{len-1});
local_high_temp = str2num(a{len-2});
local_low_temp = str2num(a{len-3});

% Reading Coefficients
% Extracting lower and higher temperature ciefficients 
% Extracting FIRST 7 coefficients i.e. HIGH -  temperature coefficients 
coeff_line_1 = fgetl(f);
pos1 = strfind(coeff_line_1,'E');

a1 = str2num(coeff_line_1(1:pos1(1)+3));
a2 = str2num(coeff_line_1(pos1(1)+4:pos1(2)+3));
a3 = str2num(coeff_line_1(pos1(2)+4:pos1(3)+3));
a4 = str2num(coeff_line_1(pos1(3)+4:pos1(4)+3));
a5 = str2num(coeff_line_1(pos1(4)+4:pos1(5)+3));

coeff_line_2 = fgetl(f);
pos2 = strfind(coeff_line_2,'E');
 
% Extracting SECOND 7 coefficients i.e. LOW - temperature coefficients 
a6 = str2num(coeff_line_2(1:pos2(1)+3));
a7 = str2num(coeff_line_2(pos2(1)+4:pos2(2)+3));
% lower coefficients
a8 = str2num(coeff_line_2(pos2(2)+4:pos2(3)+3));
a9 = str2num(coeff_line_2(pos2(3)+4:pos2(4)+3));
a10 = str2num(coeff_line_2(pos2(4)+4:pos2(5)+3));
    
coeff_line_3 = fgetl(f);
pos3 = strfind(coeff_line_3,'E');

a11 = str2num(coeff_line_3(1:pos3(1)+3));
a12 = str2num(coeff_line_3(pos3(1)+4:pos3(2)+3));
a13 = str2num(coeff_line_3(pos3(2)+4:pos3(3)+3));
a14 = str2num(coeff_line_3(pos3(3)+4:pos3(4)+3));

% Temperature range assumed 
T = linspace(local_low_temp,local_high_temp,100);

% Universal Gas Constant 
R = 8.314;

% Specific Heat Calculation 
cp = specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temp,T,R);

% Enthalpy Calculation 
h = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_mid_temp,R,T);

% Entropy Calculation 
s = entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_mid_temp,T,R);

% Molecular Weight 
molecularweight(i) = molecular_weight(species_name);

% Read & Write Molecular list
Molecular_list = fopen('Molecular weight.txt','a+');
fprintf( Molecular_list,'Molecular weight of %s is %d \n',species_name,molecularweight);
fclose(Molecular_list);

% Creating directory 
cd('C:\Users\surka\OneDrive\Documents\MATLAB\species_name');
mkdir(species_name)
cd(species_name)

% Plotting 
% Plot 1 Temperature vs Specific Heat 
figure(1)
plot(T,cp,'linewidth',2,'color','r')
saveas(figure(1),'cp VS T.jpg')
grid on 
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ]')
title(sprintf('Varying Specific Heat with Temperature Range of %s',species_name))

% Plot 2 Temperature vs Enthalpy 
figure(2)
plot(T,h,'linewidth',2,'color','c')
saveas(figure(2),'h VS T.jpg')
grid on 
xlabel('Temperature [K]')
ylabel('Enthalpy [KJ/mol]')
title(sprintf('Varying Enthalpy with Temperature Range of %s',species_name))

% Plot 3 Temperature vs Entropy 
figure(3)
plot(T,s,'linewidth',2,'color','m')
saveas(figure(3),'s VS T.jpg')
grid on 
xlabel('Temperature [K]')
ylabel('Entropy [KJ/mol-K]')
title(sprintf('Varying Entropy with Temperature Range of %s',species_name))
end

Screenshot of the window where all the folders are saved

Plots for o2, N2 and Co2 species 

Plots for O2

Plots for N2

Plots for 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.