data processing
introduction
this project was to extract a data from the file themo.dat and make the the plots and finf the molecular weight of the molecule
explanation summary
first created file to store the values extracted from the data file and ihad to figure out proper ways of extracting the data out of the datha file and store it in a seperate which was the hardest part of the this assignment ,faced so mant errors during this
then after sucessfully extracting the data i created the plots of the molecules and stored it in a sepreate file (using mkdir and change direcory) then ihad to calculate the molecular weight of each molecule found the atomic mass of each element in the inter net and using that the molecular mass was calculated
coding
in the starting i open the file(usingfopen)and gave the file a pointer variable, then icreated two more files to store the values of coefficient and temprature range (usinf fgetl)
then reading the values stored in the coefficients
then comes the coding of the molecularmass function(falsly named as molar mass)
the values of the atomomic weight which igot from the internet is copied in this piece of code ,here i created 3 files to write the values of temprature,details ofthe molecule and the name of the molecule this was the three file created then by reading those files i calculated the values of molecular mass this molecular mass value is then called from the main code and along with the plots the value of molecular mass is saved in a text file when we look at the plots close enough we can see that the plot is split into two sections this is beacuse of the different coefficeients for the different coefficient range (low-medium and medium-high)
lots of small small snippets used for extracting the data has made this code this big othe wise it would have been shot
honestly this so far this the biggest set of code i have ever written thus i faced a lot of errors
but was a great experince for me in coding
maincode
%reading from nasa file and writing
f1=fopen('THERMO.dat','r');
f2=fopen('co_efficients.txt','w')
f3=fopen('temprature range.txt','w')
%startline skip
for i=1:2
a=fgetl(f1);
b=fgetl(f1);
c=fgetl(f1);
end
% writing values of temp and coeff
r=8.314
q=c(1:10);
x=str2num(c(48:75))%temp range value1
fprintf(f3,'%10d\n',x)
%loop for temp and coeff
for i=1:53
a=fgetl(f1);
b=fgetl(f1);
c=fgetl(f1);
e=fgetl(f1);
if any (size(e)>20)
z=str2num(e(48:75))%temp range for all values except last
fprintf(f3,'%10d\n',z)
end
d1=[a(1:75),b(1:75),c(1:75)]
d=findstr(d1,'E');
d=[-3,d(1:14)]
for j=1:14
v(j)=str2num(d1(d(j)+4:d(j+1)+3))
fprintf(f2,'%20d\n',v(j))% writing coefficients
end
end
fclose(f3)
fclose(f1)
fclose(f2)
%reading the written file
f4=fopen('temprature range.txt','r')
f5=fopen('co_efficients.txt','r')
f6=fopen('molecules.txt','r')
fgetl(f6)
k=molarmass()
ct=1
%loop for reading temp range
for i=1:53%give the value 53
d=fgetl(f6)
e=d(1:10)
a=str2num(fgetl(f4))
b=str2num(fgetl(f4))
c=str2num(fgetl(f4))
t=a:c%low temp
t1=c:b%high temp
for j=1:7
q(j)=str2num(fgetl(f5))%low temp coefficients
end
for l=1:7
q1(l)=str2num(fgetl(f5))%high temp coefficients
end
mkdir(e)%make a directory
cd(e)%change the working directory
f7=fopen('molarmass.txt','w')
fprintf(f7,'%s\n%d%s\n%s','molecularmass',k(i),'AMU','1amu=1.6605e-27kg')
%low temp
term1=q(2).*t
term2=q(3).*t.^2
term3=q(4).*t.^3
term4=q(5).*t.^4
cp=r.*(q(1)+term1+term2+term3+term4+q(5).*t.^3)
h=r.*(q(1)+term1/2+term2/3+term3/4+term4/5+q(6)./t).*t
s=r.*(q(1).*log(t)+term1+term2/2+term3/3+term4/4+q(7))
%high temp
term1=q1(2).*t1
term2=q1(3).*t1.^2
term3=q1(4).*t1.^3
term4=q1(5).*t1.^4
cp1=r.*(q1(1)+term1+term2+term3+term4)
h1=r.*(q1(1)+term1/2+term2/3+term3/4+term4/5+q(6)./t1).*t1
s1=r.*(q1(1).*log(t1)+term1+term2/2+term3/3+term4/4+q1(7))
%plots
figure(ct)
hold on
plot(t,cp,'linewidth',3,'color','r');
plot(t1,cp1,'linewidth',3,'color','r');
xlabel('tempratutre range ');
ylabel('specfic heat capacity')
plot1=sprintf('(t vs cp) plot %s.png',e);
saveas(gcf,plot1);
ct=ct+1
figure(ct)
hold on
plot(t,h,'linewidth',3,'color','b');
plot(t1,h1,'linewidth',3,'color','b');
xlabel('temp range')
ylabel('enthalphy')
plot2=sprintf('(t vs h)plot %s.png',e);
saveas(gcf,plot2);
ct=ct+1
figure(ct)
hold on
plot(t,s,'linewidth',3,'color','y');
plot(t1,s1,'linewidth',3,'color','y');
xlabel('temp range')
ylabel('entropy')
plot3=sprintf('(t vs s)plot%s.png',e)
saveas(gcf,plot3);
ct=ct+1
cd .. %return to previous directory
end
fclose(f5)
fclose(f6)
fclose(f4)
function for molecular weight calculation
function F=molarmass()
%molar mass of atom
C=12.0107;
H=1.00784;
N=14.0067;
S=32.065;
O=15.999;
AR=39.948;
amu=1.66054e-18;
%reading from the data and writing it in a file
f1=fopen('THERMO.dat','r');
f2=fopen('molecules.txt','w')
f3=fopen('atom_no.txt','w')
f4=fopen('temp.txt','w')
%start line skip
for i=1:5
fgetl(f1);
end
%data for molecules txt file
for i=1:53
m=(fgetl(f1))
p(i)={m(1:10)}
atom1(i)={m(25:26)};
no_of_atom_1(i)=m(29:29);
atom2(i)={m(30:31)};
no_of_atom_2(i)=m(34:34);
atom3(i)={m(35:36)};
no_of_atom_3(i)=m(39:39);
%skip coefficient line
for j=1:3
fgetl(f1);
end
end
%writing the header file of moleculetxt
fprintf(f2,'%10s%10s%10s%10s%10s%10s%10s\n','molecule','atom1','atom1no','atom2','atom2no','atom3','atom3no')
atom1=cell2mat(atom1)
atom2=cell2mat(atom2)
atom3=cell2mat(atom3)
%atom3=[' ',atom3(1:105)]
atom3(23)='S'%the text of this was missing from the file
z=' ';
%making the other than non numeric values zero
n=1
for i=1:53
if no_of_atom_1(i)==z
no_of_atom_1(i)='0'
end
if (no_of_atom_2(i))==z
no_of_atom_2(i)='0'
end
if (no_of_atom_3(i))==z
no_of_atom_3(i)='0'
end
if (atom2(n))==z
atom2(n)='0'
end
if (atom3(n))==z
atom3(n)='0'
end
n=n+2
end
%writing the data into the molecule txt
for i=1:53
noA1(i)=str2num(no_of_atom_1(i))
noA2(i)=str2num(no_of_atom_2(i))
noA3(i)=str2num(no_of_atom_3(i))
end
noA3(12)=1
n=1
for i=1:53
fprintf(f2,'%10s%10s%10d%10s%10d%10s%10d',p{i},atom1(n:n+1),noA1(i),atom2(n:n+1),noA2(i),atom3(n:n+1),noA3(i))
fprintf(f2,'\n')
n=n+2;
end
%making the atom value zero if not present
a=strfind(atom2,' ')
for i=1:length(a)
b=a(i)
atom2(b)=0
end
a=strfind(atom3,' ')
for i=1:length(a)
b=a(i)
atom3(b)=0
end
n=1
for i=1:53
a=eval(atom1(n:n+1))
b=eval(atom2(n))
c=eval(atom3(n))
F(i)=(a*noA1(i)+b*noA2(i)+c*noA3(i))
n=n+2
end
fclose(f1)
fclose(f2)
fclose(f3)
fclose(f4)
file where all the plotsand moleculear weight are stored
molecules detalis
plots