function[name,x,y,z]=coeff(X)
%first line after the header information
gases=fgets(X);
%naming the gaeses
name=gases(1:7);
line_2 = fgetl(X);
line_3 = fgetl(X);
line_4 = fgetl(X);
%two similar appraoches can be used to extract the coefficients
x = sscanf(line_2, \\\'%f\\\');
x = x(1:end-1)\\\';
y = sscanf(line_3,\\\'%f\\\');
y = y(1:end-1)\\\';
z = sscanf(line_4,\\\'%f\\\');
z = z(1:end-1)\\\';
%a=findstr(line_2,\\\'E\\\');
%hc1=line_2(1:a(1)+3);
%hc2=line_2(a(1)+4:a(2)+3);
%hc3=line_2(a(2)+4:a(3)+3);
%hc4=line_2(a(3)+4:a(4)+3);
%hc5=line_2(a(4)+4:a(5)+3);
%b=findstr(line_3,\\\'E\\\');
%hc6=line_3(1:b(1)+3);
%hc7=line_3(b(1)+4:b(2)+3);
%lc1=line_3(b(2)+4:b(3)+3);
%lc2=line_3(b(3)+4:b(4)+3);
%lc3=line_3(b(4)+4:b(5)+3);
%c=findstr(line_4,\\\'E\\\');
%lc4=line_4(1:c(1)+3);
%lc5=line_4(c(1)+4:c(2)+3);
%lc6=line_4(c(2)+4:c(3)+3);
%lc7=line_4(c(3)+4:c(4)+3);
%x=[hc1 hc2 hc3 hc4 hc5];
%y=[hc6 hc7 lc1 lc2 lc3];
%z=[lc4 lc5 lc6 lc7];
end
2. Write functions that calculate the enthalpy, entropy and specific heats using the NASA polynomials.
Function to calculate the specific heat (J/Kg K) for all gases at different temperatures(specific_heat.m)
function[cp]=specific_heat(temp,coeff_high,coeff_low)
%function to calculate the specific heat (J/Kg K) for all gases at different temperatures.
mean_temp=1000;
R=8.314;
for i=1:length(temp)
cph1=coeff_high(1);
cph2=coeff_high(2)*(temp(i));
cph3=coeff_high(3)*(temp(i)^2);
cph4=coeff_high(4)*(temp(i)^3);
cph5=coeff_high(5)*(temp(i)^4);
cpl1=coeff_low(1);
cpl2=coeff_low(2)*(temp(i));
cpl3=coeff_low(3)*(temp(i)^2);
cpl4=coeff_low(4)*(temp(i)^3);
cpl5=coeff_low(5)*(temp(i)^4);
if(temp>mean_temp)
cp(i)=R*(cph1+cph2+cph3+cph4+cph5);
else
cp(i)=R*(cpl1+cpl2+cpl3+cpl4+cpl5);
end
end
end
function to calculate the enthalpy(J) for all gases at different temperatures(enthalpy.m)
function[H]=enthalpy(temp,coeff_high,coeff_low)
%function to calculate the enthalpy(J) for all gases at different temperatures.
mean_temp=1000;
R=8.314;
for i=1:length(temp)
cph1=coeff_high(1);
cph2=coeff_high(2)*(temp(i)/2);
cph3=coeff_high(3)*(temp(i)^2/3);
cph4=coeff_high(4)*(temp(i)^3/4);
cph5=coeff_high(5)*(temp(i)^4/5);
cph6=coeff_high(6)/temp(i);
cpl1=coeff_low(1);
cpl2=coeff_low(2)*(temp(i)/2);
cpl3=coeff_low(3)*(temp(i)^2/3);
cpl4=coeff_low(4)*(temp(i)^3/4);
cpl5=coeff_low(5)*(temp(i)^4/5);
cpl6=coeff_low(6)/temp(i);
if(temp>mean_temp)
H(i)=R*temp(i)*(cph1+cph2+cph3+cph4+cph5+cph6);
else
H(i)=R*temp(i)*(cpl1+cpl2+cpl3+cpl4+cpl5+cpl6);
end
end
end
function to calculate the entropy(J/K) for all gases at different temperatures(entropy.m)
function[S]=entropy(temp,coeff_high,coeff_low)
%function to calculate the entropy(J) for all gases at different temperatures.
mean_temp=1000;
R=8.314;
for i=1:length(temp)
cph1=coeff_high(1)*log(temp(i));
cph2=coeff_high(2)*(temp(i));
cph3=coeff_high(3)*(temp(i)^2/2);
cph4=coeff_high(4)*(temp(i)^3/3);
cph5=coeff_high(5)*(temp(i)^4/4);
cph6=coeff_high(7);
cpl1=coeff_low(1)*log(temp(i));
cpl2=coeff_low(2)*(temp(i));
cpl3=coeff_low(3)*(temp(i)^2/2);
cpl4=coeff_low(4)*(temp(i)^3/3);
cpl5=coeff_low(5)*(temp(i)^4/4);
cpl6=coeff_low(7);
if(temp>mean_temp)
S(i)=R*(cph1+cph2+cph3+cph4+cph5+cph6);
else
S(i)=R*(cpl1+cpl2+cpl3+cpl4+cpl5+cpl6);
end
end
end
3. Write a function that calculates the molecular weight of each species
function to calculate the molecular weight(Kg) of different gases (mol_weight.m)
function [W] = mol_weight(name)
%function to calculate the molecular weight(Kg) for all gases
%various elements that the gases constitute
elements={\\\'H\\\' \\\'C\\\' \\\'N\\\' \\\'O\\\' \\\'AR\\\'};
%atomic weights of the individual elements
atomic_weight=[1 12 14 16 40];
W = 0;
for i = 1:length(name)
for j = 1:length(elements)
if strcmp(name(i),elements(j))
W = W + atomic_weight(j);
Y = j;
end
end
%incase same element is present more than one. Eg: 2 or 3 hydrogen atoms in a gas
n = str2num(name(i));
if n > 1
W = W + atomic_weight(Y)*(n-1);
end
end
end
4 and 5. Write a function that plots the Cp, Enthalpy and Entropy of an input species.You should save the plots with appropriate names. The names should be generated automatically
plotting the paramters and saving them in individual folders.
function plots(temp,cp,H,S,W,name)
%function to plot the various parameters with respect to temperature
directory=sprintf(\\\'%s\\\',name);
mkdir(directory);
%plot of specific heat vs temperature
a=figure(1)
set(a, \\\'visible\\\', \\\'off\\\')
plot(temp,cp,\\\'linewidth\\\',1,\\\'marker\\\',\\\'o\\\',\\\'markersize\\\',2,\\\'markerfacecolor\\\',\\\'r\\\');
title(\\\'Specific heat vs Temperature\\\');
xlabel(\\\'Temperature(K)\\\');
ylabel(\\\'Specific heat(J/Kg K)\\\');
%plot of Enthalpy vs temperature
b=figure(2)
set(b, \\\'visible\\\', \\\'off\\\')
plot(temp,H,\\\'linewidth\\\',1,\\\'marker\\\',\\\'o\\\',\\\'markersize\\\',2,\\\'markerfacecolor\\\',\\\'g\\\');
title(\\\'Enthalpy vs Temperature\\\');
xlabel(\\\'Temperature(K)\\\');
ylabel(\\\'Enthalpy(J)\\\');
%plot of Entropy vs temperature
c=figure(3)
set(c, \\\'visible\\\', \\\'off\\\')
plot(temp,S,\\\'linewidth\\\',1,\\\'marker\\\',\\\'o\\\',\\\'markersize\\\',2,\\\'markerfacecolor\\\',\\\'b\\\');
title(\\\'Entropy vs Temperature\\\');
xlabel(\\\'Temperature(K)\\\');
ylabel(\\\'Entropy(J/ K)\\\');
pf = pwd;
cd(directory);
%saving the images under the mentioned names in jpeg format
saveas(1, \\\'cp.jpeg\\\');
saveas(2, \\\'H.jpeg\\\');
saveas(3, \\\'S.jpeg\\\');
cd(pf);
end
6.The main program that you will submit will just take the \\\"therm.dat\\\" file as an input and generate cp, h and s plots for all the species that are included in the therm.dat file.
The main program to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
close all
clear all
clc
%reading the file containing the coefficients
X=fopen(\\\'therm.dat\\\',\\\'r\\\');
Heading=fgetl(X);
%obtaining the temperature data
temp_data=fgetl(X);
A=strsplit(temp_data,\\\' \\\');
global_low_temp=str2num(A{2});
global_mean_temp=str2num(A{3});
global_high_temp=str2num(A{4});
% Skipping the header information
for i = 1:3
skip = fgetl(X);
end
temp=linspace(global_low_temp,global_high_temp,50);
%num_cases=53;
for j=1:53
[name,x,y,z]=coeff(X);
%splitting the obtained coefficients into high and low temperature
%coefficients
coeff_high=[x y(1:2)];
coeff_low=[y(3:end) z];
%specific heat(J/kg K) data
cp= specific_heat(temp,coeff_high,coeff_low);
%enthalpy(J) data
H= enthalpy(temp,coeff_high,coeff_low);
%Entropy(J/K)
S= entropy(temp,coeff_high,coeff_low);
%molecular weight (g)
W=mol_weight(name);
%plotting data and saving them into seperate folders
plots(temp,cp,H,S,W,name);
end
%closing the file
fclose(X);