Challenge

Project 1 - Parsing NASA thermodynamic data

%this code is about parsing the file which contains the coefficient of
%equations of the specific heat, enthalpy and entropy of various and using
%them to there graph with respect to the given temperature in the file and
%this file also contains the code for calculating the molecular weight of
%the gases in the file.

%Opening the file
f1 = fopen('THERMO.DAT','r');

%skipping line
sk1 = fgetl(f1);

%getting the line containing the global temp. of the gases
temp_line1 = fgetl(f1);

%splitting the string
global_temp = strsplit(temp_line1,' ');

%obtaining all the temp. from the above splitted string and converting them
%to numbers for furthur use.
global_low_temp = str2double(global_temp{2});
global_mid_temp = str2double(global_temp{3});
global_high_temp = str2double(global_temp{4});

%skipping 3 lines from the file
sk2 = fgetl(f1);
sk3 = fgetl(f1);
sk4 = fgetl(f1);

%creating the folder in which all the folders of different species will be
%stored
folder = 'project1';
mkdir(folder)
cd(folder)

%for loop for reading the data of each species
for i = 1:53

        %storing information from the 4 lines of the species
        line1 = fgetl(f1);
        line2 = fgetl(f1);
        line3 = fgetl(f1);
        line4 = fgetl(f1);
        
        %splitting the local temp. and species text
        [name,temp] = strtok(line1,'G');
        
        %getting the species text
        species_name = strtok(name);
        
        %splitting local low, mid and high temp. text and converting them
        %to number
        tline1 = strsplit(temp,' ');
        localtemp_low = str2double(tline1{2});
        localtemp_mid = str2double(tline1{4});
        localtemp_high = str2double(tline1{3});
        
        %creating the array of local temp. for smoother plot
        T = linspace(localtemp_low,localtemp_high,1000);
        
        %splitting the coeffients of low temp. from line 1 and converting
        %them to number
        a = strfind(line2,'E');
        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));
        
        %splitting the coeffients of low and high temp. from line 2 and
        %converting them to number
        b = strfind(line3,'E');
        a6 = str2double(line3(1:b(1)+3));
        a7 = str2double(line3(b(1)+4:b(2)+3));
        b1 = str2double(line3(b(2)+4:b(3)+3));
        b2 = str2double(line3(b(3)+4:b(4)+3));
        b3 = str2double(line3(b(4)+4:b(5)+3));
        
        %splitting the coeffients of high temp. from line 3 and converting
        %them to number
        c = strfind(line4,'E');
        b4 = str2double(line4(1:c(1)+3));
        b5 = str2double(line4(c(1)+4:c(2)+3));
        b6 = str2double(line4(c(2)+4:c(3)+3));
        b7 = str2double(line4(c(3)+4:c(4)+3));
        
        R = 8.31446;    %Universal gas constant
        
        %Calculating the values of specific heat, enthaly and entropy from
        %there functions which are created as per the equations given in
        %the challenge
        cp = cp_func(T,R,a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,localtemp_mid);
        H = h_func(T,R,a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,localtemp_mid);
        S = s_func(T,R,a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,localtemp_mid);
        
        %Calculating the molecular weight

        %letters representing each species or atom of gas in file
        atoms = ['H','C','N','O','S','R'];
        %atomic weight corresponding to there respective atoms
        atomic_weight = [1, 12, 14, 16, 32 40];

        mw = 0;     %initial molecular weight

        %for loop for separating each atom of the species and calculating
        %there molecular by adding them
        for u = 1:length(species_name)
            for j = 1:length(atoms)
                if strcmp(species_name(u),atoms(j))
                    mw = mw+atomic_weight(j);
                    m = j;
                end
            end
                x = str2double(species_name(u));
                if x>1
                    mw = mw+(atomic_weight(m)*(x-1));
                end
        end

        %displaying the molecular weight on the command window
        fprintf('Molecular weight of %s is %f \n',species_name,mw)
        
        %storing the path path of initial folder to return to it later on
        %after storing the plot figures into there respective species
        %folder
        oldfolder = cd(pwd);

        %creating the species folder for saving the plot figures
        mkdir(species_name)
        %entering the species folder
        cd(species_name)
        
        %plotting

        %plotting code for temp. vs specific heat
        figure(1)
        plot(T,cp)
        title('Temperature vs Specific heat')
        grid on
        xlabel('Temperature')
        ylabel('Specific Heat')
        saveas(gcf,'specific heat.png') %saving the figure in the folder
        
        %plotting code for temp. vs enthalpy
        figure(2)
        plot(T,H)
        title('Temperature vs Enhalpy')
        grid on
        xlabel('Temperature')
        ylabel('Enthalpy')
        saveas(gcf,'Enthalpy.png') %saving the figure in the folder
        
        %plotting code for temp. vs enthalpy
        figure(3)
        plot(T,S)
        title('Temperature vs Entropy')
        grid on
        xlabel('Temperature')
        ylabel('Entropy')
        saveas(gcf,'Entropy.png') %saving the figure in the folder
        
        %returning to initial folder for creating other species folder
        cd(oldfolder)

        %closing all plots to avoid congestion of plot on screen
        close all
                
end

%closing the thermo file
f1 = fclose("all");


%function for calculating specific heat for local low and high temp arrays 
%as per the equation provided in the challenge
function specific_heat = cp_func(T,R,a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,localtemp_mid)

if T< localtemp_mid
    specific_heat = (a1+(a2.*T)+(a3*T.^2)+(a4*T.^3)+(a5*T.^4)).*R;
else
    specific_heat = (b1+(b2.*T)+(b3*T.^2)+(b4*T.^3)+(b5*T.^4)).*R;    
end

end

%function for calculating enthalpy for local low and high temp arrays 
%as per the equation provided in the challenge
function enthalpy = h_func(T,R,a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,localtemp_mid)

if T<localtemp_mid
    enthalpy = (a1+((T.*a2)./2)+((a3*T.^2)./3)+((a4*T.^3)./4)+((a5*T.^4)./5)+(a6./T))./(R.*T);
else
    enthalpy = (b1+((T.*b2)./2)+((b3*T.^2)./3)+((b4*T.^3)./4)+((b5*T.^4)./5)+(b6./T))./(R.*T);
end

end

%function for calculating entropy for local low and high temp arrays 
%as per the equation provided in the challenge
function entropy = s_func(T,R,a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,localtemp_mid)

if T<localtemp_mid
    entropy = ((a1.*log(T))+(a2.*T)+((a3*T.^2)./2)+((a4*T.^3)./3)+((a5*T.^4)./4)+a7).*R;
else
    entropy = ((b1.*log(T))+(b2.*T)+((b3*T.^2)./2)+((b4*T.^3)./3)+((b5*T.^4)./4)+b7).*R;
end

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.

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.