Challenge

Project 1 - Parsing NASA thermodynamic data

clear all
close all
clc
%inputs 
R=8.314;
%open an reading the data file
f1=fopen('THERMO.dat','r')
get=fgetl(f1);

% Reading the temperature
tline=fgetl(f1)
%splitting the string
s=strsplit(tline,' ');
global_low_temp=str2num(s{2});
global_mid_temp=str2num(s{3});
global_high_temp=str2num(s{4});
T=linspace(low_temp,high_temp,1000);
%For skipping comment lines
for i=1:3
get=fgetl(f1)
end

%Reading the species
for j=1:53
  tline=fgetl(f1)

  A=strsplit(tline,' ');
  species=A(1); %species name
  % converting string to number
  low_temp=str2num(A{end-3});
  mid_temp=str2num(A{end-2});
  high_temp=str2num(A{end-1});
  
  tline1=fgetl(f1);
  %finding the string E
  a=strfind(tline1,'E');
  %finding the coefficients
  a1=tline1(1:a(1)+3);
  a1=str2num(a1)
  a2=tline1(a(1)+4:a(2)+3);
  a2=str2num(a2);
  a3=tline1(a(2)+4:a(3)+3);
  a3=str2num(a3);
  a4=tline1(a(3)+4:a(4)+3);
  a1=str2num(a4);
  a5=tline1(a(4)+4:a(5)+3);
  a5=str2num(a5);
  
  tline2=fgetl(f1);
  b=strfind(tline2,'E');
  a6=tline2(1:b(1)+3);
  a6=str2num(a6);
  a7=tline2(b(1)+4:b(2)+3);
  a7=str2num(a7);

  %For finding lower co efficients
  a8=tline2(b(2)+4:b(3)+3);
  a8=str2num(a8);
  a9=tline2(b(3)+4:b(4)+3);
  a9=str2num(a9);
  a10=tline2(b(4)+4:b(5)+3);
  a10=str2num(a10);

  tline3=fgetl(f1);
  c=strfind(tline3,'E');
  a11=tline3(1:c(1)+3);
  a11=str2num(a11);
  a12=tline3(c(1)+4:c(2)+3);
  a12=str2num(a12);
  a13=tline3(c(2)+4:c(3)+3);
  a13=str2num(a13);
  a14=tline3(c(3)+4:c(4)+3);
  a14=str2num(a14);

  % Calculating the specific heat
 % cp= R*T*(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12)
  if (T<=mid_temp)
      cp=R*(a8+(a9*T)+(a10*(T).^2)+(a11*(T).^3)+(a12*(T).^4))
  else 
      cp=R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4))
  end

%Calculating entropy
% S =(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,mid_temp)
%for higher and lower coefficients
if (T<=mid_temp)
  S=R*(a8.*log(T)+(a9*T)+((a10*(T).^2)/2)+((a11*(T).^3)/3)+((a12*(T).^4)/4)+a14);
else
  S= R*(a1.*log(T)+(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(T).^4)/4)+a7); 
end

% calculating enthalpy
%H= (a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,mid_temp)
if (T<=mid_temp)
  H= R*T.*(a8+(a9*(T)/2)+((a10*(T)^.2)/3)+((a11*(T)^.3)/4)+((a12*(T)^.4)/5)+(a13./T));
else
  H= R*T.*(a1+(a2*(T)/2)+((a3*(T)^.2)/3)+((a4*(T)^.3)/4)+((a5*(T)^.4)/5)+(a6./T));  
end

%plotting cp,H,S with temperature(T)
figure(1)
plot(T,cp,'linewidth',2,'color','r');
xlabel('TEMPERATURE');
ylabel('SPECIFIC HEAT');
title('specific heat vs temperature');

%plot2 S vs T
figure(2)
plot(T,S,'linewidth',2,'color','b');
xlabel('TEMPERATURE');
ylabel('ENTROPY');
title('entropy vs temperature');
   
%plot3 H vs T
figure(3)
plot(T,H,'linewidth',2,'color','g');
xlabel('TEMPERATURE');
ylabel('ENTHALPY');
title('enthalpy vs temperature');

end

First we have taken the input R.Now to open and read the THERMO.dat file we used commands like fopen and fgetl.To read the lines of the file,the tline variable is used to read the different lines accordingly.Then to read the global temperature line,the tline variable is used to read it and the command strsplit is used to splitting the string and the string is converted into number using str2num command.The global temperature was provided with linspace of lowtemp to hightemp with range of 1000 between them.Then the temperature values are stored in assigned variables.Now for skipping the three comment lines we have used for loop.

Now to the species line,again we have used tline variable to read it.Splitting of the line was done using strsplit and converted into numbers and stored in assigned variables.Now to read another line by using tline1 variable,the strfind command was used to find the string with delimiter as E and string is changed into number and the name of the species was read first.The lower and higher coefficients with assigned variables was stored into number datatype.To read the another line by using tline2 ang fgetl command and the other co efficients were read,changed into number and stored as mentioned.

For calculating the specific heat,entropy and enthalpy with lower and higher coefficients the general mathematical expression.The if else command was used to differentiate between the lower coefficients and higher coefficients foe the specified temperature of the species.

Also for calculating the entropy and enthalpy if else command is used for lower and higher coefficients.

Now to plot the cp,S,H vs T by using plot command and specify the different color for different plots.
When finding the string in string line make sure the delimiter and the variable which the strfind is stored.To go to the next string in the same string line carefull while giving the range.Also when converting string into number make use of parenthisis and brackets and the string to change.Make sure of giving the desired variable at each string and also in converting it. 
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.