Challenge

Project 1 - Parsing NASA thermodynamic data

FUNCTION CODE-1

%specific heat ,Enthalpy,Endropy calculation

function[Cp,H,S]=inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)
for i=1:length(T)
    if T(i)>Tm1
    Cp=(c1+c2.*T+c3.*T.^2+c4.*T.^3+c5.*T.^4).*R;
    H=(c1+(c2.*T)./2+(c3.*T.^2)/3+(c4.*T.^3)./4+(c5.*T.^4)./5+c6./T).*T.*R;
    S=(c1.*log(T)+c2.*T+(c3.*T.^2)./2+(c4.*T.^3)./3+(c5.*T.^4)./4+c7).*R;
    else
        Cp=(c8+c9.*T+c10.*T.^2+c11.*T.^3+c12.*T.^4).*R;
    H=(c8+(c9.*T)./2+(c10.*T.^2)/3+(c11.*T.^3)./4+(c12.*T.^4)./5+c13./T).*T.*R;
    S=(c8.*log(T)+c9.*T+(c10.*T.^2)./2+(c11.*T.^3)./3+(c12.*T.^4)./4+c14).*R;
    end
end
end

FUNCTION CODE-2

%specific heat ,Enthalpy,Endropy calculation

function[Cp,H,S]=inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)
for i=1:length(T)
    if T(i)>Tm1
    Cp=(c1+c2.*T+c3.*T.^2+c4.*T.^3+c5.*T.^4).*R;
    H=(c1+(c2.*T)./2+(c3.*T.^2)/3+(c4.*T.^3)./4+(c5.*T.^4)./5+c6./T).*T.*R;
    S=(c1.*log(T)+c2.*T+(c3.*T.^2)./2+(c4.*T.^3)./3+(c5.*T.^4)./4+c7).*R;
    else
        Cp=(c8+c9.*T+c10.*T.^2+c11.*T.^3+c12.*T.^4).*R;
    H=(c8+(c9.*T)./2+(c10.*T.^2)/3+(c11.*T.^3)./4+(c12.*T.^4)./5+c13./T).*T.*R;
    S=(c8.*log(T)+c9.*T+(c10.*T.^2)./2+(c11.*T.^3)./3+(c12.*T.^4)./4+c14).*R;
    end
end
end

MAIN CODE

%Parsing NASA thermodynamics Data
clear all 
close all
clc
%getting the data from the given data
f1=fopen('THERMO.dat','r');
sk=fgetl(f1);
tline=fgetl(f1);
B=strsplit(tline,' ');
global_low_tp=str2num(B{2});
global_mid_tp=str2num(B{3});
global_high_tp=str2num(B{4});

%skipping next three lines
for l=1:3
    skippline=fgetl(f1);
end
%create for loop to calculate all species data
for l=1:53
 
species=fgetl(f1);
%finding the species name
p=findstr(species,' ');
sp_name=species(1:p(1)-1);


t=findstr(species,'.');
%local temperature of each species 
species(52-3:52+3);
Tl=species(52-3:t(1)+3);
Tl1=str2double(Tl);

species(62-4:62+3);
Th=species(62-4:t(2)+3);
Th1=str2double(Th);

species(72-4:72+3);
Tm=species(72-4:t(3)+3);
Tm1=str2double(Tm);

sp_name=species(1:6);
%coefficient of each species
coeff_line1=fgetl(f1);
A11=strfind(coeff_line1,'E');
coeff_line1(1:12+3);

a1=coeff_line1(1:12+3);
c1=str2double(a1);
a2=coeff_line1(27-11:17+3);
c2=str2double(a2);
a3=coeff_line1(42-11:42+3);
c3=str2double(a3);
a4=coeff_line1(57-11:57+3);
c4=str2double(a4);
a5=coeff_line1(72-11:72+3);
c5=str2double(a5);

coeff_line2=fgetl(f1);
A12=strfind(coeff_line2,'E');
coeff_line2(1:12+3);

a6=coeff_line2(1:12+3);
c6=str2double(a6);
a7=coeff_line2(27-11:17+3);
c7=str2double(a7);
a8=coeff_line2(42-11:42+3);
c8=str2double(a8);
a9=coeff_line2(57-11:57+3);
c9=str2double(a9);
a10=coeff_line2(72-11:72+3);
c10=str2double(a10);


