Project 1 - Parsing NASA thermodynamic data
Aim:
To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species using the given data file . to display the molecular weight of each species in command window and separately plot the graph as png file in folders
Procedure:
As per the given thermo data the program is provided after understanging the arrangement of values. Saved the themo data to the workfile saved folder and open the filer using the fopen. fgetl(f1) read the first line when we use the command for next time it reads the next line data in this way this command reads the file
For loop is used to skip the unwanted lines and next for loop is used for as per the data they are in continuously in the same manner so to skip the long program here used. In this data they are 53 datas so they need to run for loop for multi times
In the 6th line the temperature is available so first we assuming as per the temperature as in the program and the values are splited using the strsplit commend as per the format is assigned it automatically split them and str2num is used to assign the position and the value is loaded
In this way with use of common variable E the line is splited by findstr and str2double is used for load the value by assign the position from to in this way all the values are loaded
Then the temperature is splited 1000 with in the low and high temperature in array
As per formula need to find the cp, h and s value for that as per the given formula the function is created and recalled to the main program to plot them
function [cp, H, S] = coeff (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,global_mid_temp)
R=8.314;
for j = 1:length(T);
if T(j)>global_mid_temp;
cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4) .*R;
H = (a1 + a2.*T./2 + a3.*T.^2./3 + a4.*T.^3./4 +a5.*T.^4./5).*R.*T;
S = (a1.*log(T) + a2.*T + a3.*T.^2./2 + a4.*T.^3./3 + a5.*T.^4./4 +a7).*R;
else
cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4) .*R;
H = (a8 + a9.*T./2 + a10.*T.^2/3 + a11.*T.^3./4 +a12.*T.^4./5).*R.*T;
S = (a8.*log(T) + a9.*T + a10.*T.^2./2 + a11.*T.^3./3 + a12.*T.^4./4 +a14).*R;
end
end
end
here the if is used. As per given statement high and low temperature as per that the efficient value is taken as input and calculate it.
As per the above statement need to provide the separate folder for each species and plot the separate graph mkdir is used as per the program
The plotting’s are programmed for save the plot png. change directory is used to change folder and save as command is used for save the plot and changed the directory to were main program is then fprintf is used to display the molecular weight species display in command window as per the given statement
Another function is used to calculate the molecular weight and recalled in main program and the values are displayed in command window
function[W] = molecular_weight(species)
atomic_name = [ 'H' , 'C' , 'O' , 'N' , 'A' ];
atomic_weight = [ 1 , 12 , 16 , 14 , 40 ];
W=0;
for i = 1 : length(species);
for j=1:length(atomic_name);
if species(i)==atomic_name(j);
W=W+atomic_weight(j);
position = j;
end
end
n=str2num(species(i));
if n>=1;
W=W+atomic_weight(position)*(n-1);
Here
end
end
here as per the atomic names the symbols and there atomic weight are mentioned and assigned and with use of if command each variable is compared with the atomic name and species and value is taken in case the variable is number for that there is another if command is used the position of the last weight is taken and multiplied with the number and values are added and out is taken as per the formula
The function recalled to main program and value is displayed in command window. In this way 53 times iteration is takes place as per I explained they are saved and I attached the report with it
Main program:
clear all
close all
clc
f1 = fopen('THERMO.dat');
filename = fgetl(f1);
for i=1:4
fgetl(f1);
end
for i=1:53
tline = fgetl(f1);
A = strsplit(tline,' ');
X = length(A);
global_low_temp = str2num(A{X-3});
global_mid_temp = str2num(A{X-1});
global_high_temp = str2num(A{X-2});
species = tline(1:6);
line1=fgetl(f1);
l1=findstr(line1,'E');
a1=str2double(line1(1:(l1(1))+3));
a2=str2double(line1((l1(1)+4):(l1(2)+3)));
a3=str2double(line1((l1(2)+4):(l1(3)+3)));
a4=str2double(line1((l1(3)+4):(l1(4)+3)));
a5=str2double(line1((l1(4)+4):(l1(5)+3)));
line2=fgetl(f1);
l2=findstr(line2,'E');
a6=str2double(line2(1:(l2(1))+3));
a7=str2double(line2((l2(1)+4):(l2(2)+3)));
a8=str2double(line2((l2(2)+4):(l2(3)+3)));
a9=str2double(line2((l2(3)+4):(l2(4)+3)));
a10=str2double(line2((l2(4)+4):(l2(5)+3)));
line3=fgetl(f1);
l3=findstr(line3,'E');
a11=str2double(line3(1:(l3(1))+3));
a12=str2double(line3((l3(1)+4):(l3(2)+3)));
a13=str2double(line3((l3(2)+4):(l3(3)+3)));
a14=str2double(line3((l3(3)+4):(l3(4)+3)));
mkdir('projectspecies',species);
T= linspace(global_low_temp,global_high_temp,1000);
[cp, H, S] = coeff (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,global_mid_temp);
%plotting
figure(1);
plot(T,cp,'linewidth',3);
title(species);
xlabel('temperature');
ylabel('specific_heat');
figure(2);
plot(T,H,'linewidth',3);
title(species);
xlabel('temperature');
ylabel('enthalpy');
figure(3)
plot(T,S,'linewidth',3)
title(species)
xlabel('temperature')
ylabel('entropy')
cd('D:\matlab\Matlab for mechanical\projectspecies')
mkdir(species)
cd(species)
saveas(figure(1),'specific heat.png')
saveas(figure(2),'enthalpy.png')
saveas(figure(3),'entropy.png')
cd 'D:\matlab\Matlab for mechanical'
fprintf('molecular weight of %s ',species)
[W] = molecular_weight(species)
end
fclose(f1)
Function 1
function [cp, H, S] = coeff (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,global_mid_temp)
R=8.314;
for j = 1:length(T);
if T(j)>global_mid_temp;
cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4) .*R;
H = (a1 + a2.*T./2 + a3.*T.^2./3 + a4.*T.^3./4 +a5.*T.^4./5).*R.*T;
S = (a1.*log(T) + a2.*T + a3.*T.^2./2 + a4.*T.^3./3 + a5.*T.^4./4 +a7).*R;
else
cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4) .*R;
H = (a8 + a9.*T./2 + a10.*T.^2/3 + a11.*T.^3./4 +a12.*T.^4./5).*R.*T;
S = (a8.*log(T) + a9.*T + a10.*T.^2./2 + a11.*T.^3./3 + a12.*T.^4./4 +a14).*R;
end
end
end
Function 2
function[W] = molecular_weight(species)
atomic_name = [ 'H' , 'C' , 'O' , 'N' , 'A' ];
atomic_weight = [ 1 , 12 , 16 , 14 , 40 ];
W=0;
for i = 1 : length(species);
for j=1:length(atomic_name);
if species(i)==atomic_name(j);
W=W+atomic_weight(j);
position = j;
end
end
n=str2num(species(i));
if n>=1;
W=W+atomic_weight(position)*(n-1);
end
end