Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA thermodynamic data

Aim

  • Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
  • Write function to calculate the molecular weight of each species and for display it in the command window.
  • Write main program to parse "THERMO.dat" file as an input and generate Cp, H and S plots.
  • Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
  • Write code to save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
  • Lastly,write a detailed report to describe logic.

Objective

  • Read the data from "THERMO.dat" file.
  • Read and separate global Temperature.
  • Skip the comment/ Unnecessary lines.
  • Create loop to read all local temperature.
  • Read and separate all the coefficient values.
  • Create the appropriate function for enthalpy, entropy and specific heats by using given equation.
  • Create another function to calculate molecular weight.
  • Write plot code for first figure which is specific heat and temperature.
  • Write plot code for second figure which is Enthalpy and temperature.
  • Write plot code for last figure which is Entropy and temperature.
  • Create directory file to save all the plots in separate folder
  • Save all the generated plots in directory file by generating separate folders.
  • Save all the Molecular weight of each species by writing data in .txt file
  • Write final report on program and plots, generated outputs.

File Parsing

The process of "parsing" can be described as reading in a data stream of some sort and building an in-memory model or representation of the semantic content of that data, in order to facilitate performing some kind of transformation on the data.

Some examples:

  1. A compiler parses your source code to (usually) build an abstract syntax tree of the code, with the objective of generating object- (or byte-) code for execution by a machine.
  2. An interpreter does the same thing but the syntax tree is then directly used to control execution (some interpreters are a mashup of byte-code generators and virtual machines and may generate intermediate byte-code).

Starting of main program

It is good practice to use clear all, close all and clc which usually clear all previous inputs, close all previous plots and figures and clear command window in their respective order.

%% Parsing NASA thermodynamic data 
%% Clear Everything
clear all
close all
clc

Below code is the starting of main program. First use the “fopen” command to mention a specific file that has to read.Then, skip first line using “fgetl” command as it is heading of the data file and not useful for our calculation or in other words to skip unworkable lines of data.

Next line contains global temperature. By implementing “fgetl” command store temperatures value in string. But this only help to store temperature line. In order to separate all temperature we execute “strsplit” command to break temperatures and assign it as Global High Temperature, Global Mid Temperature and Global Low Temperature as per following codes.

%% Read first line and Temperature
 
f1 = fopen('THERMO.dat','r');             % Open file for reading
line_1 = fgetl(f1);                       % Read file Header
GlobalTemp = fgetl(f1);                   % Read second line
Temp = strsplit(GlobalTemp,' ');          % Split string form line
% Use str2num to convert charecter array or string in numeric array
Global_low_Temp = str2num(Temp{2});       % Assign indices of 'Temp' to a variable
Global_mid_Temp = str2num(Temp{3});       % Assign indices of 'Temp' to a variable
Global_Hgh_Temp = str2num(Temp{4});       % Assign indices of 'Temp' to a variable

After reading Global temperatures next line in the .txt file contain comments in 3 line to skip that line apply “fgetl” to each of them. In a bid to do it in a short and easy way apply for loop using “fgetl” to all three line.

%% Reading the comment line with for loop
for i=3:5
    fgetl(f1);
end

Below comment line from 6th line data file contain set of species number with its temperatures and following lines hold 14 coefficients. And there are total 53 such sets are contained in file. So, to repeat same lines of code to all 53 make for loop to run same program through each sets.

First define 6th line and numbers of column contain by species name which separate out all the species name in that column range including spaces. In order to remove spaces use “deblank” command on the species name.

Secondly, extract local temperature from line 6 by using its column range location, employ “strsplit” to split each local temperature using spaces between them and name them as local low temperatures, local mid temperatures and local high temperatures accordingly. Afterwards, create two temperature range,’local low temperature range’ and ‘local high temperature range’ using “linspace” command.

%% Main data Reading
for j = 1:53                           % Defining total species data range for looping
    line_6 = fgetl(f1);                % Read 6th line
 
    Species = line_6(1:10);            % Defining species name
