Project

Parsing and visualising NASA thermodynamic data using MATLAB

AIM: To parse NASA thermodynamic data file and calculate the thermodynamic properties of various gas species namely Specific Heat, Enthalpy and Entropy

OBJECTIVE:

  • To write a function in MATLAB that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file
  • To calculate the molecular weight of each species and display it in the command window
  • To plot the Specific Heat, Enthalpy, and Entropy for the local temperature range specific for each species
  • To save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name automatically in the code

FORMULAE

  • `(C_p)/R= a_1 + a_2*T+a_3*T^2+a_4*T^3+a_5*T^5`
  • `H/(R*T) = a_1 + a_2*T/2 + a_3*T^2/3+a_4*T^3/4+a_5*T^4/5+a_6/T`
  • `S/R = a1*lnT + a_2*T +a_3*T^2/2 +a_4*T^3/3+a5*T^4/4+a_7`
    • where:
    • `R`=8.314
    • `T`-temperature
    • `a_1 - a_14`- temperature coefficients
    • `a_1 - a_7`- high temperatre coefficients
    • `a_8 - a_14`- low temperature coefficients

Inputs

  • Global Temperatures
  • Name of the Species or Elements
  • Local Temperature of each species
  • Temperature coefficients for each species

Outputs

  • Specific Heat - Cp
  • Enthalpy - H
  • Entropy - S
  • The molecular weight of each species or element

PROGRAM CODE

Main Program

%clear all the previous data or stores values
clear all
close all
clc

%Open the the THERMO.dat file
f1=fopen('THERMO.dat','r')

%skip the first line of the file
fgetl(f1);

%reading the tempersture values distingushed as local,average and high temperatures
line=fgetl(f1);
a=strsplit(line,' ');
global_low_temp=str2num(a{2});
global_average_temp=str2num(a{3});
global_high_temp=str2num(a{4});
fgetl(f1);
fgetl(f1);
fgetl(f1);

%extracting data for each gas
for k=1:53
    
    %step7
    line=fgetl(f1);
    a=strsplit(line,' ');
    Gas=a(1);
    b=findstr(line,'.');
    local_low_temp=str2num(line(b(1)-4:b(1)+4));
    local_high_temp=str2num(line(b(2)-4:b(2)+4));
    local_average_temp=str2num(line(b(3)-4:b(3)+4));
    
    %extracting all 14 coefficients
    ct=1;
    for j=1:3
        line=fgetl(f1);
        b=strfind(line,'E');
        for i=1:length(b)
            if ct<=7
                Ha(ct)=str2num(line(b(i)-11:b(i)+3));
                ct=ct+1;
            else
              La(ct-7)=str2num(line(b(i)-11:b(i)+3));
              ct=ct+1;
            
            end
        end
    end
    
    %initializing specific heat,entropy and enthalpy values to zero
    cp=0;
    H=0;
    S=0;
    
    %calling the function where cp,H and S are calculated
    [Cp,H,S] = coefficient(local_low_temp,local_average_temp,local_high_temp,La,Ha);
    
    %molecular weight calculating function
    M=Weight(Gas);
    
    %plotting
    GAS=char(Gas);
    T=local_low_temp:local_high_temp;
    current=pwd;
    mkdir(GAS);
    cd(GAS)
    figure(1);
    plot(T,Cp,'color','g','linewidth',2);
    txt1=['Molecular Weight:' num2str(M)];
    text(500,max(Cp)*0.98,txt1,'color','red');
    grid on
    
    xlabel('Temperature(K)');
    ylabel('Specific Heat(J)');
    title('Specific Heat VS Temperature');
    
    figure(2);
    plot(T,H,'color','r','linewidth',2);
    txt1=['Molecular Weight:' num2str(M)];
    text(500,max(H)*0.98,txt1,'color','yellow');
    grid on
    xlabel('Temperature(K)');
    ylabel('Enthalpy(J/K)');
    title('Enthalpy VS Temperature');
    
    figure(3);
    plot(T,S,'color','b','linewidth',2);
    txt1=['Molecular weight:' num2str(M)];
    text(500,max(S)*0.98,txt1,'color','blue');
    grid on
    xlabel('Temperature(K)');
    ylabel('Entropy(J/K)');
    title('Entropy VS Temperature');
    
    saveas(figure(1),'Cp.jpg');
    saveas(figure(2),'H.jpg');
    saveas(figure(3),'S.jpg');
    
   cd(current);
