Project 1 - Parsing NASA thermodynamic data
close all
clear all
clc
f1= fopen('THERMO.dat','r');
fgetl(f1);
a = str2num(fgetl(f1));
gl_low_temp = a(1);
gl_mid_temp = a(2);
gl_high_temp = a(3);
for i=1:3
fgetl(f1);
end
for i=1:53
l1=fgetl(f1);
l= strsplit(l1,' ');
m= length(l);
species = l{1};
low_temp = str2double(l{m-3});
high_temp = str2double(l{m-2});
mid_temp = str2double(l{m-1});
l2=fgetl(f1);
a= strfind(l2,'E');
a1=str2double(l2(1:(a(1)+3)));
a2=str2double(l2((a(1)+4):(a(2)+3)));
a3=str2double(l2((a(2)+4):(a(3)+3)));
a4=str2double(l2((a(3)+4):(a(4)+3)));
a5=str2double(l2((a(4)+4):(a(5)+3)));
l3= fgetl(f1);
b=strfind(l3,'E');
a6=str2double(l3(1:b(1)+3));
a7=str2double(l3((b(1)+4):(b(2)+3)));
a8=str2double(l3((b(2)+4):(b(3)+3)));
a9=str2double(l3((b(3)+4):(b(4)+3)));
a10=str2double(l3((b(4)+4):(b(5)+3)));
l4= fgetl(f1);
c=strfind(l4,'E');
a11=str2double(l4(1:c(1)+3));
a12= str2double(l4((c(1)+4):(c(2)+3)));
a13= str2double(l4((c(2)+4):(c(3)+3)));
a14= str2double(l4((c(3)+4):(c(4)+3)));
lower_temp = linspace(low_temp,mid_temp,500);
higher_temp = linspace(mid_temp,high_temp,500);
R=8.314;
for k=1:500
high_cp(k) = (a1+(a2*higher_temp(k))+(a3*higher_temp(k).^2)+(a4*higher_temp(k).^3)+(a5*higher_temp(k).^4))*R;
high_h(k) = (a1+(a2*higher_temp(k))/2+(a3*higher_temp(k).^2)/3+(a4*higher_temp(k).^3)/4+(a5*higher_temp(k).^4)/5+(a6/higher_temp(k)))*(R*higher_temp(k));
high_s(k) = ((a1*log(higher_temp(k))+(a2*higher_temp(k))+(a3*higher_temp(k).^2)/2+(a4*higher_temp(k).^3)/3+(a5*higher_temp(k).^4)/4+(a7)));
low_cp(k) = (a8+(a9*lower_temp(k))+(a10*lower_temp(k).^2)+(a11*lower_temp(k).^3)+(a12*lower_temp(k).^4))*R;
low_h(k) = (a8+(a9*lower_temp(k))/2+(a10*lower_temp(k).^2)/3+(a11*lower_temp(k).^3)/4+(a12*lower_temp(k).^4)/5+(a13/lower_temp(k)))*(R*lower_temp(k));
low_s(k) = ((a8*log(lower_temp(k))+(a9*lower_temp(k))+(a10*lower_temp(k).^2)/2+(a11*lower_temp(k).^3)/3+(a12*lower_temp(k).^4)/4+(a14)));
end
mkdir(species)
cd(species)
figure(1)
plot(lower_temp,low_cp,'LineWidth',3,'Color','c');
hold on
plot(higher_temp,high_cp,'LineWidth',3,'Color','c');
hold off
xlabel('Temperature in K');
ylabel('Specific heat in KJ/k');
title(['Temperarure VS Specific Heat',species]);
saveas(figure(1),'Cp vs temp.jpg')
figure(2)
plot(lower_temp,low_h,'LineWidth',3,'Color','m');
hold on
plot(higher_temp,high_h,'LineWidth',3,'Color','m');
hold off
xlabel('Temperature');
ylabel('Enthalpy');
title(['Temperarure VS Enthalpy',species]);
saveas(figure(2),'enthalpy vs temp.jpg')
figure(3)
plot(lower_temp,low_s,'LineWidth',3,'Color','b');
hold on
plot(higher_temp,high_s,'LineWidth',3,'Color','b');
hold off
xlabel('Temperature');
ylabel('Entropy');
title(['Temperarure VS Entropy',species]);
saveas(figure(3),'entropy vs temp.jpg')
cd ..
end
Explanation of code
- we star the code with calling our data file in the matlab with read funtion.
- then we are defining temperature values as global low, medium & high. the command str2num is used to create a array of numbers present in the line.
- as we can see in the data file three lines are of no use so we have skipped those three lines with the use of loop function by giving the no stored value.
- we can count that there are 53 values present in the data. so the next lines we are segrigating each value by using strsplit command.
- Local temperature values are stored as low, mid & high temp.
- Now we are finding coefficients by using str2double as each coeffiecient value is having the term E in it and additional 2-3 numbers after E. command strfind i used for finding E.
- From L2,L3 & L4. 14 cofficient values are taken. these coffiecients are used in the formulae given for calculating specific heat, enthalpy & entropy.
- Now 500 values between lower and higher temp are taken. randomly for plotting Graph.
- A loop k is created for formulae to run for 500 times as we took 500 random temp values.
- In next lines we are creating folder for plots to be saved by using mkdir & cd commands.
- Now the plots codes are written as usual the lables and titles are given to them at the end we have added a command saveas which saves the plots created by the above code will be saved in folder we have created.
- same plot code is repeated for 2 times for different plots.
Results
- Plots of O2,N2,Co2 are attached.
- Screen shot of Folder is also attached.
{ in this code i have taken two temp arrays such as lower and higher temp values and kept them in a loop to perform for graphs.
insted of this method we can also use an if else function in which we can create one single array of temp then the values which are lesser then tousand are taken as lower temp and above are taken as higher }
Errors occured
species = l{1};
low_temp = str2double(l{m-3});
high_temp = str2double(l{m-2});
mid_temp = str2double(l{m-1});
- in above code i was using a normal bracket's insted of flower brackets. so my values where not comming.
- I also didnt know how to creat folder for storing the whole graphs mentors helped me with that.