Species_Name = deblank(Species);   % Remove all the blank space
    
    Species_Temp = line_6(49:75);      % Read Local temperate at 6th line
    % Split temperature using spaces between them
    local_values = strsplit(Species_Temp,' ');
    
    %% Define value for each local Temperature 
    % Use str2num to convert charecter array or string in numeric array
    Local_low_Temp = str2num(local_values{1});
    Local_mid_Temp = str2num(local_values{3});
Local_hgh_Temp = str2num(local_values{2});
    
    % Storing the local temperatures in the from of array range
    Low_temp_range = linspace(Local_low_Temp,Local_mid_Temp,50);
Hgh_temp_range = linspace(Local_mid_Temp,Local_hgh_Temp,50);

Next, extract all the coefficients from line 7, 8 and 9. For that, first, read the 7th line and use “findstr” to locate E in each coefficients. For first coefficient provide a line range from 1 to 12 (1:12) as there are 12 numeric values before E. After that to get latter 3 numeric value use +3. Using above concept and “findstr” each coefficient value can be break into array so in order to extract first coefficient 12 can be replaced as “coeff_line_7(1)” first value of array. Hence, one can write (1:coeff_line_7(1)+3).

After that use 1st coefficient value as starting variable to second value from array of coeff_line_7(2) to get the value of second coefficient,(coeff_line_7(1)+4:coeff_line_7(2)+3). Same procedure can use to find out other remaining coefficients.

    %% Extracting all the coefficients from file
    line_7 = fgetl(f1);                 % Read 7th line 
    coeff_line_7= findstr(line_7,'E');  % Separate each coefficients useing E
    
    % Separate out all values of the coefficients in 7th line
    % Convert it into numeric value using str command
    a1  = line_7(1:coeff_line_7(1)+3);  
    a1  = str2num(a1);
    a2  = line_7(coeff_line_7(1)+4:coeff_line_7(2)+3);
    a2  = str2num(a2);
    a3  = line_7(coeff_line_7(2)+4:coeff_line_7(3)+3);
    a3  = str2num(a3);
    a4  = line_7(coeff_line_7(3)+4:coeff_line_7(4)+3);
    a4  = str2num(a4);
    a5  = line_7(coeff_line_7(4)+4:coeff_line_7(5)+3);
    a5  = str2num(a5);
 
    % Separate out all values of the coefficients in 8th line
    % Convert it into numeric value using str command    
    line_8 = fgetl(f1);
    coeff_line_8 = findstr(line_8,'E');
    a6  = line_8(1:coeff_line_8(1)+3);
    a6  = str2num(a6);
    a7  = line_8(coeff_line_8(1)+4:coeff_line_8(2)+3);
    a7  = str2num(a7);
    a8  = line_8(coeff_line_8(2)+4:coeff_line_8(3)+3);
    a8  = str2num(a8);
    a9  = line_8(coeff_line_8(3)+4:coeff_line_8(4)+3);
    a9  = str2num(a9);
    a10 = line_8(coeff_line_8(4)+4:coeff_line_8(5)+3);
    a10 = str2num(a10);
    
    % Separate out all values of the coefficients in 9th line
    % Convert it into numeric value using str command      
    line_9 = fgetl(f1);
    coeff_line_9 = findstr(line_9,'E');
    a11 = line_9(1:coeff_line_9(1)+3);
    a11 = str2num(a11);
    a12 = line_9(coeff_line_9(1)+4:coeff_line_9(2)+3);
    a12 = str2num(a12);
    a13 = line_9(coeff_line_9(2)+4:coeff_line_9(3)+3);
    a13 = str2num(a13);
    a14 = line_9(coeff_line_9(3)+4:coeff_line_9(4)+3);
    a14 = str2num(a14);

It is given that first 7 coefficients are high temperature coefficients and other 7 are low temperature coefficients therefore range of high and low temperature coefficient are shown below.

    % First 7 High Temperature Coefficients Range
    Coeff_Hgh = [a1 a2 a3 a4 a5 a6 a7];
    % Second 7 Low Temperature Coefficients Range
    Coeff_Low = [a8 a9 a10 a11 a12 a13 a14];

After extracting all the values from the TERMO data file, it is right time to move forward for calculation.

As per this reports aim to find out Specific heat, Enthalpy and Entropy lets first learn about their definitions.

