Challenge

Project 1 - Parsing NASA thermodynamic data

CODE:

Main CODE:

%opening file

x = fopen('THERMO.dat','r');
glo_temp=fgetl(x);
global_temp=fgetl(x);
%getting global temperature values using split
t = strsplit(global_temp);
global_low_temp = str2double(t{2});
global_mid_temp = str2double(t{3});
global_high_temp = str2double(t{4});
for i = 1:3
information = fgetl(x);
disp(information)
end
%getting all the molecules
for z = 1:53
dl1 = fgetl(x);
molecule = strsplit(dl1);
formula = molecule{1};
%molecule name
%array is flipped as the molecules not in the same lenght from the...
%start so flipped as they are placed in same position for all the...
%molecules
a = flip(molecule);
%getting local temp values
local_low_temp = str2double(a{4});
local_mid_temp = str2double(a{2});
local_high_temp = str2double(a{3});
%values for temperature
t = linspace(local_low_temp,local_high_temp,500);
%getting the coefficients
%%splitting them with variable E
dl2 = fgetl(x);
a = strfind(dl2,'E');
dl3 = fgetl(x);
b = strfind(dl3,'E');
dl4 = fgetl(x);
c = strfind(dl4,'E');
%high temperature co_efficients
h1 = str2double(dl2(1:(a(1)+3)));
h2 = str2double(dl2(a(1)+4:a(2)+3));
h3 = str2double(dl2(a(2)+4:a(3)+3));
h4 = str2double(dl2(a(3)+4:a(4)+3));
h5 = str2double(dl2(a(4)+4:a(5)+3));
h6 = str2double(dl3(1:a(1)+3));
h7 = str2double(dl3(a(1)+4:a(2)+3));
%low temperature co_eeficients
l1 = str2double(dl3(a(2)+4:a(3)+3));
l2 = str2double(dl3(a(2)+4:a(3)+3));
l3 = str2double(dl3(a(2)+4:a(3)+3));
l4 = str2double(dl4(1:a(1)+3));
l5 = str2double(dl4(a(1)+4:a(2)+3));
l6 = str2double(dl4(a(2)+4:a(3)+3));
l7 = str2double(dl4(a(3)+4:a(4)+3));
%gamma
R = 8.314;
% Calculating Enthalpy with help of function created
enthalpy = enthalpy_func(h1,h2,h3,h4,h5,h6,l1,l2,l3,l4,l5,l6,R,t,local_mid_temp);
% Calculating Entropy with help of function created
entropy = entropy_func(h1,h2,h3,h4,h5,h6,l1,l2,l3,l4,l5,l6,R,t,local_mid_temp);
% Calculating Specific heat with help of function created
specific_heat = specific_heat_func(h1,h2,h3,h4,h5,l1,l2,l3,l4,l5,t,local_mid_temp);
% Calculating Molecular weight with help of function created
molecularweight = molecular_weight(formula);
disp(['Molecular weight of ', formula, ': ', num2str(molecularweight)]);
mkdir('E:\MATLAB programs\NASA',formula)
%Plotting all the comparisions
%Enthalpy vs Temperature
figure(1)
plot(t,enthalpy,'LineWidth',2,'Color','b')
title(sprintf('ENTHALPY VS TMPERATURE of %s',formula))
ylabel('ENTHALPY')
xlabel('TEMPERATURE')
grid on
%Entropy vs Temperature
figure(2)
plot(t,entropy,'LineWidth',2,'Color','r')
title(sprintf('ENTROPY VS TEMPERATURE of %s',formula))
ylabel('ENTROPY')
xlabel('TEMPERATURE')
grid on
%Specific heat vs Temperature
figure(3)
plot(t,specific_heat,'LineWidth',2,'Color','g')
title(sprintf('SPECIFIC HEAT VS TEMPERATURE of %s',formula))
ylabel('SPECIFIC HEAT')
xlabel('TEMPERATURE')
grid on
%Saving all the plots in thier respective folders
cd("NASA")
cd(formula)
saveas(1, 'enthalpy.png');
saveas(2, 'entropy.png');
saveas(3, 'specific_heat.png');
cd ..
cd ..
end

LOGIC -

=> Stepwise logics involved

* Opening and reding the file.

Opening the file using "fgetl" and reading the file

*Extracting global temperature.

We have different values for temperature extract them using the cmd 'strsplit'

* Getting the molecules used.

