Project

NASA PROJECT

clear all
close all
clc
 
f1 = fopen(\'THERMO.dat\')
Heading = fgetl(f1);
 
% Global Temp
global_temp = fgetl(f1);
for i = 1:3
    comments{i} = fgetl(f1);
end
a = strsplit(global_temp,\' \');
global_low_temp = str2num(a{2});
global_mid_temp = str2num(a{3});
global_high_temp = str2num(a{4});
 
%At. Mass
O = 16;
 
for k=1:53
    v1 = fgetl(f1);
    v2 = fgetl(f1);
    v3 = fgetl(f1);
    v4 = fgetl(f1);
    v{1} = v2;
    v{2} = v3;
    v{3} = v4;
    
    % Species
    n = strsplit(v1,\' \');
    Name{k} = n{1};
    
    % Getting Temp for Species
    G = findstr(v1,\'.\');
    x = v1(G(1)-3:G(1)+23);
    G_temp = strsplit(x,\'  \');
    G_temp_low(k) = str2num(G_temp{1});
    G_temp_mid(k) = str2num(G_temp{2});
    G_temp_high(k) = str2num(G_temp{3});
    
    % Species Coefficients
    for j = 1:3
        E = strfind(v{j},\'E\');
        p(1) = -3;
        o = v{j};
        for i = 1:length(E)
            p(1+i) = E(i);
            Species{i} = o(p(i)+4:p(i+1)+3);
            sp(k,j,i)= str2num(Species{i});
        end
        clearvars E;
        clearvars o;
    end
    clearvars v1 v2 v3 v4 v G_temp;
end
 
fclose(f1)
 
% Solve
R = 8.314;
 
% High Temp Coefficients Arranging
for k = 1:53
    for i = 1:5
        High_temp_coefficiants(k,i) = sp(k,1,i);
    end
    for i = 6:7
        High_temp_coefficiants(k,i) = sp(k,2,i-5);
    end
end
 
% Low Temperature Coefficients Arrangment
for k = 1:53
    for i = 1:3
        low_temp_coefficiants(k,i) = sp(k,2,i+2);
    end
    for i = 4:7
        low_temp_coefficiants(k,i) = sp(k,3,i-3);
    end
end
 
% Calculation of Cp
for k = 1:53
    
    % High Temp
    Cp_high(k) = R*(High_temp_coefficiants(k,1)+High_temp_coefficiants(k,2)*G_temp_high(k)+High_temp_coefficiants(k,3)*G_temp_high(k)^2+High_temp_coefficiants(k,4)*G_temp_high(k)^3+High_temp_coefficiants(k,5)*G_temp_high(k)^4);
    Cp_low(k) = R*(low_temp_coefficiants(k,1)+low_temp_coefficiants(k,2)*G_temp_low(k)+low_temp_coefficiants(k,3)*G_temp_low(k)^2+low_temp_coefficiants(k,4)*G_temp_low(k)^3+low_temp_coefficiants(k,5)*G_temp_low(k)^4);
end
 
% Calculation of H
for k = 1:53
     % High Temperature 
    H_high(k) = R*G_temp_high(k)*(High_temp_coefficiants(k,1)+High_temp_coefficiants(k,2)*(G_temp_high(k))/2+High_temp_coefficiants(k,3)*(G_temp_high(k)^2)/3+High_temp_coefficiants(k,4)*(G_temp_high(k)^3)/4+High_temp_coefficiants(k,5)*(G_temp_high(k)^4)/5+High_temp_coefficiants(k,6)/G_temp_high(k));
    H_low(k) = R*G_temp_low(k)*(low_temp_coefficiants(k,1)+low_temp_coefficiants(k,2)*(G_temp_low(k))/2+low_temp_coefficiants(k,3)*(G_temp_low(k)^2)/3+low_temp_coefficiants(k,4)*(G_temp_low(k)^3)/4+low_temp_coefficiants(k,5)*(G_temp_low(k)^4)/5+low_temp_coefficiants(k,6)/G_temp_low(k));
end
 
% Calculation of S
for k = 1:53
     % High Temperature 
    S_high(k) = R*(High_temp_coefficiants(k,1)*log(G_temp_high(k))+High_temp_coefficiants(k,2)*G_temp_high(k)+High_temp_coefficiants(k,3)*(G_temp_high(k)^2)/2+High_temp_coefficiants(k,4)*(G_temp_high(k)^3)/3+High_temp_coefficiants(k,5)*(G_temp_high(k)^4)/4+High_temp_coefficiants(k,7));
    S_low(k) = R*(low_temp_coefficiants(k,1)*log(G_temp_low(k))+low_temp_coefficiants(k,2)*G_temp_low(k)+low_temp_coefficiants(k,3)*(G_temp_low(k)^2)/2+low_temp_coefficiants(k,4)*(G_temp_low(k)^3)/3+low_temp_coefficiants(k,5)*(G_temp_low(k)^4)/4+low_temp_coefficiants(k,7));
end
 
% Species Name Arrangment
names_species = categorical(Name);
for k =1:53
    for i=1
        cp(k,i)= Cp_high(k);
        H(k,i) = H_high(k);
        S(k,i) = S_high(k);
    end
    for i=2
        cp(k,i)= Cp_low(k);
        H(k,i) = H_low(k);
        S(k,i) = S_low(k);
    end
end
 
 
% Plotting
figure(1)
bar(names_species,cp)
legend(\'Cp for High Temp\',\'Cp for Low temp\')
saveas(gcf,\'Cp.jpg\')
 
figure(2)
bar(names_species,H)
legend(\'H for High Temp\',\'H for Low temp\')
saveas(gcf,\'H.jpg\')
 
figure(3)
bar(names_species,S)
legend(\'S for High Temp\',\'S for Low temp\')
saveas(gcf,\'S.jpg\')
 
 
% Species\' Molecular mass
for k = 1:53
    nam = Name{k};
   Carbon = strfind(nam,\'C\');
   Hydrogen = strfind(nam,\'H\');
   Oxygen = strfind(nam,\'O\');
   Nitrogen = strfind(nam,\'N\');
   Argon = strfind(nam,\'AR\');
   
   %Mass of Carbon
   if ~isempty(Carbon)
       Mass_C = 0;
       for i = 1:length(Carbon)
           if ischar(nam(Carbon(i))+1) ~= 0 && Carbon(i)+1 > length(nam)
               num = str2num(nam(Carbon(1))+1);
               Mass_C = 12.011*num +Mass_C;
               num = 0;
           else
               Mass_C = 12+Mass_C;
           end
       end
   else
       Mass_C = 0;
   end
   
 %Mass of Hydrogen
   if ~isempty(Hydrogen)
       Mass_H = 0;
       for i = 1:length(Hydrogen)
           if ischar(nam(Hydrogen(i))+1) ~= 0 && Hydrogen(i)+1 < length(nam)
               num = str2num(nam(Hydrogen(1))+1);
               Mass_H = 12*num +Mass_H;
               num = 0;
           else
               Mass_H = 1.008+Mass_H;
           end
       end
   else
       Mass_H = 0;
   end
   
   %Mass of Nitrogen
   if ~isempty(Nitrogen)
       Mass_N = 0;
       for i = 1:length(Nitrogen)
           if ischar(nam(Nitrogen(i))+1) ~= 0 && Nitrogen(i)+1 < length(nam)
               num = str2num(nam(Nitrogen(1))+1);
               Mass_N = 12*num +Mass_N;
               num = 0;
           else
               Mass_N = 14.007+Mass_N;
           end
       end
   else
       Mass_N = 0;
   end
   
    %Mass of Oxygen
   if ~isempty(Oxygen)
       Mass_O = 0;
       for i = 1:length(Oxygen)
           if ischar(nam(Oxygen(i))+1) ~= 0 && Oxygen(i)+1 < length(nam)
               num = str2num(nam(Oxygen(1))+1);
               Mass_O = 15.999*num +Mass_O;
               num = 0;
           else
               Mass_O = 15.999+Mass_O;
           end
       end
   else
       Mass_O = 0;
   end
   
    %Mass of Argon
   if ~isempty(Argon)
       Mass_AR = 0;
       for i = 1:length(Argon)
           if ischar(nam(Argon(i))+1) ~= 0 && Argon(i)+1 < length(nam)
               num = str2num(nam(Argon(1))+1);
               Mass_AR = 39.948*num +Mass_AR;
               num = 0;
           else
               Mass_AR = 39.948+Mass_AR;
           end
       end
   else
       Mass_AR = 0;
   end
           
   molecule_mass(k) = Mass_C+Mass_H+Mass_O+Mass_N+Mass_AR;
  
   
end
 
% Writing Results
f2 = fopen(\'result.txt\',\'w\');
fprintf(f2,\'%s %s %s %s %s %s %s\',\'Species\',\'Cp High Temp\',\'Cp Low Temp\',\'Enthalpy High Temp\',\'Enthalpy Low Temp\',\'Entropy High Temp\',\'Entropy Low Temp\');
fprintf(f2,\'\\n\');
for k= 1:53
    % Since FPRINTF Do not support array
    N = Name{k};
    CPH = Cp_high(k);
    CPL = Cp_low(k);
    HH = H_high(k);
    HL = H_low(k);
    SH = S_high(k);
    SL = S_low(k);
    
    fprintf(f2,\'%s %f %f %f %f %f %f\',N,CPH,CPL,HH,HL,SH,SL);
    fprintf(f2,\'\\n\');
    
    clearvars N CPH CPL HH HL SH SL;
end
fclose(f2)

\"\"

\"\"

\"\"

\"\"

STEPWISE EXPLANATION OF THE CODE

Code starts with clear all close all & clc

The data inside the file is read towards a function variable f1 using fopen command .And a variable heading using fgetl command of f1. fgetl command Read line from file, removing newline characters

f1 = fopen(\'THERMO.dat\')

Heading = fgetl(f1);

The value of fgetl(f1) is read towards the variable global_temp.

global_temp = fgetl(f1);

Each value of fgetl(f1) is read towards the variable comments using a counter I in a for loop

for i = 1:3

comments{i} = fgetl(f1);

end

Using strsplit command, which Split string characters of global_temp at specified delimiter,including the space is separated and all these characters are read to a column matrix a. The second, third and fourth elements of matrix a are read to variables global_low_temp, global_mid_temp and global_high_temp respectively using the command str2num, which convert character array to numeric array

a = strsplit(global_temp,\' \');

global_low_temp = str2num(a{2});

global_mid_temp = str2num(a{3});

global_high_temp = str2num(a{4});

Defining the Atomic Mass O = 16;

Starting a for loop governed by a counter k. Reading the value fgetl(f1) to v1, v2, v3, v4 to the corresponding values. And then taking values of v2, v3 and v4 to v{1}, v{2} and v{3}.

for k=1:53

v1 = fgetl(f1);

v2 = fgetl(f1);

v3 = fgetl(f1);

v4 = fgetl(f1);

v{1} = v2;

v{2} = v3;

v{3} = v4;

For getting the species name , using strsplit command of v1 to n, to get the characters.And the Name for each k is given to n

n = strsplit(v1,\' \');

Name{k} = n{1};

For getting the temperatures of species,using the findstr command, which find one string within another, and reading it to G.

G = findstr(v1,\'.\');

Finding the value of x using the equation x=v1 of G(1)-3 to G(1)+23

x = v1(G(1)-3:G(1)+23);

Using the strsplit command which splits the characters of x to G_temp

G_temp = strsplit(x,\' \')

Reading the corresponding values of G_temp ( G_temp_low(k), G_temp_mid(k)

and G_temp_high(k)) using the command str2num, which convert character array to numeric array

G_temp_low(k) = str2num(G_temp{1});

G_temp_mid(k) = str2num(G_temp{2});

G_temp_high(k) = str2num(G_temp{3});

For finding a species coefficient, starting a for loop again using a counter j from 1to 3 using the strfind command, which find one string within another, and reading v{j} to E.Defining p(1)as -3 and reading v{j}

for j = 1:3

E = strfind(v{j},\'E\');

p(1) = -3;

o = v{j};

Starting a for loop with counter i from 1 to length of the string E. Reading thevalue of E(i) to p(1+i). Reading the value of o from p(i)+4 to p(i+1)+3 towards a variable Species{i}. Then reading the value of number characters of Species{i} (using str2num ) to a 3D array sp(k,j,i).

for i = 1:length(E)

p(1+i) = E(i);

Species{i} = o(p(i)+4:p(i+1)+3);

sp(k,j,i)= str2num(Species{i});

Clear variables from memory

clearvars E;

clearvars o;

clearvars v1 v2 v3 v4 v G_temp;

Since clearvars was used for E, o, v1, v2, v3, v4, v, G_temp cannot be seen in the workspace

Closes the file f1 fclose(f1)

Defining the Universal Gas Constant R = 8.314

Arranging the High Temperature Cofficients in a 3D array,using two for loops i( 1 to 5) and i(6 to 7), these loops operate on another loop governed by k(1 to 53)

for k = 1:53

for i = 1:5

High_temp_coefficiants(k,i) = sp(k,1,i);

end

for i = 6:7

High_temp_coefficiants(k,i) = sp(k,2,i-5);

end

end

Arranging the Low Temperature Cofficients in a 3D array,using two for loops i( 1 to 5) and i(4 to 7), these loops operate on another loop governed by k(1 to 53)

for k = 1:53

for i = 1:3

low_temp_coefficiants(k,i) = sp(k,2,i+2);

end

for i = 4:7

low_temp_coefficiants(k,i) = sp(k,3,i-3);

end

end

Calculation of the Molar Specific Heat at constant pressure is done using another for loop using counter k from 1 to 53.This counter is find the High value & Low value of Molar Specific Heat(Cp_high(k) & Cp_low(k)). Also the 53x7 variables High_temp_coefficiants and low_temp_coefficiants and 1x53 variables G_temp_high and G_temp_low(k) are used and these both are governed by the counter k.

for k = 1:53

Cp_high(k) = R*(High_temp_coefficiants(k,1)+High_temp_coefficiants(k,2)*G_temp_high(k)+High_temp_coefficiants(k,3)*G_temp_high(k)^2+High_temp_coefficiants(k,4)*G_temp_high(k)^3+High_temp_coefficiants(k,5)*G_temp_high(k)^4);

Cp_low(k) = R*(low_temp_coefficiants(k,1)+low_temp_coefficiants(k,2)*G_temp_low(k)+low_temp_coefficiants(k,3)*G_temp_low(k)^2+low_temp_coefficiants(k,4)*G_temp_low(k)^3+low_temp_coefficiants(k,5)*G_temp_low(k)^4);

end

For calculation of Enthalpy pressure is done using another for loop using counter k from 1 to 53. This counter is find the High value & Low value of Enthalpy (H_high(k)& H_low(k)). )). Also the 53x7 variables High_temp_coefficiants and low_temp_coefficiants and 1x53 variables G_temp_high and G_temp_low(k) are used and these both are governed by the counter k.

S_high(k) = R*(High_temp_coefficiants(k,1)*log(G_temp_high(k))+High_temp_coefficiants(k,2)*G_temp_high(k)+High_temp_coefficiants(k,3)*(G_temp_high(k)^2)/2+High_temp_coefficiants(k,4)*(G_temp_high(k)^3)/3+High_temp_coefficiants(k,5)*(G_temp_high(k)^4)/4+High_temp_coefficiants(k,7));

S_low(k) = R*(low_temp_coefficiants(k,1)*log(G_temp_low(k))+low_temp_coefficiants(k,2)*G_temp_low(k)+low_temp_coefficiants(k,3)*(G_temp_low(k)^2)/2+low_temp_coefficiants(k,4)*(G_temp_low(k)^3)/3+low_temp_coefficiants(k,5)*(G_temp_low(k)^4)/4+low_temp_coefficiants(k,7));

For the arrangement of species name

names_species = categorical(Name);

That is a array that contains values assigned to categories

Cp_high, Cp_low, H_high, H_low, S_high and S_low using cp, H, S is calculated using two for loops

names_species = categorical(Name);

for k =1:53

for i=1

cp(k,i)= Cp_high(k);

H(k,i) = H_high(k);

S(k,i) = S_high(k);

end

for i=2

cp(k,i)= Cp_low(k);

H(k,i) = H_low(k);

S(k,i) = S_low(k);

end

end

Next is plotting. Three separate figures are defined.Bar plots are done of the following datas,using bar command.

names_species v/s cp

names_species v/s H

names_species v/s S

Individual bar are named using legend command & plots are saved using saveas

figure(1)

bar(names_species,cp)

legend(\'Cp for High Temp\',\'Cp for Low temp\')

saveas(gcf,\'Cp.jpg\')

figure(2)

bar(names_species,H)

legend(\'H for High Temp\',\'H for Low temp\')

saveas(gcf,\'H.jpg\')

figure(3)

bar(names_species,S)

legend(\'S for High Temp\',\'S for Low temp\')

saveas(gcf,\'S.jpg\')

A for loop is started with a counter k 1 to 53

Using strfind command, the symbols for Carbon, Hydrogen, Oxygen, Nitrogen and Argon are found out.

Carbon = strfind(nam,\'C\');

Hydrogen = strfind(nam,\'H\');

Oxygen = strfind(nam,\'O\');

Nitrogen = strfind(nam,\'N\');

Argon = strfind(nam,\'AR\');

A if loop is used to find out the Mass of Carbon. Firstly it is checked whether the array is empty using isempty, if yes Mass of Carbon is assigned as 0. A for loop is used inside it which changes from I to length(Carbon).

if ~isempty(Carbon)

Mass_C = 0;

for i = 1:length(Carbon)

Again a if loop is used. A ischar command is used determine if input is character array, if yes

if ischar(nam(Carbon(i))+1) ~= 0 && Carbon(i)+1 > length(nam)

A str2numis used in nam(Carbon(1))+1 and is read towards a variable num. A Mass of carbon is calculated.Then the value of num is set to 0 again for counter operations about i.

num = str2num(nam(Carbon(1))+1);

Mass_C = 12.011*num +Mass_C;

num = 0;

else condition is Mass_C = 12+Mass_C;

A if loop is used to find out the Mass of Hydrogen. Firstly it is checked whether the array is empty using isempty, if yes Mass of Hydrogen is assigned as 0. A for loop is used inside it which changes from I to length(Hydrogen).

if ~isempty(Hydrogen)

Mass_H = 0;

for i = 1:length(Hydrogen)

Again a if loop is used. A ischar command is used determine if input is character array, if yes

if ischar(nam(Hydrogen(i))+1) ~= 0 && Hydrogen(i)+1 < length(nam)

A str2numis used in nam(Hydrogen(1))+1 and is read towards a variable num. A Mass of Hydrogen is calculated.Then the value of num is set to 0 again for counter operations about i.

num = str2num(nam(Hydrogen(1))+1);

Mass_H = 12*num +Mass_H;

num = 0;

else condition is Mass_H = 1.008+Mass_H;

A if loop is used to find out the Mass of Nitrogen. Firstly it is checked whether the array is empty using isempty, if yes Mass of Nitrogen is assigned as 0. A for loop is used inside it which changes from I to length(Nitrogen).

if ~isempty(Nitrogen)

Mass_N = 0;

for i = 1:length(Nitrogen)

Again a if loop is used. A ischar command is used determine if input is character array, if yes

if ischar(nam(Nitrogen(i))+1) ~= 0 && Nitrogen(i)+1 < length(nam)

A str2numis used in nam(Nitrogen(1))+1 and is read towards a variable num. A Mass of Nitrogen is calculated.Then the value of num is set to 0 again for counter operations about i.

num = str2num(nam(Nitrogen(1))+1);

Mass_N = 12*num +Mass_N;

num = 0;

else condition is Mass_N = 14.007+Mass_N;

A if loop is used to find out the Mass of Oxygen. Firstly it is checked whether the array is empty using isempty, if yes Mass of Oxygen is assigned as 0. A for loop is used inside it which changes from I to length(Oxygen).

if ~isempty(Oxygen)

Mass_0 = 0;

for i = 1:length(Oxygen)

Again a if loop is used. A ischar command is used determine if input is character array, if yes

if ischar(nam(Oxygen(i))+1) ~= 0 && Oxygen(i)+1 < length(nam)

A str2numis used in nam(Oxygen(1))+1 and is read towards a variable num. A Mass of Oxygen is calculated.Then the value of num is set to 0 again for counter operations about i.

num = str2num(nam(Oxygen(1))+1);

Mass_O = 15.999*num +Mass_O;

num = 0;

else condition is Mass_O = 15.999+Mass_O;

A if loop is used to find out the Mass of Argon. Firstly it is checked whether the array is empty using isempty, if yes Mass of Argon is assigned as 0. A for loop is used inside it which changes from I to length(Argon).

if ~isempty(Argon)

Mass_0 = 0;

for i = 1:length(Argon)

Again a if loop is used. A ischar command is used determine if input is character array, if yes

if ischar(nam(Argon(i))+1) ~= 0 && Argon(i)+1 < length(nam)

A str2numis used in nam(Oxygen(1))+1 and is read towards a variable num. A Mass of Oxygen is calculated.Then the value of num is set to 0 again for counter operations about i.

num = str2num(nam(Argon(1))+1);

Mass_AR = 39.948*num +Mass_AR;

num = 0;

else condition is Mass_AR = 39.948+Mass_AR;

Total Molecular Mass is calculated

molecule_mass(k) = Mass_C+Mass_H+Mass_O+Mass_N+Mass_AR;

For writing the result a new file is opened in f2 using fopen and Species, Cp High Temperature, Cp Low Temperature, Enthalpy High Temperature, Enthalpy Low Temperature , Entropy High Temperature, Entropy Low Temperature.

f2 = fopen(\'result.txt\',\'w\');

fprintf(f2,\'%s %s %s %s %s %s %s\',\'Species\',\'Cp High Temp\',\'Cp Low Temp\',\'Enthalpy High Temp\',\'Enthalpy Low Temp\',\'Entropy High Temp\',\'Entropy Low Temp\');

fprintf(f2,\'\\n\');

Final printing is done which is governed by a counter k from1 to 53 and all the values are printed after reading to some temporary variables CPH, CPL, HH, HL,SH, SL which will take the values of Cp_high, Cp_low, H_high, H_low, S_high, S_low respectively. Temporary variables are cleared using clearvars

CPH = Cp_high(k);

CPL = Cp_low(k);

HH = H_high(k);

HL = H_low(k);

SH = S_high(k);

SL = S_low(k);

fprintf(f2,\'%s %f %f %f %f %f %f\',N,CPH,CPL,HH,HL,SH,SL);

fprintf(f2,\'\\n\');

clearvars N CPH CPL HH HL SH SL;

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.