Project 1 - Parsing NASA thermodynamic data
clear all
close all
clc
f1 = fopen('THERMO.dat','r') % This the that we took data for our project using fopen command
fgetl(f1); % fgetl command use for skip or attend next line.
tline = fgetl(f1);
A = strsplit(tline,' '); % this command actually split the distance between the coefficients.
%for Global Temperatures
% This command is actually convert string into number
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});
% i need to skip three lines so instead of putting fgetl command three time
% i directly use 'for' function.
for i = 3:5
fgetl(f1);
end
for j = 1:53 % this is aslo for total 53 species
tline_1 = fgetl(f1);
B = strsplit(tline_1,' ');
species = B{1};
% I use one logic to finf local Temperatures using command findstr and find dot (.)
% and from this i can find the positions of temperatures. from the below
% logic.
pos_dot = findstr(tline_1,'.');
low_temp = tline_1(pos_dot(1)-3:pos_dot(1)+3);
low_temp = str2num(low_temp);
high_temp = tline_1(pos_dot(2)-4:pos_dot(2)+3);
high_temp = str2num(high_temp);
mid_temp = tline_1(pos_dot(3)-4:pos_dot(3)+3);
mid_temp = str2num(mid_temp);
T = linspace(low_temp,high_temp,500); % This is Temperature that contain local
% temperatures 500 values between low_temp to high_temp.
R = 8.314 % Universal Gas Constant
% First Species
%for the 1st Line equation
line_1 = fgetl(f1);
a = findstr(line_1,'E');
a1 = line_1(1:a(1)+3); % This actually string so we need to convert this string into number.
a_1 = str2num(a1); % So this command convert string to number.
a2 = line_1(a(1)+4:a(2)+3);
a_2 = str2num(a2);
a3 = line_1(a(2)+4:a(3)+3);
a_3 = str2num(a3);
a4 = line_1(a(3)+4:a(4)+3);
a_4 = str2num(a4);
a5 = line_1(a(4)+4:a(5)+3);
a_5 = str2num(a5);
A_1 = [a1 a2 a3 a4 a5] ; % This the combine equation .
% For the 2nd line equation
line_2 = fgetl(f1);
b = findstr(line_2,'E');
a6 = line_2(1:b(1)+3);
a_6 = str2num(a6);
a7 = line_2(b(1)+4:b(2)+3);
a_7 = str2num(a7);
a8 = line_2(b(2)+4:b(3)+3);
a_8 = str2num(a8);
a9 = line_2(b(3)+4:b(4)+3);
a_9 = str2num(a9);
a10 = line_2(b(4)+4:b(5)+3);
a_10 = str2num(a10);
A_2 = [a6 a7 a8 a9 a10];
% for the 3rd line equation
line_3 = fgetl(f1);
c = findstr(line_3,'E');
a11 = line_3(1:c(1)+3);
a_11 = str2num(a11);
a12 = line_3(c(1)+4:c(2)+3);
a_12 = str2num(a12);
a13 = line_3(c(2)+4:c(3)+3);
a_13 = str2num(a13);
a14 = line_3(c(3)+4:c(4)+3);
a_14 = str2num(a14);
A_3 = [a11 a12 a13 a14];
% This is the function which give me values of specific heat,enthalpy and
% entropy
for i = 1:length(T)
[c_p,H,S] = therm_properties(a_1,a_2,a_3,a_4,a_5,a_6,a_7,a_8,a_9,a_10,a_11,a_12,a_13,a_14,mid_temp,T);
end
% This figure gives me plot of Temperature vs Specific Heat.
figure(1)
plot(T,c_p,'LineWidth',2,'Color','b');
xlabel('Temperature in K')
ylabel('Specific Heat in KJ/kg.K');
title(sprintf('Variation in Specific Heat with Temperature for %s',species));
saveas(figure(1),'c_p.png');
% This figure gives me plot of Temperature vs Enthalpy.
figure(2)
plot(T,H,'LineWidth',3,'Color','g');
xlabel('Temperature in K')
ylabel('Enthalpy in KJ/kg');
title(sprintf('Variation in enthalpy with Temperature for %s',species));
saveas(figure(2),'H.png');
% This figure gives me plot of Temperature vs entropy.
figure(3)
plot(T,S,'LineWidth',3,'Color','r');
xlabel('Temperature in K')
ylabel('Entropy in KJ/K');
title(sprintf('Variation in Entropy with Temperature for %s',species));
saveas(figure(3),'S.png');
% To Calculate Molecular weight
Molecular_wt = molecular_wt(species);
k = sprintf('Molecular weight of %s is %f',species,Molecular_wt);
disp(k);
end
%Sub_function
function [c_p,H,S] = therm_properties(a_1,a_2,a_3,a_4,a_5,a_6,a_7,a_8,a_9,a_10,a_11,a_12,a_13,a_14,mid_temp,T)
R = 8.314;
if (T >=mid_temp)
c_p = R.*(a_1+ a_2*T+a_3*T.^2+a_4*T.^3+a_5*T.^4);
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.*(a_1*log(T))+(a_2*T)+(a_3*(T.^2)/2)+(a_4*T.^3/3)+(a_5*(T.^4)/4)+(a_7);
else
c_p = R.*(a_8+ a_9*T+a_10*T.^2+a_11*T.^3+a_12*T.^4);
H = R.*T.* (a_8+((a_9*T)/2)+(a_10*T.^2)/3)+((a_11*T.^3)/4)+((a_12*T.^4)/5)+(a_13./T);
S = R.*(a_8*log(T))+a_9*T+a_10*(T.^2)/2+(a_11*(T.^3)/3)+a_12*((T.^4)/4+a_14);
end
end
% Molecular Weight Function
function[molecular_weight] = molecular_wt(species)
elements = {'H','C','O','N','A','S'};
atomic_weight = [1,12,16,14,39,32];
molecular_weight = 0;
for i = 1:length(species)
for j = 1:length(elements)
if strcmp(species(i),elements(j))
molecular_weight = molecular_weight + atomic_weight(j)
a =j;
end
end
n = str2num(species(1));
if (n>1)
molecular_weight = molecular_weight + (atomic_weight(a)*(n-1))
end
end
end
% Explanation -
During completing the file parsing challenge i familiar with too many command functions.i use so many function actually during file parsing firstly we need understand the data which we need to convert into matlab code. during this challenge i use three formules which i used in my academics.