Challenge

Project 1 - Parsing NASA thermodynamic data

%Calculation of entropy

function s = entropy(A1,A2,A3,A4,A5,A6,A7,B1,B2,B3,B4,B5,B6,B7,T,R,global_med_temp)
if T<= global_med_temp
    s = (A1.* log(T)+A2.*T+A3.*(T.^2)./2+A4.*(T.^3)./3+A5.*(T.^4)./4+A7)*R;
else
    s = (B1.* log(T)+B2.*T+B3.*(T.^2)./2+B4.*(T.^3)./3+B5.*(T.^4)./4+B7)*R;
end


Expilination : 

               This the code for entropy(s) that is writen in the form of formulas that is given in the challenge to get the required inputs.



%Calculation of enthalpy

function h = enthalpy(A1,A2,A3,A4,A5,A6,B1,B2,B3,B4,B5,B6,T,R,global_med_temp)
if T<= global_med_temp
    h = (A1+(A2.*(T./2))+(A3.*(T.^2)./3)+(A4.*(T.^3)./4)+(A5.*(T.^4)./5)+(A6./T)).*(T.*R);
else
    h = (B1+(B2.*(T./2))+(B3.*(T.^2)./3)+(B4.*(T.^3)./4)+(B5.*(T.^4)./5)+(B6./T)).*(T.*R);
end

Expilination : 

               This the code for enthalpy(h) that is writen in the form of formulas that is given in the challenge to get the required inputs.


%Calculation of specific heat

function cp = specificheat(A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,T,R,global_med_temp)
% Comparing with global temp
% If less than global it will take min coeffs
if T<= global_med_temp
    cp = (A1+(A2.*T)+(A3.*T.^2)+(A4.*T.^3)+(A5.*T.^4)) *R;
%If temp is more it will take max coeffs
else 
    cp = (B1+(B2.*T)+(B3.*T.^2)+(B4.*T.^3)+(B5.*T.^4)) *R;

end

Expilination : 

               This the code for specific heat(cp) that is writen in the form of formulas that is given in the challenge to get the required inputs.

%Function to calculate molecular weight
function MW = molecular_weight(name)
atoms = ['H','C','O','N','R'];
     fprintf('Molecular Weight of');
    fprintf(name);
    fprintf(' : ');
    atomic_weight = [1 12 16 14 40];
    MW = 0;
    for i = 1:length(name)
        for j = 1:length(atoms)
            if strcmp(name(i),atoms(j))
                MW = MW + atomic_weight(j);
                position = j;
            end
        end
        n = str2num(name(i));
        if n>1
            MW = MW + atomic_weight(position)*(n-1);
        end
    end
    fprintf('%d',MW);
    disp(' ');
end


Expilination :
 
                This is the code for calculating 'Molecular weight' of ['H','C','O','N','R'] and we get the molecular weights of components those are shown in images.
close all 
clear all
clc

% program for parsing NASA thermodynamic data
f1 = fopen('THERMO.dat','r');
first_line = fgetl(f1);

% Obtaining global max,min and medium temperature
global_temp = fgetl(f1);
global_temp_range = strsplit(global_temp,' ');
global_min_temp = str2num(global_temp_range{2});
global_med_temp = str2num(global_temp_range{3});
global_max_temp = str2num(global_temp_range{4});

%Range of temperature
T = linspace(global_min_temp,global_max_temp,400);
R = 8.314

for i = 3:5
    comments = fgetl(f1);
end

%Getting the coeffs of species

for i = 1:53
    %Extracting line 1 and the name of the species
    line_1 = fgetl(f1);
    name = strtok(line_1);
    %Extracting higher and lower temperature coeffs
    line2 = fgetl(f1);
    line3 = fgetl(f1);
    line4 = fgetl(f1);

    A = strfind(line2,'E');
    B = strfind(line3,'E');
    C = strfind(line4,'E');




    %Extracting higher temperature coeffs
    A1 = str2num(line2(1:A(1)+3));
    A2 = str2num(line2(A(1)+4:A(2)+3));
    A3 = str2num(line2(A(2)+4:A(3)+3));
    A4 = str2num(line2(A(3)+4:A(4)+3));
    A5 = str2num(line2(A(4)+4:A(5)+3));
    A6 = str2num(line3(1:B(1)+3));
    A7 = str2num(line3(B(1)+4:B(2)+3));

    %Extracting lower temperature coeffs

    B1 = str2num(line3(B(2)+4:B(3)+3));
    B2 = str2num(line3(B(3)+4:B(4)+3));
    B3 = str2num(line3(B(4)+4:B(5)+3));
    B4 = str2num(line3(1:C(1)+3));
    B5 = str2num(line3(C(1)+4:C(2)+3));
    B6 = str2num(line3(C(2)+4:C(3)+3));
    B7 = str2num(line3(C(3)+4:C(4)+3));

    %Specific heat calculation
    cp = specificheat(A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,T,R,global_med_temp);

    %Enthalpy calculation
    h = enthalpy(A1,A2,A3,A4,A5,A6,B1,B2,B3,B4,B5,B6,T,R,global_med_temp);

    %Entropy calculation
    s = entropy(A1,A2,A3,A4,A5,A6,A7,B1,B2,B3,B4,B5,B6,B7,T,R,global_med_temp);

    %Calculating molecular weight of species
    Molecular_wt(i) = molecular_weight(name)

    %Creating individual folder for each species and plotting
    % plotting(cp,h,s,T,name)

    cd(pwd)
dir_name = name;
mkdir(dir_name);


figure (1);
plot(T,cp,'linewidth',3,'color','b')
grid on
xlabel('Temperature')
ylabel('Specific Heat')
title(sprintf('Specific Heat Vs Temperature for %s',name))


figure (2);
plot(T,h,'linewidth',3,'Color','r')
grid on
xlabel(' Temperature')
ylabel('Enthalpy')
title(sprintf('Enthalpy Vs Temperature for %s',name))


figure (3);
plot(T,s,'linewidth',3,'Color','g')
grid on
xlabel('Temperature')
ylabel('Entropy')
title(sprintf('Entropy Vs Temperature for %s',name))


cd(dir_name)

   saveas(figure(1),'cp_vs_t.png')
   saveas(figure(2),'h_vs_t.png')
   saveas(figure(3),'s_vs_t.png')

   cd ..
end


Expilination :

             This code is for parsing NASA Thermodynamic data and that theromo data provided and we used 'r' command to read the file but data is in string mode we have convert in number or single variables.If you starting doing that it first covert in 'cell' array the cell array is different compared to other arrays because it will store  different types variables in it so to convert it into single variables we should use '{}' these.

            In this we use some new commanda those are
fopen = file open
fgetl = to read the header information
strsplit = to split line
str2num = Convert character array or string to numeric array
strtok = select parts of string
pwd = return to current working direction
cd = to specifiy that file should be stored in a specified file
            As in the challenge mensioned we taken 14 charecterstics those are (A1,A2,A3,A4,A54,A6,A7,B1,B2,B3,B4,B5,B6,B7) by using these we are extracting the temperature ranges we will calculate specific heat,enthalpy,entropy.


   
Conclussion :

             After observing these plots,we understood that the specific heat of any species is not a constent value it depends on temperature. Generally we use constant values for our caliculation but we conclude that the specific heat,enthalpy,entropy are temperature deended variables. NASA has determined these polinomial vaqlues to calculate these properties of each species. In this project we evaluate them with the help of parsing data file along with we write a program to calculate the molecular weight of each 53 species and create individual folders to store the respective plots.
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.