Specific Heat

  • The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius.

Enthalpy

  • One of the fundamental quantities which occur in thermodynamics is the sum of internal energy and pressure volume product.
  • The enthapy of fluid is the property of fluid, since it consist of sum of a properties and the product of the two properties it can be introduced into any problem whether the process is a flow or non-flow process.

Entropy

  • The measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtain from ordered molecular motion.
  • The amount of entropy is also a measure of the molecular disorder, or randomness, of a system.

Now, getting the introduction of the entities that we have to calculate, we create a function using given equation. That is ..

For specific heat

`frac{Cp}{R} = a1 + a2 T + a3T^2 +a4T^3+a5T^4`

For Enthalpy

`frac{H}{RT}=a1+frac{a2T}{2}+frac{a3T^2}{3}+frac{a4T^3}{4}+frac{a5T^4}{5}+frac{a6}{T}`

For Entropy

`frac{S}{R}=a1 lnT+a2T+frac{a3T^2}{2}+frac{a4T^3}{3}+frac{a5T^4}{4}+a7`

Using above equation, write the function program.

In this program, calculate Cp_low & Cp_hgh, H_low & H_hgh, S_low & S_hgh separately, using that plot graph line of high and low curve with different color.

function [Cp_low, Cp_hgh, H_low, H_hgh, S_low, S_hgh] = Output_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,LT,HT,R);
 
%% Specific Heat Calculation
Cp_low = (a8+(a9.*LT)+(a10.*LT.^2)+(a11.*LT.^3)+(a12.*LT.^4))*R;
Cp_hgh = (a1+(a2.*HT)+(a3.*HT.^2)+(a4.*HT.^3)+(a5.*HT.^4))*R;
 
%% Enthalpy Calculation
H_low = (a8 + (a9.*LT)/2 + (a10.*LT.^2)/3 + (a11.*LT.^3)/4 + (a12.*LT.^4)/5 + a13./LT).*R.*LT;
H_hgh = (a1 + (a2.*HT)/2 + (a3.*HT.^2)/3 + (a4.*HT.^3)/4 + (a5.*HT.^4)/5 + a6./HT).*R.*HT;
 
%% Entropy Calculation
S_low = (a8.*log(LT) + (a9.*LT) + (a10.*LT.^2)/2 + (a11.*LT.^3)/3 + (a12.*LT.^4)/4 + a14)*R;
S_hgh = (a1.*log(HT) + (a2.*HT) + (a3.*HT.^2)/2 + (a4.*HT.^3)/3 + (a5.*HT.^4)/4 + a7)*R;
 
end 

Provide Inputs for the function in the main program.

    %% Input for calculation
    % Universal gas constant (KJ/K-mol-k)
    R = 8.314;
    %  Temperature(K) Range
    LT = Low_temp_range;
    HT = Hgh_temp_range;
    % Coefficients Range
    CL = Coeff_Low;
    CH = Coeff_Hgh;

Now, call the program by writing below codes.

    %% Function for Specific Heat, Enthalpy & Entropy Calculation
[Cp_low,Cp_hgh,H_low,H_hgh,S_low,S_hgh]=Output_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,LT,HT,R);

Next step, write program function to calculate Molecular weight

Molecular Weight

  • Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule.
  • Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations.

In function define all the species element in data file (O,H,N,A,C,S) also mention their atomic mass accordingly.

At initial point, consider molecular weight is declared as zero. Make for loop to defile complete number of species in the thermo data file also do the same for overall species element, means their will be two for loop. Add if condition to proceed species different way if species element and species name are same it will pass through the first formula otherwise it will pass to last one. Before passing through final formula, convert all species name into numeric value, meaning, in atomic mass and pass the value which is greater than 1. it will ignore less than 1 atomic value as it negligible. Molecular weight is again calculated by having the atomic mass for the current species by multiplying it into n-1.

%% Function program for Molecular Weight
function   Mol_Wt = Mol_weight_function(Species_Name);
 
% Mention all species elements use in data file
Species_Element = {'O','H','N','A','C','S'};