Extract all the molecules involved again using 'fgetl' and splitting the line using split cmd.

* Getting data related to temperature.

Getting data related LOCAL TEMPERATURE of each molecule.

* Extraction temperature coefficients for each of the molecule.

extracting all the coefficients for each molecule using by splitting them using the variable E.

* Calculating thermodynamic properties and molecular weight.

Creating the functions 'enthalpy_func', 'entropy_func', 'specific_heat_fumc', 'molecular_weight'

and plots are created for entropy, enthalpy and specific heat with related to temperature

* Saving all the plots.

Plots created SHOULD BE SAVED IN A SPECIFIED DIRETOERY should be saved in a specific directory using 'cd' and 'saveas' commands

* Doing these operations for each of the molecule.

they are all placed in loop so that the process repeats for all the molecules

****in all these steps a string will be converted to number using 'str2double'.

strfind is used to find any string.



FUNCTION CODE:

ENTHALPY:

%Enthalpy
function enthalpy = enthalpy_func(h1,h2,h3,h4,h5,h6,l1,l2,l3,l4,l5,l6,R,t,local_mid_temp)
if t<=local_mid_temp
enthalpy = R*t.*(l1 + l2.* (t./2) + l3.* (t.^2/3) + l4.* (t.^3/4) + l5.* (t.^4/5) + l6./t);
else
enthalpy = R*t.*(h1 + h2.* (t./2) + h3.* (t.^2/3) + h4.* (t.^3/4) + h5.* (t.^4/5) + h6./t);
end
end

ENTROPY:

%Entropy
function entropy = entropy_func(h1,h2,h3,h4,h5,h6,l1,l2,l3,l4,l5,l6,R,t,local_mid_temp)
if t <= local_mid_temp
entropy = R.*((l1.* log(t)) + (l2.*(t/2)) + (l3.*(t.^2/3)) + (l4.*(t.^3/4)) + (l5.*(t.^4/5)) + (l6/t));
else
entropy = R.*((h1.* log(t)) + (h2.*(t/2)) + (h3.*(t.^2/3)) + (h4.*(t.^3/4)) + (h5.*(t.^4/5)) + (h6./t));
end
end


SPECIFIC HEAT:

%Specific Heat
function specific_heat = specific_heat_func(h1,h2,h3,h4,h5,l1,l2,l3,l4,l5,t,local_mid_temp)
if t <= local_mid_temp
specific_heat = l1 + l2.*t + l3.*t.^2 + l4.*t.^3 + l5.*t.^4;
else
specific_heat = h1 + h2.*t + h3.*t.^2 + h4.*t.^3 + h5.*t.^4;
end
end


MOLECULAR WEIGHT:

%Molecular weight
function molecularweight = molecular_weight(formula)
% Define the elements and their atomic weights
%for AR using just A as its atomic weight and and R is 0 so we get its mass
%used in the alphabetical order
atom = {'A','C', 'H', 'N', 'O', 'S','R'};
atomic_weight = [39.948, 12.011, 1.00784, 14.0067, 15.999, 32.065, 0];
molecularweight = 0;
% extracting elements and their counts using regex
tokens = regexp(formula, '([A-Z][a-z]*)(\d*)', 'tokens');
for i = 1:length(tokens)
%getting symbol
element = tokens{i}{1};
%getting count
countStr = tokens{i}{2};
% If the count is empty, it means 1 atom
if isempty(countStr)
count = 1;
else
count = str2double(countStr);
end
% Find the atomic weight of the current element
found = false;
for j = 1:length(atom)
if strcmpi(element, atom{j})
molecularweight = molecularweight + atomic_weight(j) * count;
found = true;
%if element is found breaking the loop
break;
end
end
% If element is not found error message is displayed
if ~found
fprintf('Error: Element %s not found in the list.\n', element);
end
end
end


OUTPUT:


PLOTS:

plots of a random molecule for reference from my output folder:

1.

2.


3.

Screenshot of folders

One random folder




ERRORS

=>

grid should be used after plotting

=>

starting from the first element of line elements are not correctly placed for all the line so it is flipped and then extracted

=>

As 'AR' is a single element with two letters the function is assuming it as two separate elements

Assumed 'A' as symbol and calculated but still shown error as 'R' is not defined, later R is defined as 0.

=>

The function logic is wrong so confronted an error later changed the logic

=>

't' is not defined in the inputs.

References:

Browser, Mathworks official website, youtube, live sessions.

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.