Project 1 - Parsing NASA thermodynamic data
Enthalpy
function H = Enthalpy(A1,A2,A3,A4,A5,A6,A8,A9,A10,A11,A12,A13,T,R,gt_mid)
% Created function for enthalpy
%by comparing with the global_temperature
%Minimum co-effs will be taken if it is below global medium temperature
%Maximum co-effs will be taken if it is above global medium temperature
if T <= gt_mid
H = R * T((A8 +A9*T)/2) + ((A10*(T).^2)/3) + ((A11 * (T).^3)/4) + ((A12 * (T).^4)/5) + (A13/T);
else
H = R * T.*(((A1 + A2*T)*0.5) + ((A3*(T).^2)*0.33) + ((A4 * (T).^3)*0.25) + ((A5 * (T).^4)*0.2) + (A6./T));
end
Entropy
function S = Entropy(A1,A2,A3,A4,A5,A7,A8,A9,A10,A11,A12,T,R,gt_mid)
% Created function for entropy
%by comparing with the global_temperature
%Minimum co-effs will be taken if it is below global medium temperature
%Maximum co-effs will be taken if it is above global medium temperature
if T <= gt_mid
S = R((A8 * log(T)) + (A9 * T) + (A10 * (T)^2/2) + (A11 * (T)^3) + (A12 * (T)^4/4) + A14);
else
S = R * ((A1 * log(T)) + (A2 * T) + ((A3 * T.^2)/2)+ ((A4 * T).^3) + ((A5 * T).^4/4) + A7);
end
Specifc heat
function cp = specificheat(A1,A2,A3,A4,A5,A8,A9,A10,A11,A12,T,R,gt_mid)
% Created function for specific heat
%by comparing with the global_temperature
%Minimum co-effs will be taken if it is below global medium temperature
%Maximum co-effs will be taken if it is above global medium temperature
if T <= gt_mid
cp = R * (A8 +(A9*T) + (A10*(T).^2) + (A11 * (T).^3) + (A12 * (T).^4));
else
cp = R * (A1 +(A2*T) + (A3*(T).^2) + (A4 * (T).^3) + (A5 * (T).^4));
end
Molecular weight
function Molecularweight = molecular_weight(element)
atomic_name = [ 'H', 'C', 'O', 'N', 'A']; %Atomic name
%Hydrogen, Carbon, Oxygen, Nitrogen, Argon
Atomic_weight = [1.088 12.011 15.99 14.007 39.48]; %Standard atomic number
Molecularweight= 0; %Starting range of molecule
%Adding Atomic number to obtain particular value
for i = 1:length(element)
for j = 1:length(atomic_name)
if strcmpi(element(i),atomic_name(j)) %Comparing the values
Molecularweight = Molecularweight + Atomic_weight(j);
position = j;
end
end
n = str2double(element(i));
if n > 1
Molecularweight = Molecularweight + Atomic_weight(position)*(n-1);
end
end
end
Main code
clear all
close all
clc
%INPUTS
R=8.314;
ff = fopen('THERMO.dat','r'); %Opening data file
fgetl(ff); %Getting 1st line
gtline = fgetl(ff); % Getting global temperature line
gtline = strsplit(gtline,' '); %Splitting of temperatures
gt_low = str2num(gtline{2}); %Assigning the temperatures and converting to numbers
gt_mid = str2num(gtline{3});
gt_high = str2num(gtline{4});
fgetl(ff); %Skipping the header lines
fgetl(ff);
fgetl(ff);
T = linspace(gt_low,gt_high,1000); %Assigning temperature values
for i = 1:53
species = fgetl(ff); %Assigning the species line
m = strsplit(species,' ');
element = char(m(1));
lt_low = str2num(m{end-3});
lt_high = str2num(m{end-2});
lt_mid = str2num(m{end-1});
line1 = fgetl(ff);
p1 = findstr(line1,'E');
a1 = line1(1:p1(1)+3);
A1 = str2num(a1);
a2 = line1(p1(1)+4:p1(2)+3);
A2 = str2num(a2);
a3 = line1(p1(2)+4:p1(3)+3);
A3 = str2num(a3);
a4 = line1(p1(3)+4:p1(4)+3);
A4 = str2num(a4);
a5 = line1(p1(4)+4:p1(5)+3);
A5 = str2num(a5);
line2 = fgetl(ff);
p2 = strfind(line2,'E');
a6 = line2(1:p2(1)+3);
A6 = str2num(a6);
a7 = line2(p2(1)+4:p2(2)+3);
A7 = str2num(a7);
a8 = line2(p2(2)+4:p2(3)+3);
A8 = str2num(a8);
a9 = line2(p2(3)+4:p2(4)+3);
A9 = str2num(a9);
a10 = line2(p2(4)+4:p2(5)+3);
A10 = str2num(a10);
line3 = fgetl(ff);
p3 = findstr(line3,'E');
a11 = line3(1:p3(1)+3);
A11 = str2num(a11);
a12 = line3(p3(1)+4:p3(2)+3);
A12 = str2num(a12);
a13 = line3(p3(2)+4:p3(3)+3);
A13 = str2num(a13);
a14 = line3(p3(3)+4:p3(4)+3);
A14 = str2num(a14);
H = Enthalpy(A1,A2,A3,A4,A5,A6,A8,A9,A10,A11,A12,A13,T,R,gt_mid);
S = Entropy(A1,A2,A3,A4,A5,A7,A8,A9,A10,A11,A12,T,R,gt_mid);
cp = specificheat(A1,A2,A3,A4,A5,A8,A9,A10,A11,A12,T,R,gt_mid);
Molecularweight = molecular_weight(element);
%Read and write molecular list
sprintf('Molecular weight of %s is %d ', element, Molecularweight)
%Plotting (H,S,cp,temperature)
%saving in separate folder
q = sprintf('%s', element);
mkdir('Species');
cd('Species');
figure(1) %Plot 1 Enthalpy vs Temperature
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature');
ylabel('Enthalpy');
title(sprintf('Enthalpy vs Temperature range of %s',element));
grid on
saveas(1,'Enthalpy.jpg')
figure(2)%Plot 2 Entropy vs Temperature
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature');
ylabel('Entropy');
title(sprintf('Entropy vs Temperature range of %s',element));
grid on
saveas(2,'Entropy.jpg')
figure(3)%Plot 3 Specificheat vs Temperature
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature');
ylabel('Specificheat');
title(sprintf('Specificheat vs Temperature range of %s',element));
grid on
saveas(3,'Specificheat.jpg')
cd ..;
end
- Step-wise explanation of your code.
1) Basic codes for clearing previous files
2) Required data
3) Creating separate functions for Enthalpy, Entropy, Specific heat and Molecular weight
4) Opening the file in matlab using code (fopen)
5) Getting the lines and splitting the required data (fgetl, strsplit)
6) Converting characters to numbers (str2num)
7) Skipping the header lines
8) Generating temperature values for calculation (linspace)
9) Using 'for' loop for accesing the lines of temperatures and co- effecients
10) Splitting the values (findstr) and coverting to numbers (str2num)
11) Accessing the functions created for calculation (Enthalpy, Entropy, Specific heat and Molecular weight)
12) Printing the molecular weight in command window (sprintf)
13) Making the folder (mkdir) and saving the images plotted in different folders of different species name
14) Plotting the graph of Enthalpy vs Temperature, Entropy vs Temperature, Specific heat vs Temperature.
15) Ending the loop
Mention the errors faced while programming.
1) Not Using elements in formulas
2) Incorrect dimensions of matrix
3) Defining the path for saving plots
4) Finding and converting the co-effecients and name
- Steps taken to overcome those errors.
1) Using correct elements (,)(.)
2) using mkdir
3) Using findstr, splitstr, str2num
- Screenshots showing errors thrown in command window.
Couldn't take more screenshots of errors because of laptop issues


Refernces
skill-lync
mathworks
youtube
Took help from Sai (skill-lync)
Took help from Abubakar(skill-lync)