Project 1 - Parsing NASA thermodynamic data
clear all
close all
clc
%input
R = 8.314;
d_1=fopen('THERMO.dat','r');
dataline_1=fgetl(d_1);
dataline_2=fgetl(d_1);
glob_temp = strsplit(dataline_2,' ');
max_glob_temp=str2num(glob_temp{4});
mid_glob_temp=str2num(glob_temp{3});
min_glob_temp=str2num(glob_temp{2});
fgetl(d_1)
fgetl(d_1)
fgetl(d_1)
%creating a new folder
mkdir 'c:\users\student.local\desktop\fp'
A1='end';
for i = 1:Inf
dataline_6_str = fgetl(d_1)
dataline_6 = strsplit(dataline_6_str,' ');
species = dataline_6{1}
tf = strcmpi(A1,species)
if tf == 1
break
end
max_temp= str2num(dataline_6{end-2});
mid_temp= str2num(dataline_6{end-1});
min_temp= str2num(dataline_6{end-3});
%extracting the coefficients
%for dataline 7
dataline_7 = fgetl(d_1);
pt=strfind(dataline_7,'E');
cf1_str= dataline_7(1: pt(1)+3);
cf2_str= dataline_7(pt(1)+4 : pt(2)+3);
cf3_str= dataline_7(pt(2)+4 : pt(3)+3);
cf4_str= dataline_7(pt(3)+4 : pt(4)+3);
cf5_str= dataline_7(pt(4)+4 : pt(5)+3);
cf1=str2num(cf1_str);
cf2=str2num(cf2_str);
cf3=str2num(cf3_str);
cf4=str2num(cf4_str);
cf5=str2num(cf5_str);
%for dataline 8
dataline_8 = fgetl(d_1);
pt=strfind(dataline_8,'E');
cf6_str= dataline_8(1: pt(1)+3);
cf7_str= dataline_8(pt(1)+4 : pt(2)+3);
cf8_str= dataline_8(pt(2)+4 : pt(3)+3);
cf9_str= dataline_8(pt(3)+4 : pt(4)+3);
cf10_str= dataline_8(pt(4)+4 : pt(5)+3);
cf6=str2num(cf6_str);
cf7=str2num(cf7_str);
cf8=str2num(cf8_str);
cf9=str2num(cf9_str);
cf10=str2num(cf10_str);
%for dataline 9
dataline_9 = fgetl(d_1);
pt=strfind(dataline_9,'E');
cf11_str= dataline_9(1: pt(1)+3);
cf12_str= dataline_9(pt(1)+4 : pt(2)+3);
cf13_str= dataline_9(pt(2)+4 : pt(3)+3);
cf14_str= dataline_9(pt(3)+4 : pt(4)+3);
cf11=str2num(cf11_str);
cf12=str2num(cf12_str);
cf13=str2num(cf13_str);
cf14=str2num(cf14_str);
%evaluating thermodynamic properties such as cp, H, S
local_t_range = linspace(min_temp,max_temp,100);
for j=1:length(local_t_range)
t = local_t_range(j);
if t>1000
Cp(j)= R*( cf1 + cf2*t + cf3*(t^2) + cf4*(t^3) + cf5*(t^4) );
H(j) = (R*t)*( cf1 + (cf2*t)/2 + (cf3*t^2)/3 + (cf4*t^3)/4 + (cf5*t^4)/5 + cf6/t);
S(j) = R*(cf1*log(t) + cf2*t + (cf3*t^2)/2 + (cf4*t^3)/3 + (cf5*t^4)/4 + cf7);
else t<=1000
Cp(j)= R*( cf8 + cf9*t + cf10*(t^2) + cf11*(t^3) + cf12*(t^4) );
H(j) =(R*t)*( cf8 + (cf9*t)/2 + (cf10*t^2)/3 + (cf11*t^3)/4 + (cf12*t^4)/5 + cf13/t);
S(j) = R *(cf8*log(t) + cf9*t + (cf10*t^2)/2 + (cf11*t^3)/3 + (cf12*t^4)/4 + cf14);
end
end
%making subfolder
mkdir (sprintf('c:/users/student.local/desktop/fp/%s',species))
figure(1)
plot(local_t_range,Cp)
xlabel('temperature')
ylabel('specific heat')
title('variation of specific heat with temperature','color',[0 rand rand])
figure(2)
plot(local_t_range,H)
xlabel('temperature')
ylabel('enthalphy')
title('variation of enthalphy with temperature','color',[rand 0 rand])
figure(3)
plot(local_t_range,S)
xlabel('temperature')
ylabel('entropy')
title('variation of entropy with temperature','color',[rand rand rand])
for k = 1:3
subfilename = sprintf('c:/users/student.local/desktop/fp/%s/figure_%d.jpg',species,k);
saveas(figure(k),subfilename);
end
%calculating molecular weight
mol_weight = molecular_weight(species);
ff = fprintf('the molecular weight of %s is %d.\n',species,mol_weight)
end
%function
function y = molecular_weight(species)
[O,H,C,S,N,AR] = deal(16,1,12,32,14,39.948)
elements = ['O','H','C','S','N','AR'];
weight = [ O , H , C , S , N , AR];
mole_wgt= 0 ;
for i = 1:length(species);
for j = 1:length(elements);
td=strcmpi(species(i),elements(j));
tf=strcmpi(species(i),'A');
tn=strcmpi(species(i),'R');
if td == 1
mole_wgt = mole_wgt + weight(j);
position = j;
elseif tf == 1
p = strcat(species(i) ,species(i+1));
q = strfind(elements,p);
mole_wgt = mole_wgt + weight(q);
position = q;
break
elseif tn == 1
break
end
end
m = str2num(species(i));
if m>1
mole_wgt = mole_wgt + weight(position)*(m-1);
end
end
y=mole_wgt;
end
step 1 : first by using the command fopen , we open and read the thermo.dat file
Step 2 : by using fgetl the second line is read
Step 3 : the second line is splitted by using strsplit command , then every elements in that line are converted to numberic values from strings , this will help us gaining the global temperature range
Step 4 : by entering fgetl three times ..we skip the next 3 lines
Step 5 : next we create a new folder using mkdir command and naming that folder as fp
Step 6 : next we have to take data from every species , each 4 line has 14 values of that particular species , so we use for loop to reach the end , and strcmpi command is used , which shows 1 when it reaches the word we are seeking , and it end the loop
Step 7 : the first line gives the local temperature range , by using strsplit and str2num commands
Step 8 : In the next line , the word E is found by using strfind command , which tells us the value of coefficient . The value of co efficient will be all number before E and three positions after E . Then the values are converted to numberic values from string values
Step 9 : the same if repeated for next 2 lines ..
Step 10 : after this , we calculate the values of Cp , H and S by using the formula given against every local temperature values , as the values are different so if condition is used
Step 11 : then three plots are created for cp and h and s variation for every temperature value , xlabel , ylabel and title commands are used to label them .
Step 12 : a new subfolder is created with the name of the species using mkdir command and giving the location under fp , new subfolder is created for each species on their corresponding three figures by using for loop ,
Step 13 : then the molecular weight is calculated by calling the function defined and results is printed in command window ,
FUNCTION :
Step 1 : here species is taken as input , after assigning value to each possible elements of species and everything were defined here
Stel 2 : every entry of species is called one by one by for loop , then another for loop is used to check that entry agress with any element , strcmpi , if , elseif commands are used here .
Step 3 : if entry doesn't match with any of the given conditions , i come to second loop , now the entry to the species is converted into numberic data by str2num ,
ERRORS FACED :
in line 108 , i have inputed i values for the loop , instead of placing j there , so the values of cp is not calculated and the graph is implotted ,
next in line 142 , i have missed the k value in the figure command , so the loop dosent run and throws me an error, after go throughing the documentation file , i have managed to find how it works , and i have made several mistakes which are common like , improper coding of the command names and missing pranthesis , etc .. as this problem was a quite difficult for me to understand, it took me a long to understand , so i cant attach all the errors thrown while coding