NASA Polynomials - File Parsing
The Project is on Parsing data from a NASA dat file, in order to extract all the polynomials and required data to calculate Specific Heat, Enthalpy, Entropy & to present it graphically using MATLAB
Main Program:
% File Parsing - NASA Polynomials - Project 1
clear all
close all
clc
f1=fopen('therm.dat','r');
Title=fgetl(f1);
% Extracting Global_low, Global_mid and Global_high temperatures
l1=fgetl(f1);
l1_split=strsplit(l1,' ');
Global_low_temp =str2double(l1_split{2});
Global_mid_temp =str2double(l1_split{3});
Global_high_temp =str2double(l1_split{4});
% Skipping the comment lines
for k=1:3
skip=fgetl(f1);
end
t=linspace(200,3500,50);
f='NASA Results';
mkdir(f)
% Extracting Data
for i=1:53
[name_species,c1,c2,c3]= coef_extract(f1);
ht=[c1;c2(1:2)];
lt=[c2(3:end);c3];
% Calculating Cp, H, S & Molecular Weight
Cp=cp_func(ht,lt,t,Global_mid_temp);
H=h_func(ht,lt,t,Global_mid_temp);
S=s_func(ht,lt,t,Global_mid_temp);
molecular_weight= mw_func(name_species);
plot_func(t,Cp,H,S,name_species,molecular_weight,f); % Plotting the Graphs
end
fclose(f1)
Extraction of Co-Efficients Function:
% Function to extract coefficients of each species
function [name_species,c1,c2,c3]= coef_extract(f1)
tline=fgetl(f1);
name_species=tline(1:7);
line1=fgetl(f1);
c1=sscanf(line1,'%f');
c1=c1(1:end-1);
line2=fgetl(f1);
c2=sscanf(line2,'%f');
c2=c2(1:end-1);
line3=fgetl(f1);
c3=sscanf(line3,'%f');
c3=c3(1:end-1);
end
Specific Heat Function:
% Function to calculate specific heat
function [Cp]=cp_func(ht,lt,t,Global_mid_temp)
for i=1:length(t)
temp1=ht(1);
temp2=ht(2)*t(i);
temp3=ht(3)*t(i)^2;
temp4=ht(4)*t(i)^3;
temp5=ht(5)*t(i)^4;
temp6=lt(1);
temp7=lt(2)*t(i);
temp8=lt(3)*t(i)^2;
temp9=lt(4)*t(i)^3;
temp10=lt(5)*t(i)^4;
if t(i)>=Global_mid_temp
Cp(i)= temp1 +temp2 +temp3 +temp4 +temp5;
else
Cp(i)= temp6 +temp7 +temp8 +temp9 +temp10;
end
end
end
Enthalpy Function:
% Function to calculate Enthalpy
function [H]=h_func(ht,lt,t,Global_mid_temp)
for i=1:length(t)
temp1=ht(1);
temp2=ht(2).*t(i)/2;
temp3=ht(3).*(t(i)^2)/3;
temp4=ht(4).*(t(i)^3)/4;
temp5=ht(5).*(t(i)^4)/5;
temp6=ht(6)./t(i);
temp7=lt(1);
temp8=lt(2).*t(i)/2;
temp9=lt(3).*(t(i)^2)/3;
temp10=lt(4).*(t(i)^3)/4;
temp11=lt(5).*(t(i)^4)/5;
temp12=lt(6)./t(i);
if t(i)>=Global_mid_temp
H(i)=temp1+temp2+temp3+temp4+temp5+temp6;
else
H(i)=temp7+temp8+temp9+temp10+temp11+temp12;
end
end
end
Entropy Function:
% Function to calculate Entropy
function [S]=s_func(ht,lt,t,Global_mid_temp)
for i=1:length(t)
temp1=ht(1)*log(t(i));
temp2=ht(2).*t(i);
temp3=ht(3).*(t(i)^2)/2;
temp4=ht(4).*(t(i)^3)/3;
temp5=ht(5).*(t(i)^4)/4;
temp6=ht(7);
temp7=lt(1)*log(t(i));
temp8=lt(2).*t(i);
temp9=lt(3).*(t(i)^2)/2;
temp10=lt(4).*(t(i)^3)/3;
temp11=lt(5).*(t(i)^4)/4;
temp12=lt(7);
if t(i)>=Global_mid_temp
S(i)=temp1+temp2+temp3+temp4+temp5+temp6;
else
S(i)=temp7+temp8+temp9+temp10+temp11+temp12;
end
end
end
Molecular Weight Function:
% Function to calculate molecular weight
function [molec_weight] = mw_func(name_species)
a = ['H','C','N','O','A'];
at_w = [1 ,12, 14, 16, 40];
molec_weight=0;
for i=1:length(name_species)
for j=1:length(a)
if strcmp(name_species(i),a(j))
molec_weight=molec_weight+at_w(j);
J=j ;
end
end
n=str2num(name_species(i));
if n>1
molec_weight=molec_weight+(at_w(J)*n)-at_w(J);
end
end
end
Plot Function:
%function to plot the Solutions
function plot_func(t,Cp,H,S,name_species,molecular_weight,f)
p=pwd;
cd(f)
fd=sprintf('%s',name_species);
mkdir(fd);
% Generating plots for Cp vs. Temp
a=figure(1);
set(a,'visible','off')
plot(t,Cp,'-ko','markeredgecolor','r','linewidth',1,'markersize',5,'markerfacecolor','c')
title(sprintf('Specific heat vs. Temprature\n Species - %s Molecular Weight - %d',name_species,molecular_weight),'Fontsize',13)
xlabel('Temperature','FontSize',10)
ylabel('Cp/R','FontSize',10)
% Generating plots for H vs. Temp
a = figure(2);
set(a, 'visible', 'off');
plot(t,H,'-ks', 'markeredgecolor','g','linewidth',1 ,'markersize',5,'markerfacecolor','y')
title(sprintf('Enthalpy vs. Temprature\nSpecies - %s Molecular weight - %d',name_species,molecular_weight),'FontSize',13)
xlabel('Temperature','FontSize',10)
ylabel('H/RT','FontSize',10)
% Generating plots for S vs. Temp
a = figure(3);
set(a, 'visible', 'off')
plot(t,S,'-kd', 'markeredgecolor','b','linewidth',1,'markersize',5,'markerfacecolor','m')
title(sprintf('Entropy vs. Temprature\nSpecies - %sMolecular weight - %d',name_species,molecular_weight),'FontSize',13)
xlabel('Temperature','FontSize',10)
ylabel('S/R','FontSize',10)
cd(fd)
saveas(1,'Cp.png')
saveas(2,'H.png')
saveas(3,'S.png')
cd(p)
end
Some Example Solutions:
AR - Cp Vs. T
AR - H Vs. T
AR - S Vs. T
Link to the MATLAB Generated Result Files : Click Here