coeff_line3=fgetl(f1);
A13=strfind(coeff_line3,'E');
coeff_line2(1:12+3);

a11=coeff_line3(1:12+3);
c11=str2double(a11);
a12=coeff_line3(27-11:17+3);
c12=str2double(a12);
a13=coeff_line3(42-11:42+3);
c13=str2double(a13);
a14=coeff_line3(57-11:57+3);
c14=str2double(a14);

%creating the folder to save the plot
cd('C:\Users\91956\Documents\MATLAB')%cd=change directory
mkdir(species);%mkdir=make directory/folder
cd(species);

%please bevare
%calculating the specific heat enthalpy entropy
T=linspace(Tl1,Th1,100);
R= 8.314;

[Cp,H,S]=inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);
%plotting
figure(1)
plot(T,Cp,'linewidth',2,'color','r')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('specific heat')

figure(2)
plot(T,H,'linewidth',2,'color','b')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('Enthalpy')

figure(3)
plot(T,S,'linewidth',2,'color','y')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('Entropy')


%saving plots
saveas(1,'Specific Heat.jpg')
saveas(2,'Enthalpy.jpg')
saveas(3,'Entropy.jpg')

cd('C:\Users\91956\Documents\MATLAB')
%finding the molecular weight
is=molecular_weight(sp_name);
z=sprintf('molecular_weights of %s is',sp_name);
x=sprintf('%f',is);
disp(z) 
disp(x)


end

fclose(f1)


RESULTS
Folders of each species

folder contains

PLOT OF SPECIFIC HEAT v/s TEMPERATURE

PLOT OF ENTHALPY v/s TEMPERATURE

PLOT OF ENTROPY v/s TEMPERATURE

Molecular weight of species

ERRORS

some index error arise while calling the function and  and clarified it by editing the funtions 

error arise in calling a function

REPORT

FILE PARSING:- It is the methord of spliting the file into pieces of data that can be easily stored and manipulate

SPECIFIC HEAT:-It is the amount of heat required to raise the temperature of of body per unit mass.

ENTHALPY:-When a substance change at constant pressure,enthalpy tells how much heat and work was added or removed from the substance.

ENTROPY:-It is a measure of the amount of energy which is unavailable to do work.

clear all
close all
clc 
The program begins with 'clear all ',' close all' and 'clc' that to free up command window and the workspace
 
 
f1=fopen('THERMO.dat','r');
sk=fgetl(f1);
tline=fgetl(f1); 
  • We will then read the data from external NASA file using 'fopen',which is used to open the file 
and' fgetl' command which is used to read the contant in line by line . After we use the fopen command we wiil store it in a variable called f1.Thus the 'fgetl(f1)'acts like a line cursor,which whill retives the next line while runing the command of second and so on.
B=strsplit(tline,' ');
global_low_tp=str2num(B{2});
global_mid_tp=str2num(B{3});
global_high_tp=str2num(B{4}); 
  • Then we will get the global temperature by using 'strsplit' command 
%skipping next three lines
for l=1:3
skippline=fgetl(f1);
end
  • Now, we will skipping the first five lines to enter the pattern of values of the respective gas species,we make use of 'for' loop command to get all temperature and coefficients that present in the file.
%create for loop to calculate all species data
for l=1:53
%taking local temperature form the first line
species=fgetl(f1);
%finding the species name
p=findstr(species,' ');
sp_name=species(1:p(1)-1);
  • The name of gas species will be found using 'findstr' command and for find out species we are going to locate the string ' ' which is present after each name.Then we locate the exact location of the species ,where the name of name of gas stored in 'sp_name' 
t=findstr(species,'.');
The name of the gas species is find out using the command 'findstr'
%local temperature of each species
species(52-3:52+3);
Tl=species(52-3:t(1)+3);
Tl1=str2double(Tl);
species(62-4:62+3);
Th=species(62-4:t(2)+3);
Th1=str2double(Th);
species(72-4:72+3);
Tm=species(72-4:t(3)+3);
Tm1=str2double(Tm);
  • The local temperature for the respective gas species is found similarly using the 'findstr' command through the string'.' which will find out the value of low temperature ,mid temperature,high temperature as Tl,Tm,Th respectivily.But the value we obtained is in the form of string so we need to change to number using the command 'str2num'.