end

Coefficient

%function to calculate the Cp,H,S.
function [Cp,H,S]=coefficient(local_low_temp,local_average_temp,local_high_temp,La,Ha)
for i=1:local_high_temp-local_low_temp+1
    
    R=8.34;
    T=i+local_low_temp-1;
    %for lower temperature values
     if T<= local_average_temp
         Cp(i)=R*(La(1)+La(2)*T+La(3)*T^2+La(4)*T^3+La(5)*T^4);
         H(i)=R*T*(La(1)+(La(2)*T)/2+(La(3)*T^2)/3+(La(4)*T^3)/4+(La(5)*T^4)/5+La(6)/T);
         S(i)=R*(La(1)*log(T)+(La(2)*T)+((La(3)*T^2)/2)+((La(4)*T^3)/3)+((La(5)*T^4)/4)+(La(7)));
         
         %for higher temperature values
         
     else
         Cp(i)=R*(Ha(1)+Ha(2)*T+Ha(3)*T^2+Ha(4)*T^3+Ha(5)*T^4);
         H(i)=R*T*(Ha(1)+((Ha(2)*T)/2)+(Ha(3)*T^2)/3+(Ha(4)*T^3)/4+(Ha(5)*T^4)/5+(Ha(6)/T));
         S(i)=R*(Ha(1)*log(T)+(Ha(2)*T)+(Ha(3)*T^2)/2+(Ha(4)*T^3)/3+(Ha(5)*T^4)/4+(Ha(7)));
         
     end
end
end

Weight calculation

%function to calculate molecular weight
function Mo=Weight(Gas)

Weight=[1 12 14 16 32 0 40];
Gas1=['H' 'C' 'N' 'O' 'S' 'A' 'R'];
number=[1 2 3 4];

M=0;
GAS=char(Gas);
for i=1:length(GAS)
    for j=1:length(Gas1)
    if GAS(i)==Gas1(j)
        M(i)= Weight(j);
    end
    for j=1:length(number)
        if str2double(GAS(i))==number(j)
            M(i)=M(i-1)*(number(j)-1);
        end
    end
end
end

Mo=sum(M);
end

PROCEDURE:

  1. Initially we will opening the given THERMO.dat file using fopen command and then we will extract all the information provided in the file using fgetl command.
  2. First we we will get the global temperature by using strslplit command and then we skip some lines using the fget command.
  3. Now we make use of for loop command to get all temperature and coefficients that are presnt in our file.
  4. For getting temperature and coefficients we use finstr command inorder to input the accurate spot and specifying it so that we can get all the data.
  5. Now the datas which are obtanied from the file will be of string format which we need to convert into numbers by using str2num command.and by this we will get the temperature and coefficient of all the species.
  6. Now we make use of if command to create file for each species, in case if the folder exist it will simply save as plot or else it will create a folder to save the plot.
  7. Now the specific heat,enthalpy ,entropy function is calculated and gets plotted.
  8. The three figures are plotted using plot command and each species name is displayed using title command.And further it will named using xlabel and ylabel command.
  9. Now we are changing the dierectory using cd command and then we will save the plots as our images using save command.
  10. Finally we will again change the directory using cd command to calculate molecular weight of each species using the function program and closing the file using fclose command.

RESULTS:

PLOTS

`NO_2`

`HO_2`

Only minor synatx errors were encountered

CONCLUSION:

Using the MATLAB program above, we were able to plot graphs for each of the cases:- Specific Heat, Enthalpy and Entropy for all the molecules above.

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.