Challenge

Matlab coding for extracting coefficients of O2,CO2 and N2 form Parsing NASA thermodynamic data.

Aim: To write the matlab code for extracting data for coefficients of O2,N2 and CO2 species from the given NASA thermodynamic data.

Objective:

  • Function code to extract coefficients from given species.
  • Calculate molecular mass of each and every species.
  • Plot cp,s and h graph for local tempratures.

Theory:

Specific heat(cp): In thermodynamics, the specific heat capacity (symbol cp) of a substance is the heat capacity of a sample of the substance divided by the mass of the sample. Informally, it is the amount of energy that must be added, in the form of heat, to one unit of mass of the substance in order to cause an increase of one unit in temprature. The SI unit of specific heat is joule per kelvin and kilogram, J/(K kg). For example, at a temperature of 25 °C the heat required to raise the temperature of 1 kg of water by 1 K is 4179.6 joules so the specific heat capacity of water is 4179.6 J·kg−1·K−1.

Enthalpy(H): Enthalpy is a property of a thermodynamic system, defined as the sum of the system's internal energy and the product of its pressure and volume, H = U + pV. It is a convenient state system standardly used in many measurements in chemical, biological, and physical systems at a constant pressure. The pressure-volume term expresses the work required to establish the system's physical dimensions, i.e. to make room for it by displacing its surroundings. As a state function, enthalpy depends only on the final configuration of internal energy, pressure, and volume, not on the path taken to achieve it. The unit of measurement for enthalpy in the International System of units (SI) is the joule.

Entropy(S): Entropy is a scientific concept, as well as a measurable physical property that is most commonly associated with a state of disorder, randomness, or uncertainty. The term and the concept are used in diverse fields, from classical thermodynamics, where it was first recognized, to the microscopic description of nature in statistical physics, and to the principles of information theory. It has found far-ranging applications in chemistry and physics, in biological systems and their relation to life, in cosmology, economics, sociology, weather science, climate change, and information systems including the transmission of information in telecommunication.

Matlab coding for extracting co efficients from species

clear all
close all
clc


f1 = fopen('THERMO.dat','r');

%reading header from f1:
fgetl(f1);

%temprature line
t_line1= fgetl(f1);
a = strsplit(t_line1,' ');

global_low_temp = str2num(a{2});
global_mid_temp = str2num(a{3});
global_high_temp = str2num(a{4});

%ignoring the remaining 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);

% extracting coefficients from given data
for i = 1:53
    t_line2=fgetl(f1);
    s = strsplit(t_line2,' ');
    species_name = s{1};
    
    
    local_low_temp = str2num(s{(end-3)});
    local_mid_temp = str2num(s{(end-1)});
    local_high_temp = str2num(s{(end-2)});
    
    t_line3 = fgetl(f1);
    b=findstr(t_line3,'E');
    b1 = t_line3(1:b(1)+3);
    b1 = str2num(b1);
    
    b2 = t_line3(b(1)+4:b(2)+3);
    b2 = str2num(b2);
    
    b3 = t_line3(b(2)+4:b(3)+3);
    b3 = str2num(b3);
    
     b4 = t_line3(b(3)+4:b(4)+3);
     b4 = str2num(b4);
    
     b5 = t_line3(b(4)+4:b(5)+3);
     b5= str2num(b5);
     
      t_line3 = fgetl(f1);
    b=findstr(t_line3,'E');
    b6 = t_line3(1:b(1)+3);
    b6 = str2num(b6);
    
    b7 = t_line3(b(1)+4:b(2)+3);
    b7 = str2num(b7);
    
    b8 = t_line3(b(2)+4:b(3)+3);
    b8 = str2num(b8);
    
     b9 = t_line3(b(3)+4:b(4)+3);
     b9 = str2num(b9);
    
     b10= t_line3(b(4)+4:b(5)+3);
     b10= str2num(b10);
     
      t_line3 = fgetl(f1);
    b=findstr(t_line3,'E');
    b11 = t_line3(1:b(1)+3);
    b11 = str2num(b11);
    
    b12 = t_line3(b(1)+4:b(2)+3);
    b12 = str2num(b12);
    
    b13 = t_line3(b(2)+4:b(3)+3);
    b13 = str2num(b13);
    
     b14 = t_line3(b(3)+4:b(4)+3);
     b14 = str2num(b14);
     
     
     
     %specific heat
     %c_p =specific_heat(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R)
     
     %universal gas constant
     R = 8.314;
     
     t = linspace(local_low_temp,local_high_temp,1000);
    for i = 1:length(t)
        T=t(i); 
        %FOR specific heat
        c_p(i)=specific_heat(b1,b2,b3,b4,b5,b8,b9,b10,b11,b12,local_mid_temp,T,R);

        %for enthalpy
         H(i)=enthalpy(b1,b2,b3,b4,b5,b6,b8,b9,b10,b11,b12,b13,local_mid_temp,T,R);

        %for entropy
        S(i)=entropy(b1,b2,b3,b4,b5,b7,b8,b9,b10,b11,b12,b14,local_mid_temp,T,R);
        
       
    end
    %creating folder
    mkdir(species_name)
    cd(species_name)
    
    %plotting
    figure(1)
    plot(t,c_p)
    xlabel('temprature')
    ylabel('specific_heat')
    title ('temprature vs specific_heat')
    saveas(1,'specific_heat','jpg')
    
    figure(2)
    plot(t,H)
    xlabel('temprature')
    ylabel('enthalpy')
    title ('temprature vs enthalpy')
    saveas(2,'enthalpy','jpg')
    
    figure(3)
    plot(t,S)
    xlabel('temprature')
    ylabel('entropy')
    title ('temprature vs entropy')
    saveas(3,'entropy','jpg')
    
    cd ..
    
    %molecular weight
    [mol_wt] = molecular_weight(species_name);
   fprintf('Molecular weight of %s is %f.n',species_name,mol_wt)
      
   