% provide atomic mass of above entities
Atomic_mass = [15.9994 1.00794 14.0067 39.9480 12.0110 32.0650];

% consider Initial value of molecular wt as 0
Mol_Wt=0;                           

for l = 1:length(Species_Name)              % Define range of species length in colomn
    for m = 1:length(Species_Element)

        %strcmp - compares s1 and s2 and returns 1 (true) if the two are identical and 0 (false) otherwise

        % compare to entities using if statements
        if strcmp(Species_Name(l),Species_Element(m))
            % Add each molecular weight/ atomic mass in every species 
            Mol_Wt = Mol_Wt + Atomic_mass(m);
            % that value of molecular weight stored in string p
            p = m;                                   
        end
    end
    % convert all carecter array into atomic masses in numeric form 
    n=str2num(Species_Name(l)); 
    % apply condition if atomic mass is greter than 1 then proceed it forward                 
    if q > 1 
        % Molecular weight again calculated by having species value (q-1)                                 
        Mol_Wt = Mol_Wt + Atomic_mass(p)*(q-1);      
    end
end
end

Call molecular weight function into main program.

    %% Molecular Weight Function
    Mol_Wt = Mol_weight_function(Species_Name);

Using all the calculated value of specific heat, enthalpy, entropy and given value of temperature we can plot the graph

[1] - Plot- Specific Heat vs Temperature

[2] - Plot- Enthalpy vs Temperature

[3] - Plot- Entropy vs Temperature

Codes for the first plot

% [1]-Plot- Specific Heat vs Temperature
    figure(1)
    plot(LT,Cp_low,'b','linewidth',2)
    hold on
    plot(HT,Cp_hgh,'r','linewidth',2)
    title({'Specific Heat vs Temperature',['Species Name= ',Species_Name,' & Molecular Weight= ',num2str(Mol_Wt)]})
    xlabel('Temperature (k)')
    ylabel('Specific Heat (J/K kg)')
    legend('Low Temperature','High Temperature')
    grid on
    hold off

Out put plots of specific Heat vs Temperature of o2, H2 and Co2 in their respective order:-

Codes for the Second plot : -

    % [2]-Plot- Enthalpy vs Temperature
    figure(2)
    plot(LT,H_low,'b','linewidth',2)
    hold on
    plot(HT,H_hgh,'m','linewidth',2)
    % generate title with their species name and molecular weight using following codes 
    title({'Enthalpy vs Temperature',['Species Name= ',Species_Name,' & Molecular Weight= ',num2str(Mol_Wt)]})
    xlabel('Temperature (k)')
    ylabel('Enthalpy (J/K)')
    legend('Low Temperature','High Temperature')
    grid on
    hold off

Output plots of Enthalpy vs Temperature of o2, H2 and Co2 in their respective order:-

Codes for the third plot : -

    % [3]-Plot- Entropy vs Temperature
    figure(3)
    plot(LT,S_low,'g','linewidth',2)
    hold on
    plot(HT,S_hgh,'r','linewidth',2)
    title({'Entropy vs Temperature',['Species Name= ',Species_Name,' & Molecular Weight= ',num2str(Mol_Wt)]})
    xlabel('Temperature (k)')
    ylabel('Entropy (J/K)')
    legend('Low Temperature','High Temperature')
    grid on
    hold off

Output plots of Entropy vs Temperature of o2, H2 and Co2 in their respective order:-

