Project 1 - Parsing NASA thermodynamic data
Parsing NASA Thermodynamic Data :
Aim - The aim of the project is to evaluate the thermodynamic data such as specific heat, enthalpy, entropy and to create the plots for all the species and to display the molecular weight by using matlab.
Objectives :
- to write a function that extract the 14 coefficients and calculates the specific heat, enthalpy, entropy for all the species in the data file
- calculating the molecular weight of each species and display in command window
- plotting cp, enthalpy and entropy for local tempreture range specific for each species
- save the plot as images with appropiriate names and dumb them in a seperate folders for each species with suitable name
Definations :
Specific Heat - specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree.
Enthalpy - enthalpy is simply define as the sum of the internal energy and the product of the pressure and volume of a thermodynamic system.
Entropy - entropy, the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
Equations :
- cp/R=a1+a2T+a3T^2+a4T^3+a5T^4
- H/RT=a1+a2T/2+a3t^2/3+a4T^3/4+a5T^4/5+a6/T
- S/R=a1logT+a2T+a3T^2/2+a4T^3/3+a5T^4/4+a7
where,
cp=Specific heat
H=Enthalpy
S=Entropy
R=universal gas constant
a=coefficients
T=tempreture
Function for Specific Heat,
function [cp]=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,local_mid_temp)
%calculating specific heat for higher and lower temp
if T>local_mid_temp
cp=(a1+(a2*T)+(a3*T.^2.)+(a4*T.^3.)+(a5*T.^4.))*R;
else
cp=(b1+(b2*T)+(b3*T.^2.)+(b4*T.^3.)+(b5*T.^4.))*R;
end
end
Function for Enthalpy,
function [H]=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,local_mid_temp)
%calculating enthalpy for higher and lower temp
if T>local_mid_temp
H=R*T*(a1+(a2*T/2)+(a3*T.^2./3)+(a4*T.^3./4)+(a5*T.^4./5)+a6./T);
else
H=R*T*(b1+(b2*T/2)+(b3*T.^2./3)+(b4*T.^3./4)+(b5*T.^4./5)+b6./T);
end
end
Function for Entropy,
function [S]=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,local_mid_temp)
%calculating entropy for higher and lower temp
if T>local_mid_temp
S=R*(a1*log(T)+(a2*T)+(a3*T.^2./2)+(a4*T.^3./3)+(a5*T.^4./4)+a7);
else
S=R*(b1*log(T)+(b2*T)+(b3*T.^2./2)+(b4*T.^3./3)+(b5*T.^4./4)+b7);
end
end
Function for Molecular Weight,
function molecular_wt_cal = molecular_weight(species_name)
element = ['H','O','C','N','A'];
atomic_wt = [1.008,15.999,12.011,14.007,39.948];
mol_wt = 0;
for i=1:length(species_name)
for j=1:length(element)
if strcmpi(species_name(i),element(j))
mol_wt=mol_wt+atomic_wt(j);
z=j
end
end
t=str2num(species_name(i))
if(t>1)
mol_wt=mol_wt+(atomic_wt(z)*(t-1));
end
molecular_wt_cal=mol_wt
end
Main Program is as mention below,
%main code
clear all
close all
clc
%opening a file
f1=fopen('THERMO.dat','r');
%get heading file
heading = fgetl(f1);
%global temp values
global_temp = fgetl(f1);
%splitting of string
A = strsplit(global_temp);
%convert the string into no.
global_low_temp= str2double(A{2});
global_mid_temp= str2double(A{3});
global_high_temp= str2double(A{4})
%universal gas constant
R = 8.134;
%skip next 3 lines
for i=1:3
skippinglines = fgetl(f1)
end
%creating the loop to read the species
for i=1:53
line1=fgetl(f1)
%splitting into cell array
temp=strsplit(line1,' ');
%to read the name of the species
species_name=line1(1:6);
%to convert string into no.
local_temp1=line1(49:55);
local_low_temp=str2double(local_temp1);
local_temp2=line1(58:65);
local_high_temp=str2double(local_temp2);
local_temp3=line1(68:75)
local_mid_temp=str2double(local_temp3);
%defining tempreture range
T = linspace(local_low_temp,local_high_temp,700);
end
%values A stands for high temp coefficients
%values B stands for low temp coefficients
% extracting 1st 7 coefficients
line2=fgetl(f1);
line3=fgetl(f1);
line4=fgetl(f1);
a=strfind(line2,'E');
b=strfind(line3,'E');
c=strfind(line4,'E');
A1=str2double(line2(1:a(1)+3));
A2=str2double(line2(a(1)+4:a(2)+3));
A3=str2double(line2(a(2)+4:a(3)+3));
A4=str2double(line2(a(3)+4:a(4)+3));
A5=str2double(line2(a(4)+4:a(5)+3));
A6=str2double(line3(1:b(1)+3));
A7=str2double(line3(b(1)+4:b(2)+3));
% extracting 2nd 7 coefficients
B1=str2double(line3(b(2)+4:b(3)+3));
B2=str2double(line3(b(3)+4:b(4)+3));
B3=str2double(line3(b(4)+4:b(5)+3));
B4=str2double(line4(1:c(1)+3));
B5=str2double(line4(c(1)+4:c(2)+3));
B6=str2double(line4(c(2)+4:c(3)+3));
B7 =str2double(line4(c(3)+4:c(4)+3));
%calculating specific heat
cp=specific_heat(A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,T,R,local_mid_temp);
%calculating the enthalpy
H=enthalpy(A1,A2,A3,A4,A5,A6,B1,B2,B3,B4,B5,B6,T,R,local_mid_temp);
%calculating the entropy
S=entropy(A1,A2,A3,A4,A5,A7,B1,B2,B3,B4,B5,B7,T,R,local_mid_temp)
%molecular weight Calculations
molecular_wt_cal = molecular_weight(species_name);
k = sprintf('%s mol_wt=%f',species_name,molecular_wt_cal)
disp(k)
% create the folders
data = strcat('properties of ....',species_name);
mkdir(data);
file = pwd
%plotting
figure(1)
plot(T,cp,'LineWidth',5)
data1 = strcat(species_name,'tempreture vs specific heat')
xlabel('Tempreture')
ylabel('Specific Heat')
title(species_name)
f=fullfile(data)
cd(f)
saveas(gcf,[pwd '/specific heat vs Tempreture.jpg'])
cd(file)
figure(2)
plot(T,H,'LineWidth',5,'Color','r')
data1 = strcat(species_name,'tempreture vs Enthalpy')
xlabel('Tempreture')
ylabel('Enthalpy')
title(species_name)
f=fullfile(data)
cd(f)
saveas(gcf,[pwd '/enthalpy vs Tempreture.jpg'])
cd(file)
figure(3)
plot(T,S,'LineWidth',5,'Color','g')
data1 = strcat(species_name,'tempreture vs entropy')
xlabel('Tempreture')
ylabel('Entropy')
title(species_name)
f=fullfile(data)
cd(f)
saveas(gcf,[pwd '/entropy vs Tempreture.jpg'])
cd(file)
fclose(f1)
Error which i face when programming,

to correct this error i will check all codes properly and solve the mistake
Saving the jpg images of plots,

Explanation of codes are as follows :
- first 3 codes are use for clear all the data in the editor, command and workspace window
- fopen code open the given file
- fgetl code is for reading the line in the file
- strsplit code uses for Specify multiple delimiters in a cell array or a string array
- str2double and str2num code converts the string into numbers
- linspace code is generate equal and linearly spaced vectors
- strfind uses for finding the element in the string
- strcmpi is used when comparing two strings for equality
- mkdir create the specified folder
- pwd displays the current folder
- plot code uses for plotting the graph
- xlabel and ylabel code is for naming the axis
- title generates the name for plot
- saveas command is use for saving the elements or file