end

%function for specific heat(cp)

function [c_p] = specific_heat(b1,b2,b3,b4,b5,b8,b9,b10,b11,b12,local_mid_temp,T,R)
    
if(T>local_mid_temp)
     c_p = R*(b1+b2*T+b3*T^2+b4*T^3+b5*T^4);
     
else
    c_p = R*(b8+b9*T+b10*T^2+b11*T^3+b12*T^4);

end

%function for enthalpy(H)

function [H] = enthalpy(b1,b2,b3,b4,b5,b6,b8,b9,b10,b11,b12,b13,local_mid_temp,T,R)

if (T>local_mid_temp)
    H =(b1+b2*T/2+b3*T^2/3+b4*T^3/4+b5*T^4/5+b6/T)*R*T;
    
else
    H = (b8+b9*T/2+b10*T^2/3+b11*T^3/4+b12*T^4/5+b13/T)*R*T;
    
end

%function for entropy(S)

function [S] = entropy(b1,b2,b3,b4,b5,b7,b8,b9,b10,b11,b12,b14,local_mid_temp,T,R)

if(T>local_mid_temp)
    S = (b1*log(T)+b2*T+b3*T^2/2+b4*T^3/3+b5*T^4/4+b7)*R;
    
else
    S = (b8*log(T)+b9*T+b10*T^2/2+b11*T^3/3+b12*T^4/4+b14)*R;
end

%function for molecular weight

function [total_mol_wt] = molecular_weight(species_names)

atomic_mass =[15.9994,14.0067,12.011,1.00797,39.948];
elements =['O','N','C','H','A'];
mol_wt = zeros(1,length(species_names));
for i = 1:length(species_names)
    [num,status] = str2num(species_names(i));
    if status == 0
    for j = 1:length(elements)
        if species_names(i)== elements(j)
        mol_wt(i) = mol_wt(i)+atomic_mass(j);
    end
    end
    else
        mol_wt(i-1) = mol_wt(i-1)*num;
        
        
    end
end
total_mol_wt = sum(mol_wt);
end

Explanation:

  • First we used fopen syntax to open the thermo file and using fgetl syntax we read the header line then followed to it using t_line syntax we read the global temprature values alos strsplit to get the single cell arrays and converted those cell arrays to num using str2num syntax.
  • Then we read the 3 line using fgetl syntax and we calculated the local temprature values then we specified the species name.
  • Then used the forloop for all 53 species.Using function syntax we calculated specific heat entropy and enthalpy and called the function in main programm.
  • We created the folder using mkdir and cd syntax and then plotted all the graphs and saved those plots in created folder usingh saveas syntax.
  • We calculated the molecular weight of the species and printed it using fprintf syntax.For calculating molecular weight we created function in that we used forloop and if else syntax.

Molecular weight:

  • molecular weight of O2 is 31.998800.
  • molecular weight of N2 is 28.013400.
  • molecular weight of CO2 is 44.009800.

Output:

FOR O2

FOR N2

FOR CO2

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.