Challenge

Project 1 - Parsing NASA thermodynamic data

molecular weight of species is in report along with the screenshots of folders of each species.

Main program:

clear all
close all
clc

% Inputs to give

enter_species=input('Enter the species name','s');
comp=upper(enter_species);
total_species=53;

% Universal gas constant
R=8.314;

% Assigning global temperature

f1=fopen("THERMO.dat",'r');
line1=fgetl(f1);
line2=fgetl(f1);
T=strsplit(line2,' ');

global_low_temp=str2double(T{2});
global_mid_temp=str2double(T{3});
global_high_temp=str2double(T{4});

% Removing comment lines

line3=fgetl(f1);
line4=fgetl(f1);
line5=fgetl(f1);

% Loop to obtain data and get the plots for each species
for i=1:52

    % Obtaining the name of the species and converting to a string value

    tline1=fgetl(f1);
    a=strsplit(tline1,' ');
    species_cell=a(1);
    species=char(species_cell);

    % Finding the local values of temperature for each species

    local_low_temp=str2double(tline1(49:55));
    local_high_temp=str2double(tline1(58:65));
    local_mid_temp=str2double(tline1(68:75));

    % Obtaining the coefficient from the relevant species

    tline2=fgetl(f1);
    str=strfind(tline2,'E');
    a1=str2double(tline2(1:str(1)+3));
    a2=str2double(tline2(str(1)+4:str(2)+3));
    a3=str2double(tline2(str(2)+4:str(3)+3));
    a4=str2double(tline2(str(3)+4:str(4)+3));
    a5=str2double(tline2(str(4)+4:str(5)+3));

    tline3=fgetl(f1);
    str=strfind(tline3,'E');
    a6=str2double(tline3(1:str(1)+3));
    a7=str2double(tline3(str(1)+4:str(2)+3));
    a8=str2double(tline3(str(2)+4:str(3)+3));
    a9=str2double(tline3(str(3)+4:str(4)+3));
    a10=str2double(tline3(str(4)+4:str(5)+3));

    tline4=fgetl(f1);
    str=strfind(tline4,'E');
    a11=str2double(tline4(1:str(1)+3));
    a12=str2double(tline4(str(1)+4:str(2)+3));
    a13=str2double(tline4(str(2)+4:str(3)+3));
    a14=str2double(tline4(str(3)+4:str(4)+3));

    % Creating matrices to contain the extracted coefficient

    coeff=[a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14];

    % Creating matrices for high and low temp

    coeff_high=coeff(:,1:7);
    coeff_low=coeff(:,8:14);
    
    % Creating high and low local temp range

    low_temp_range=local_low_temp:1:local_mid_temp;
    high_temp_range=local_mid_temp:1:local_high_temp;
    
    % Calculating the high temp values using thermodynamic properties
    
    Cp_high=(coeff_high(1)+(coeff_high(2).*high_temp_range)+(coeff_high(3).*(high_temp_range.^2))+(coeff_high(4).*(high_temp_range.^3))+(coeff_high(5).*(high_temp_range.^4))).*R;
    H_high=(coeff_high(1)+(coeff_high(2).*high_temp_range./2)+(coeff_high(3).*(high_temp_range.^2)./3)+(coeff_high(4).*(high_temp_range.^3)./4)+(coeff_high(5).*(high_temp_range.^4)./5)+(coeff_high(6)./(high_temp_range))).*R.*high_temp_range;
    S_high=((coeff_high(1).*log(high_temp_range))+(coeff_high(2).*high_temp_range)+(coeff_high(3).*(high_temp_range.^2)./2)+(coeff_high(4).*(high_temp_range.^3)./3)+(coeff_high(5).*(high_temp_range.^4)./4)+coeff_high(7));

    % Calculating the low temp values using thermodynamic properties
    
    Cp_low=(coeff_low(1)+(coeff_low(2).*low_temp_range)+(coeff_low(3).*(low_temp_range.^2))+(coeff_low(4).*(low_temp_range.^3))+(coeff_low(5).*(low_temp_range.^4))).*R;
    H_low=(coeff_low(1)+(coeff_low(2).*low_temp_range./2)+(coeff_low(3).*(low_temp_range.^2)./3)+(coeff_low(4).*(low_temp_range.^3)./4)+(coeff_low(5).*(low_temp_range.^4)./5)+(coeff_low(6)./(low_temp_range))).*R.*low_temp_range;
    S_low=((coeff_low(1).*log(low_temp_range))+(coeff_low(2).*low_temp_range)+(coeff_low(3).*(low_temp_range.^2)./2)+(coeff_low(4).*(low_temp_range.^3)./3)+(coeff_low(5).*(low_temp_range.^4)./4)+coeff_low(7));

    %plotting

    figure(1)
    
    subplot(3,1,1)
    hold on;
    plot(low_temp_range,Cp_low,"Color",'r','LineWidth',3);
    plot(high_temp_range,Cp_high,"Color",'r','LineWidth',3);
    xlabel('local temp range (K)');
    ylabel('specific heat (J/kg K)');
    title(['variation of cp of ',species]);
    hold off;
    
    subplot(3,1,2)
    hold on;
    plot(low_temp_range,H_low,"Color",'b','LineWidth',3);
    plot(high_temp_range,H_high,"Color",'b','LineWidth',3);
    xlabel('local temp range (K)');
    ylabel('Enthalpy (J)');
    title(['variation of H of ',species]);
    hold off;
    
    subplot(3,1,3)
    hold on;
    plot(low_temp_range,S_low,"Color",'g','LineWidth',3);
    plot(high_temp_range,S_high,"Color",'g','LineWidth',3);
    xlabel('local temp range (K)');
    ylabel('Entropy (J/K mol)');
    title(['variation of S of ',species]);
    hold off;
    
    % Molecular weight of species
    
    mol_wt=molecular_mass(species);
    
    % Creating directory

    mkdir('new_folder')
    cd('new_folder')
    mkdir(species)
    cd(species)
    saveas(1,'plot.jpg')
    close all
    cd ..
    cd ..

