close all
clear
clc
%Inputs
R= 8.314;
%Reading the data file
f1 = fopen('THERMO.dat','r');
%Reading the first line of the file
L1 = fgetl(f1);
%Getting the global minimum, maximum and mid temperatures
L2 = fgetl(f1);
t = strsplit(L2);
global_min = str2double(t{2});
global_mid = str2double(t{3});
global_max = str2double(t{4});
%Defining the temperature range
temp = linspace (global_min,global_max,500);
%Ignoring the comments in the file
for i=1:3
fgetl(f1);
end
for i=1:53
%Reading the first line which includes the information for species
l=fgetl(f1);
%Reading the name of the species
element_name=strtok(l);
%Reading the three lines which contains 7 high temp coefficients & 7 low temp
%coefficients
l1=fgetl(f1);
l2=fgetl(f1);
l3=fgetl(f1);
%Finding the position of E so that we can extract the coefficients
a=strfind(l1,'E');
b=strfind(l2,'E');
c=strfind(l3,'E');
%Extracting high temp coefficient
a1=str2double(l1(1:a(1)+3));
a2=str2double(l1(a(1)+3:a(2)+3));
a3=str2double(l1(a(2)+4:a(3)+3));
a4=str2double(l1(a(3)+4:a(4)+3));
a5=str2double(l1(a(4)+4:a(5)+3));
a6=str2double(l2(1:b(1)+3));
a7=str2double(l2(b(1)+4:b(2)+3));
%Extracting low temp coefficient
b1=str2double(l2(b(2)+4:b(3)+3));
b2=str2double(l2(b(3)+4:b(4)+3));
b3=str2double(l2(b(4)+4:b(5)+3));
b4=str2double(l3(1:c(1)+3));
b5=str2double(l3(c(1)+4:c(2)+3));
b6=str2double(l3(c(2)+4:c(3)+3));
b7=str2double(l3(c(3)+4:c(4)+3));
%Calculating specific heat
c_p = specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,...
global_min,global_max,global_mid);
%Calculalting enthalpy
H = enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,...
global_max,global_mid);
%calculating entropy
S = entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,...
global_max,global_mid);
%Plotting graphs
plotting(c_p,H,S,temp,element_name);
%Calculating molecular weights
mol_wt(i) = Molecular_weight(element_name);
disp(mol_wt(i));
Molucular_Weight = strcat(mol_wt(i),element_name)
%Function to write molecular weight to a file
weight = mol_wt(i);
writing_to_file(element_name, weight);
end
fclose(f1)
functions
1) specific heat
function c_p = specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,...
temp,global_min,global_max,global_mid)
if temp<=global_mid
c_p = R*(a1 + a2.*temp + a3.*(temp.^2) + a4.*(temp.^3) + a5.*(temp.^4));
else
c_p = R*(b1 + b2.*temp+b3.*(temp.^2) + b4.*(temp.^3) + b5.*(temp.^4));
end
2) Entropy
function S = entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid)
if temp<=global_mid
S = R*(a1.*log(temp) + a2.*temp + a3.*(temp.^2)./2 + a4.*(temp.^3)./3 + a5.*(temp.^4)./4 + a7);
else
S = R*(b1.*log(temp) + b2.*temp + b3.*(temp.^2)./2 + b4.*(temp.^3)./3 + b5.*(temp.^4)./4 + b7);
end
3) Enthalpy
function H = enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,...
global_min,global_max,global_mid)
if temp <=global_mid
H = R.*temp.*(a1 + a2.*(temp./2) + a3.*((temp.^2)./3) + a4.*((temp.^3)./4)...
+ a5.*((temp.^4)./5) + a6./temp);
else
H = R.*temp.*(b1 + b2.*(temp./2) + b3.*((temp.^2)./3) + b4.*((temp.^3)./4)...
+ b5.*((temp.^4)./5) + b6./temp);
end
4) Molecular weight
function W = Molecular_weight(species)
% Defining Atomic name of the species
atomic_name = ['H','C','O','N','Ar'];
% Defining Atomic masses of main species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
W=0; % Molecular weight starting range
for i=1:length(species) % Loop runs to the length of the species
for j=1:length(atomic_name) % Loop runs to the length of the atomic names
if strcmp(species(i), atomic_name(j)) %function to compare 2 strings
W = W + atomic_weight(j);
position = j;
end
end
n = str2num(species); % Now we will find if there is more element in the species then incrementing atomic weight of the species
if n>1
W = W + atomic_weight(position)*(n-1);
end
end
fprintf('Molecular Weight of %s :- ',species);
fprintf('%f',W);
disp(' ')
end
5) Plotting
function plotting(c_p,H,S,temp,element_name)
cd(pwd);
dir_name=element_name;
mkdir(dir_name);
%Plotting temperature vs specific heat
figure(1)
plot(temp,c_p,'lineWidth',3,'color','r');
title(sprintf('Specific Heat vs Temperature rn for %s:',element_name));
xlabel('Temperature');
ylabel('Specific Heat');
% Plotting temperature vs Enthalpy
figure(2)
plot(temp,H,'linewidth',3,'color','g');
title(sprintf('Enthalpy vs Temperature rn for %s:',element_name));
xlabel('Temperature');
ylabel('Enthalpy');
% Plotting temperature vs Entropy
figure(3)
plot(temp,S,'linewidth',3,'color','b');
title(sprintf('Entropy vs Temperature rn for %s:',element_name));
xlabel('Temperature');
ylabel('Entropy');
cd(dir_name);
saveas(1,'c_p_vs_temp.png');
saveas(2,'H_vs_temp.png');
saveas(3,'S_vs_temp.png');
cd .. ;
end
6) writting to file
function writing_to_file(element_name,weight)
dir_name = element_name;
mol_file = fopen('mol_wt.dat','a+');
fprintf(mol_file, 'Molecular weight of %s is %f n', dir_name,weight);
fclose(mol_file);
end