sp_name=species(1:6);
%coefficient of each species
coeff_line1=fgetl(f1);
A11=strfind(coeff_line1,'E');
coeff_line1(1:12+3);
a1=coeff_line1(1:12+3);
c1=str2double(a1);
a2=coeff_line1(27-11:17+3);
c2=str2double(a2);
a3=coeff_line1(42-11:42+3);
c3=str2double(a3);
a4=coeff_line1(57-11:57+3);
c4=str2double(a4);
a5=coeff_line1(72-11:72+3);
c5=str2double(a5);
coeff_line2=fgetl(f1);
A12=strfind(coeff_line2,'E');
coeff_line2(1:12+3);
a6=coeff_line2(1:12+3);
c6=str2double(a6);
a7=coeff_line2(27-11:17+3);
c7=str2double(a7);
a8=coeff_line2(42-11:42+3);
c8=str2double(a8);
a9=coeff_line2(57-11:57+3);
c9=str2double(a9);
a10=coeff_line2(72-11:72+3);
c10=str2double(a10);
coeff_line3=fgetl(f1);
A13=strfind(coeff_line3,'E');
coeff_line2(1:12+3);
a11=coeff_line3(1:12+3);
c11=str2double(a11);
a12=coeff_line3(27-11:17+3);
c12=str2double(a12);
a13=coeff_line3(42-11:42+3);
c13=str2double(a13);
a14=coeff_line3(57-11:57+3);
c14=str2double(a14);
  • For extrating the coefficient we are going to locate the string 'E' which is present in each coefficient value, then as we do the same in temperature finding using 'findstr'and then change the string to number by 'str2num' or 'str2double'.There are 14 coefficient and we call that as variable from a1-a14 out of that a1-17 is the high temperature and a7-a14 is the low temperature coefficient
%creating the folder to save the plot
cd('C:\Users\91956\Documents\MATLAB')%cd=change directory
mkdir(species);%mkdir=make directory/folder
cd(species);
  • In order to create a new folder with the 'sp_name' we are going to use the 'mkdir' command so that a new folder is created in the main folder.Now we have to change the current directory from the main folder to sp_name folder so that 'cd' command is used  
%calculating the specific heat enthalpy entropy
T=linspace(Tl1,Th1,100);
R= 8.314;
[Cp,H,S]=inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);
 
 
  • Temperature range values from low temperature  t high temperature is assigned using the 'linspace' command.
  • 'inputs' is a funtion(function code-1) .In that the 'for' loop is used to calculate the value of specific heat ,enthropy ,endhalpy and the 'else if' command is also used to find out the required values if the temperature below the mid temperature or above the mid teperature.
%plotting
figure(1)
plot(T,Cp,'linewidth',2,'color','r')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('specific heat')
figure(2)
plot(T,H,'linewidth',2,'color','b')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('Enthalpy')
figure(3)
plot(T,S,'linewidth',2,'color','y')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('Entropy')
  • The plot for specific heat vs temperature,enthalpy vs temperature ,entropy vs temperature is ploted and labelling of axis and the namming of the title is also done along with the value of molecular weight.
%saving plots
saveas(1,'Specific Heat.jpg')
saveas(2,'Enthalpy.jpg')
saveas(3,'Entropy.jpg')
cd('C:\Users\91956\Documents\MATLAB')
  • To save the plot in the sp_name folder ,'saveas'command is used tp save the plot in the current folder.After which to change the current directory to thre main folder using 'cd'command.
%finding the molecular weight
is=molecular_weight(sp_name)
  • In oder to calculate molecular weight we have a funtion 'molecular_weight'(functioncode-2) which we declared the species of element in the form of cell array.their atomic mass is declared in the form of array.Then the initial molecular weight is taken as zero.
  • 'for' loop command is used to detect if there are more than one species in the variable sp_name and the respective values of the gas species values are calculated and found out 
  • After the nested loop the sp_name which is in the form of string converted as number with 'str2num' or 'str2double' command. Then the sp_name which is now as number greater than 1, molecular weight is again calculated by having the atomic mass for the current species *n-1
sprintf('molecular_weights of %s ',sp_name)
  • The value for the molecular weight is calculated for the specific gases and is  diplayed in the command window
end
fclose(f1)
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.

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.