Next, code is to save the plots by creating directory in separate folder by each species name:-

    %% Making directory and saving the figures
    % Making a new folder for each species
    % Saving the plot in seaperate folder by each species name
    mkdir('E:\Study\skill-lync\Projects\9-File Parsing\NASA data parsing',Species_Name);
    cd(['E:\Study\skill-lync\Projects\9-File Parsing\NASA data parsing\',Species_Name]);                 
    saveas(figure(1),'Specific Heat vs Temperature.jpg');
    saveas(figure(2),'Enthalpy vs Temperature.jpg');
    saveas(figure(3),'Entropy vs Temperature.jpg');

Output for above program

Code to generate .txt file for Molecular Weight:-

    %% Writing Molecular Weight file 
    cd(['E:\Study\skill-lync\Projects\9-File Parsing\NASA data parsing\']);
    Molecular_Weight = fopen('Molecular_Weight.txt','a');
    fwrite(Molecular_Weight,sprintf('Molecular Weight of %s = %f\n',Species_Name,Mol_Wt));
    %fwrite(Molecular_Weight,sprintf('Molecular Weight of %s = %f\n',Species_Name,Mol_Wt);
    fclose(Molecular_Weight);

Output of above program:-

Code to print Molecular Weight in command:-

    % Print the molecular weight of the species in command window
    fprintf('Molecular Weight of %s = %f\n',Species_Name,Mol_Wt)

Output of above Code:-

End the program loop and close the data file using fclose command.

end   
% closing the file
fclose(f1);

Errors during Program

[1] Typo error forgot to typr ‘l’ after fget

[2] Incorrect function name

[3] Incomplete parenthesis

[4] "R" universal gas constant is not define

[5] Curly brackets “{}” is not given while labeling species name and molecular weight

[6] Incorrect folder name

[7] Functions are not present in directory file

Conclusion

Implementing this program one will be able to extract all the data from “NASA Thermo.txt” file, which will help to calculate different parameters, plots/ graphs, using proper function of their formulas.

By file parsing we also be able to extract data from “NASA Thermo.txt”to find out the molecular mass of each species and able to write data file of all the molecular weight with species in an organized form so that it can easy to read.

At last, because of this particular project we learn to organize each plot of myriads of species in there separate folder by allotting species name to that particular folders.

References

[1] R.K. rajput - Thermal engineering by - https://www.slideshare.net/hiwaiyasin/thermal-engineering-by-rk-rajput-chapter-22

[2] Mathworks Help center Retrieved from from - https://www.mathworks.com/help/matlab/ref/str2num.html

[3] Mathworks Help center Retrieved from from - https://www.mathworks.com/help/matlab/ref/strsplit.html

[4] Mathworks Help center Retrieved from from - https://www.mathworks.com/help/matlab/ref/deblank.html

Complete program

%% Parsing NASA thermodynamic data

%% Clear Everything
clear all
close all
clc

%% Read first line and Temperature
f1 = fopen('THERMO.dat','r');          % Open file for reading
line_1 = fgetl(f1);                    % Read file Header
GlobalTemp = fgetl(f1);                % Read second line
Temp = strsplit(GlobalTemp,' ');       % Split string form line
% Use str2num to convert charecter array or string in numeric array
Global_low_Temp = str2num(Temp{2});    % Assign indices of 'Temp' to a variable
Global_mid_Temp = str2num(Temp{3});    % Assign indices of 'Temp' to a variable
Global_Hgh_Temp = str2num(Temp{4});    % Assign indices of 'Temp' to a variable


%% Reading the comment line with for loop
for i=3:5
    fgetl(f1);
end


%% Main data Reading
for j = 1:53                           % Defining total species data range for looping
    line_6 = fgetl(f1);                % Read 6th line

    Species = line_6(1:10);            % Defining species name
    Species_Name = deblank(Species);   % Remove all the blank spaces
    
    Species_Temp = line_6(49:75);      % Read Local temperate at 6th line
    % Split temperature using spaces between them
    local_values = strsplit(Species_Temp,' ');
    

    %% Define value for each local Temperature 
    % Use str2num to convert charecter array or string in numeric array
    Local_low_Temp = str2num(local_values{1});
    Local_mid_Temp = str2num(local_values{3});
    Local_hgh_Temp = str2num(local_values{2});
    
    % Storing the local temperatures in the from of array range
    Low_temp_range = linspace(Local_low_Temp,Local_mid_Temp,50);
    Hgh_temp_range = linspace(Local_mid_Temp,Local_hgh_Temp,50);
    

    %% Extracting all the coefficients from file
    line_7 = fgetl(f1);                 % Read 7th line 
    coeff_line_7= findstr(line_7,'E');  % Separate each coefficients useing E
    
    % Separate out all values of the coefficients in 7th line
    % Convert it into numeric value using str command
    a1  = line_7(1:coeff_line_7(1)+3);  
    a1  = str2num(a1);
    a2  = line_7(coeff_line_7(1)+4:coeff_line_7(2)+3);
    a2  = str2num(a2);
    a3  = line_7(coeff_line_7(2)+4:coeff_line_7(3)+3);
    a3  = str2num(a3);
    a4  = line_7(coeff_line_7(3)+4:coeff_line_7(4)+3);
    a4  = str2num(a4);
    a5  = line_7(coeff_line_7(4)+4:coeff_line_7(5)+3);
    a5  = str2num(a5);

    % Separate out all values of the coefficients in 8th line
    % Convert it into numeric value using str command    
    line_8 = fgetl(f1);
    coeff_line_8 = findstr(line_8,'E');
    a6  = line_8(1:coeff_line_8(1)+3);
    a6  = str2num(a6);
    a7  = line_8(coeff_line_8(1)+4:coeff_line_8(2)+3);
    a7  = str2num(a7);
    a8  = line_8(coeff_line_8(2)+4:coeff_line_8(3)+3);
    a8  = str2num(a8);
    a9  = line_8(coeff_line_8(3)+4:coeff_line_8(4)+3);
    a9  = str2num(a9);
    a10 = line_8(coeff_line_8(4)+4:coeff_line_8(5)+3);
    a10 = str2num(a10);
    
    % Separate out all values of the coefficients in 9th line
    % Convert it into numeric value using str command      
    line_9 = fgetl(f1);
    coeff_line_9 = findstr(line_9,'E');
    a11 = line_9(1:coeff_line_9(1)+3);
    a11 = str2num(a11);
    a12 = line_9(coeff_line_9(1)+4:coeff_line_9(2)+3);
    a12 = str2num(a12);
    a13 = line_9(coeff_line_9(2)+4:coeff_line_9(3)+3);
    a13 = str2num(a13);
    a14 = line_9(coeff_line_9(3)+4:coeff_line_9(4)+3);
    a14 = str2num(a14);
    
    % First 7 High Temperature Coefficients Range
    Coeff_Hgh = [a1 a2 a3 a4 a5 a6 a7];
    % Second 7 Low Temperature Coefficients Range
    Coeff_Low = [a8 a9 a10 a11 a12 a13 a14];
    
    
    %% Input for calculation
    % Universal gas constant (KJ/K-mol-k)
    R = 8.314;
    %  Temperature(K) Range
    LT = Low_temp_range;
    HT = Hgh_temp_range;
    % Coefficients Range
    CL = Coeff_Low;
    CH = Coeff_Hgh;
    
    
    %% Function for Specific Heat, Enthalpy & Entropy Calculation
    [Cp_low,Cp_hgh,H_low,H_hgh,S_low,S_hgh] = Output_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,LT,HT,R);
    
    
    %% Molecular Weight Function
    Mol_Wt = Mol_weight_function(Species_Name);
  

    %% Plotting all the figures
    
    % [1]-Plot- Specific Heat vs Temperature
    figure(1)
    plot(LT,Cp_low,'b','linewidth',2)
    hold on
    plot(HT,Cp_hgh,'r','linewidth',2)
    title({'Specific Heat vs Temperature',['Species Name= ',Species_Name,' & Molecular Weight= ',num2str(Mol_Wt)]})
    xlabel('Temperature (k)')
    ylabel('Specific Heat (J/K kg)')
    legend('Low Temperature','High Temperature')
    grid on
    hold off
    
    % [2]-Plot- Enthalpy vs Temperature
    figure(2)
    plot(LT,H_low,'b','linewidth',2)
    hold on
    plot(HT,H_hgh,'m','linewidth',2)
    title({'Enthalpy vs Temperature',['Species Name= ',Species_Name,' & Molecular Weight= ',num2str(Mol_Wt)]})
    xlabel('Temperature (k)')
    ylabel('Enthalpy (J/K)')
    legend('Low Temperature','High Temperature')
    grid on
    hold off
    
    % [3]-Plot- Entropy vs Temperature
    figure(3)
    plot(LT,S_low,'g','linewidth',2)
    hold on
    plot(HT,S_hgh,'r','linewidth',2)
    title({'Entropy vs Temperature',['Species Name= ',Species_Name,' & Molecular Weight= ',num2str(Mol_Wt)]})
    xlabel('Temperature (k)')
    ylabel('Entropy (J/K)')
    legend('Low Temperature','High Temperature')
    grid on
    hold off
    
    
    %% Making directory and saving the figures
    % Making a new folder for each species
    % Saving the plot in seaperate folder by each species name
    mkdir('E:\Study\skill-lync\Projects\9-File Parsing\NASA data parsing',Species_Name);
    cd(['E:\Study\skill-lync\Projects\9-File Parsing\NASA data parsing\',Species_Name]);                 
    saveas(figure(1),'Specific Heat vs Temperature.jpg');
    saveas(figure(2),'Enthalpy vs Temperature.jpg');
    saveas(figure(3),'Entropy vs Temperature.jpg');
    

    %% Writing Molecular Weight file 
    cd(['E:\Study\skill-lync\Projects\9-File Parsing\NASA data parsing\']);
    Molecular_Weight = fopen('Molecular_Weight.txt','a');
    fwrite(Molecular_Weight,sprintf('Molecular Weight of %s = %f\n',Species_Name,Mol_Wt));
    %fwrite(Molecular_Weight,sprintf('Molecular Weight of %s = %f\n',Species_Name,Mol_Wt);
    fclose(Molecular_Weight);
    
    % Print the molecular weight of the species in command window
    fprintf('Molecular Weight of %s = %f\n',Species_Name,Mol_Wt)
    
end   
% closing the file
fclose(f1);

Complete function program to calculate the enthalpy, entropy and specific heats

function [Cp_low, Cp_hgh, H_low, H_hgh, S_low, S_hgh] = Output_function(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,LT,HT,R);

%% Specific Heat Calculation
Cp_low = (a8+(a9.*LT)+(a10.*LT.^2)+(a11.*LT.^3)+(a12.*LT.^4))*R;
Cp_hgh = (a1+(a2.*HT)+(a3.*HT.^2)+(a4.*HT.^3)+(a5.*HT.^4))*R;

%% Enthalpy Calculation
H_low = (a8 + (a9.*LT)/2 + (a10.*LT.^2)/3 + (a11.*LT.^3)/4 + (a12.*LT.^4)/5 + a13./LT).*R.*LT;
H_hgh = (a1 + (a2.*HT)/2 + (a3.*HT.^2)/3 + (a4.*HT.^3)/4 + (a5.*HT.^4)/5 + a6./HT).*R.*HT;

%% Entropy Calculation
S_low = (a8.*log(LT) + (a9.*LT) + (a10.*LT.^2)/2 + (a11.*LT.^3)/3 + (a12.*LT.^4)/4 + a14)*R;
S_hgh = (a1.*log(HT) + (a2.*HT) + (a3.*HT.^2)/2 + (a4.*HT.^3)/3 + (a5.*HT.^4)/4 + a7)*R;

end    

Complete Function program to calculate molecular weight

function  Mol_Wt = Mol_weight_function(Species_Name);

% Mention all species elements use in data file
Species_Element = {'O','H','N','A','C','S'};
% provide atomic mass of above entities
Atomic_mass = [15.9994 1.00794 14.0067 39.9480 12.0110 32.0650];
Mol_Wt=0;                                            % consider Initial value of molecular wt as 0
for l = 1:length(Species_Name)                       % Define range of species length in colomn
    for m = 1:length(Species_Element)
        %strcmp - compares s1 and s2 and returns 1 (true) if the two are identical and 0 (false) otherwise
        if strcmp(Species_Name(l),Species_Element(m))% compare to entities using if statements
            Mol_Wt = Mol_Wt + Atomic_mass(m);        % Add each molecular weight/ atomic mass in every species 
            p = m;                                   % that value of molecular weight stored in string p
        end
    end
    q=str2num(Species_Name(l));                      % convert all carecter array into atomic masses in numeric form 
    if q > 1                                         % apply condition if atomic mass is greter than 1 then proceed it forward
        Mol_Wt = Mol_Wt + Atomic_mass(p)*(q-1);      % Molecular weight again calculated by having species value (q-1) 
    end
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.