end
fclose(f1);

Function:

function [mol_wt]=molecular_mass(comp)

mol_wt=0;
elements=['O','H','C','N','A'];
atm_weight=[16,1,12,14,40];

for i=1:length(comp)
    for j=1:length(elements)

        if strcmp(comp(i),elements(j))
            m(i)=atm_weight(j);
        end

        if str2double(comp(i))>1
            num=str2double(comp(i));
            m(i)=m(i-1)*(num-1);
        end
    end
end

for i=1:length(m)
    mol_wt=mol_wt+m(i);
end

end

Explanation:

At first the input universal gas constant is assigned.

Then the fopen command is used to open the file THERMO.dat file and read them.

Now the global temperature is assigned to variables by getting the line using fgetl command.

The comment lines is removed by storing them in variables as line3,line4,line5.

A for loop is created for running the same coding for the rest of species.

We created variable to store the species where the strsplit command is used to split the strings.

The local temperature is obtained by using the command the str2double command.

After that the coefficient is obtained and assigned in a array then they were split into coeff_high and coeff_low.

We created the low and high temp range using the local temperatures.

Then the thermodynamic properties has been calculated for the high and low temp ranges.

Plotting is made for the thermodynamic properties.

The molecular weight is calculated by using a function which we created using simple for loop and if end commands.

We created the directory to save the plots of each species in a separate folder using the mkdir command.

Finally, the fclose command is used to close the file we opened.

Errors:

At first plotting for each species has the same title this is corrected by giving the species variable instead of comp variable.

Then the plot for each species is showed in a single figure this is corrected by giving the close all command and proper directory commands.

In molecular weight function I gave ==1 in particular step which made the answers for all the species as 1 the error is corrected by removing it.

Other errors are minute which is rectified as coding.

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.