clear all
close all
clc
%% PART 1: INPUTS
R = 8.314; % UNIVERSAL GAS CONSTANT (J/mol-K).
% Opening the THERMO.dat file.
A = fopen('THERMO.dat','r');
fgetl(A); % Reading the first line.
tline=fgetl(A); % Reading and saving second line as 'tline'.
a=strsplit(tline,' '); % removing spaces present in between.
GLT=str2double(a{2}); % GLOBAL LOW TEMPEERATURE (K).
GMT=str2double(a{3}); % GLOBAL MID TEMPEERATURE (K).
GHT=str2double(a{4}); % GLOBAL HIGH TEMPEERATURE (K).
% Skipping the next three lines.
for i = 1:3
skip = fgetl(A);
end
i=1;
%% PART 2: READING SPECIES HEADER DATA AND COEFFICIENTS DATA:
while ~feof(A) % initiating while loop till the end of the file.
l = fgetl(A); % Reading new line.
V = strsplit(l,' '); % Removing spaces in between.
% Obtaining local temperature values for different species from line No. 1.
if(strcmpi(V{end},'1')) % Checking if present line is 1st line out of 4 lines group for each species.
name{i}=V{1};
Local_Low_Temp = str2double(V{end-3}); % LOCAL LOW TEMPEERATURE (K).
Local_High_Temp = str2double(V{end-2}); % LOCAL HIGH TEMPEERATURE (K).
Local_Mid_Temp = str2double(V{end-1}); % LOCAL MID TEMPEERATURE (K).
i=i+1;
end
% Obtaining coefficients C1-C5 values for different species from line No. 2.
if (strcmpi(V{end},'2')) % Checking if present line is 2nd line out of 4 lines group for each species.
p=strfind(l,'E'); % finding position of 'E', which is repeated in the line.
a1=l(1:p(1)+3);
C1(i-1)=str2double(a1); % 'C1' is coefficient row vector of C1 for all species.
a2=l(p(1)+4:p(2)+3);
C2(i-1)=str2double(a2); % 'C2' is coefficient row vector of C2 for all species.
a3=l(p(2)+4:p(3)+3);
C3(i-1)=str2double(a3); % 'C3' is coefficient row vector of C3 for all species.
a4=l(p(3)+4:p(4)+3);
C4(i-1)=str2double(a4); % 'C4' is coefficient row vector of C4 for all species.
a5=l(p(4)+4:p(5)+3);
C5(i-1)=str2double(a5); % 'C5' is coefficient row vector of C5 for all species.
end
% Obtaining coefficients C6-C10 values for different species from line No. 3.
if (strcmpi(V{end},'3')) % Checking if present line is 3rd line out of 4 lines group for each species.
p=strfind(l,'E'); % finding position of 'E', which is repeated in the line.
a6=l(1:12+3);
C6(i-1)=str2double(a6); % 'C6' is coefficient row vector of C6 for all species.
a7=l(p(1)+4:p(2)+3);
C7(i-1)=str2double(a7); % 'C7' is coefficient row vector of C7 for all species.
a8=l(p(2)+4:p(3)+3);
C8(i-1)=str2double(a8); % 'C8' is coefficient row vector of C8 for all species.
a9=l(p(3)+4:p(4)+3);
C9(i-1)=str2double(a9); % 'C9' is coefficient row vector of C9 for all species.
a10=l(p(4)+4:p(5)+3);
C10(i-1)=str2double(a10); % 'C10' is coefficient row vector of C10 for all species.
end
% Obtaining coefficients C11-C14 values for different species from line No. 4.
if (strcmpi(V{end},'4')) % Checking if the present line is 4th line out of 4 lines group for each species.
p=strfind(l,'E'); % finding position of 'E', which is repeated in the line.
a11=l(1:12+3);
C11(i-1)=str2double(a11); % 'C11' is coefficient row vector of C11 for all species.
a12=l(p(1)+4:p(2)+3);
C12(i-1)=str2double(a12); % 'C12' is coefficient row vector of C12 for all species.
a13=l(p(2)+4:p(3)+3);
C13(i-1)=str2double(a13); % 'C13' is coefficient row vector of C13 for all species.
a14=l(p(3)+4:p(4)+3);
C14(i-1)=str2double(a14); % 'C14' is coefficient row vector of C14 for all species.
end
end
T=linspace(Local_Low_Temp,Local_High_Temp,100); % row vector of temperature with 100 elements.
%% PART 3: CALCULATING SPECIFIC HEAT, ENTHALPY, AND ENTROPY FOR EACH SPECIES.
for j=1:53
% Specific Heat calculation using its function.
[Cp(j,:)] = Specific_Heat(C1(j),C2(j),C3(j),C4(j),C5(j),C6(j),C7(j),C8(j),C9(j),C10(j),C11(j),C12(j),C13(j),C14(j),Local_Low_Temp,Local_Mid_Temp,Local_High_Temp,T,R);
% Enthalpy calculation using its function.
[H(j,:)] = Enthalpy(C1(j),C2(j),C3(j),C4(j),C5(j),C6(j),C7(j),C8(j),C9(j),C10(j),C11(j),C12(j),C13(j),C14(j),Local_Low_Temp,Local_Mid_Temp,Local_High_Temp,T,R);
% Entropy calculation using its function.
[S(j,:)] = Entropy(C1(j),C2(j),C3(j),C4(j),C5(j),C6(j),C7(j),C8(j),C9(j),C10(j),C11(j),C12(j),C13(j),C14(j),Local_Low_Temp,Local_Mid_Temp,Local_High_Temp,T,R);
end
get = input('Enter the Species Name: ', 's'); % Asking user to enter species name.
MH = strcmpi(name,get); % Checking if input species is present in THERMO.dat file.
[r,c] = find(MH==1); % r = row index; c = column index of the element = 1.
% If the species is in the file.
if c~=0
disp('Species Data Available')
Sp_Name = name{c};
mkdir(Sp_Name); % Creating directory named 'Sp_Name'.
cd(Sp_Name) % Assigning above directory as current working directory.
%% PART 4.1: PLOTTING SPECIFIC HEAT, ENTHALPY AND ENTROPY V/S TEMPERATURE OF SPECIES ENTERED BY USER:
% Specific Heat plot.
figure(1)
plot(T,Cp(c,:),'linewidth',2,'color','b');
grid on
xlabel('Temperature[K]');
ylabel('Specific Heat[J/K]');
title(Sp_Name)
image = getframe(gcf);
imwrite(image.cdata,'T vs Cp.png')
saveas(1,'Specific Heat','jpg')
% Enthalpy Plot.
figure(2)
plot(T,H(c,:),'linewidth',2,'color','b');
grid on
xlabel('Temperature[K]');
ylabel('Enthalpy[J/K]');
title(Sp_Name)
image = getframe(gcf);
imwrite(image.cdata,'T vs H.png')
saveas(2,'Enthalpy','jpg')
% Entropy Plot.
figure(3)
plot(T,S(c,:),'linewidth',2,'color','b');
grid on
xlabel('Temperature[K]');
ylabel('Entropy[J/K]');
title(Sp_Name)
image = getframe(gcf);
imwrite(image.cdata,'T vs S.png')
saveas(3,'Entropy','jpg')
cd .. % Assigning the parent directory as the current directory.
%% PART 4.2: CALCULATING MOLECULAR WEIGHT OF THE GIVEN SPECIES:
[M] = Mol_wt(Sp_Name); % Calculating Molecular Weight of species entered by user.
% Displaying the species name and molecular weight.
fprintf('The Species entered is: %s\n', Sp_Name) % Displaying the Species Name.
fprintf('Molecular Weight: %fu\n', M) % Displaying the Molecular Weight.
else % If the species is not in the file.
disp('Species Data Unavailable') % Display when entered species is not present in 'THERMO.dat' file.
end
The code is divided into 4 parts discussed one by one as follows:
PART 1: INPUTS:
In this part, all the necessary data provided in the problem arer mentioned along with the quantities that are universally known.
Line 7 ‘R, is the Universal Gas Constant.
Line 10, ‘fopen’ is the command that allows MATLAB to access a text or data file for reading or writing purposes.
‘A’ is termed as FILE IDENTIFIER. The syntax used for the ‘fopen’ command in line 10 has 2 quantities. First is the name of the file to be accessed (THERMO.dat in this case).
Second is the permission for the action performed on the file ‘r’ is for reading and ‘w’ is for writing.
So, line 10 commands to read the file named THERMO.dat.
If the value of A in the workspace is positive it implies the command is successfully executed. On the other hand, if its value is negative then the command is not executed properly and inspection is needed.
A itself is not the file, it is the identifier through which file can be accessed.
Line 12, ‘fgetl’, is the command that reads the file in a line-by-line manner. This command will read one line at a time and whenever called again will read the next line.
Line 13, ‘fgetl’ reads the second line and stores the line as it is into a variable named, ‘tline’.
Line14, ‘strsplit’ is command used to split a string array (here ‘tline’, is a string characters vector) at specified locations.
These locations are provided in single quotes and are termed as DELIMITER.
In present case the ‘tline’ is a string character vector shown below:
tline = ' 300.000 1000.000 5000.000'
For this line the delimiter is the space between two numbers.
Once executed, the above string character vector will be converted into a cell array, ‘a’.
The cell array is a special kind of array only in MATLAB. these arrays unlike other array types can store elements of different data types.
Line 14, will give a cell array ‘a’, containing 4 elements out of which the last 3 are the temperature values but right now are in string format.
To access the elements of a cell array, curly braces {} are used.
Line 15 -17, the elements of the cell array ‘a’ are one by one converted from strings into numbers so that mathematical operations can be applied to them when needed. To do so, command ‘str2double’ is used an element which is to be converted is supplied to the command.
For e.g. GLT=str2double(a{2});
This will transform (a{2}) the 2nd element of the cell array into number which is saved as variable ‘GLT’ for Global Low Temperature, and similarly for the other two also is done.
Line 21 -23, next three lines of the file are read and skipped since these three lines are comments from the writer and are of no usage.
This is done by running a for loop 3 times and each time one line from the file is read.
Line25, variable ‘i’ is created and set to 1, this variable will in counting the total number of species present in the file. It will also be used to locate the specific species location entered by the user.
PART 2: READING SPECIES DATA AND COEFFICIENTS DATA:
Line 30, while loop is initiated.
‘feof’ is MATLAB in-built function known as Test for end-of-file. This function returns a logical value equal to 1(true) when the keyword end is found out in the file, otherwise it returns the logical value 0(false).
But in line 30 while loop condition is given is ~feof which means Test for not end-of-file. This will return logical value 0 as output when the keyword ‘end’ is found in the any of the lines, and logical 1 for the rest lines.
So, loop condition will search for the keyword ‘end’ in every line and will return 1 for lines that does not have this keyword and 0 for line having it (implying that the file ends at this position).
Thus, for all lines without the keyword the loop will be executed.
Line 31, ‘fgetl’ command reads the next line which is saved as variable ‘l’.
Line 32, splits the line ‘l’ characters from the position where space is present in the line and store them as different cell array elements named ‘V’.
Line 36, checks for 1st if-statement:
‘Strcmpi’ is MATLAB in-built function which compares strings or character vectors ignoring their cases.
It returns logical value 1 for true, if the two characters are same otherwise 0 if not.
This function can also do an element wise comparison also. search for a particular character or string in an array.
In line 36, strcmpi(V{end},’1’)
V{end} is the last element of the current cell array ‘V’.
The function will check if this character is same as ‘1’.
If both are same the function gives logical value 1(meaning true) which validates the condition under scrutiny and moves ahead to execute the commands mentioned for this if statement.
If function returns value 0 (meaning false), code skips this if condition and moves forward to check the next if statement.
V{1} is the 1st element of the cell row vector, which actually is the name of the species given in line.
Line 37, name{i} is a cell row vector which will store the first element of the row vector ‘V’ as its elements for each loop iteration.
Hence for entire file, the name{i} will store the character names of all the species present in the file.
Line 39-41, the last 3 elements of V, which are the Local Temperatures for respective species is converted from the string datatype into numbers using ‘str2double’, as previously done.
Line 43, ‘i’ value is incremented by 1.
First if-statement commands finish here.
Line 48, 2nd if-condition is checked.
Here again the last element of the ‘V’ cell vector is checked whether it is character is ‘2’ or not, if it is then second if-statement commands will be executed.
The second line does not have repeated spaces in between, but the character ‘E’ is repeated in this line.
So, line 50, the strfind command finds the locations of this character in the line.
The positions for all the location are stored in a row vector named ‘p’.
This vector ‘p’ will help in calculating the coefficients values.
Line 51, a1=l(1:p(1)+3);
This command will create variable ‘a1’ which is a subset of ‘l’ such that it contains the characters of ‘l’ starting from the 1st character to the character at the positioned at (p(1)+3).
‘p’ is row vector that stores the locations of the character ‘E’.
First element of p i.e. p(1) is equal to 12, so value of p(1)+3 becomes 14.
So, in ‘a1’ 1-14 characters of the ‘l’ will be present.
This variable obtained is nothing but the coefficient ‘C1’ mentioned in specific heat (Cp), Enthalpy (H) and Entropy (S) equations provided.
But it is still a string datatype and must be converted into the double datatype, this is done in line 52 using ‘str2double’command.
In line 52 on left hand side, C1 is followed by a bracket (i-1).
It is done so that C1 will become a row vector which will store the values of C1 coefficient for all the species present in the file.
C1, on complete execution of the while loop will be a 1x53 row vector.
Line 54, same as ‘a1’, new variable named a2 is created here. It is also a subset of ‘l’ but this time the characters starts from (p(1)+4) till (p(2)+3).
This variable is the string datatype row vector of the coefficient C2.
Line 55, a2 is converted into double datatype and again the C2 obtained is a row vector which stores the C2 values for all the species.
Following the same pattern, C3, C4 and C5 are calculated. These stores the respective coefficients values for all the species.
Line 65, the second if-statement commands end here.
Line 69, the 3rd if-statement is checked here.
‘strcmpi’ checks if the last character of V is ‘3’or not.
If it is true, then again ‘p’ row vector is obtained, which contains the locations index for each time the character ‘E’ occurs in ‘l’.
After getting the ‘p’ row vector, all the coefficients present in this line are obtained first as string datatypes, and then converted into double datatype with the help of ‘str2double’ command.
C6, C7, C8, C9 and C10 are the coefficient vectors obtained from these commands. All these are row vectors storing the respective coefficient’s values for all the species.
Line 88, the 3rd if-statement commands end here.
Line 92, the 4th if-statement begins here, by checking if the end character of the V cell array is ‘4’ or not.
If true, then following the same method as done above if-statements the coefficient row vectors (C11, C12, C13 and C14) are obtained from ‘l’ in the code lines from 94-106.
In this case the ‘p’ vector will have 4 elements only since in the 4th line for any species the character ‘E’ occurs only 4 times.
Line 107 the 4th if-statement commands end here.
Line 109, the while loop finishes.
Line 111, ‘T’ is row vector having linearly spaced values of temperature starting from ‘Local_Low_Temp’ till ‘Local_High_Temp’, divided into exactly 100 elements.
PART 3: CALCULATING SPECIFIC HEAT (Cp) , ENTHALPY(H) AND ENTROPY(S) FOR EACH SPECIES:
Line 116, a for-loop is initiated with loop counter ‘j’ beginning from 1 till 53.
Line 120, ‘Cp’ is specific heat array.
Cp(j,:), the jth row of this array, is the variation of the Specific Heat for one species from the file. If j = 1; the row will be for 1st species and if j = 53 then it is for the 53rd species present in the file.
For each value of ‘j’, the specific heat function is executed. The inputs required for the specific heat functions are:
a) the jth element from each coefficient row vectors (C1-C14).
b) Local_Low_Temp.
c) Local_Mid_Temp.
d) Local_High_Temp.
e) Temperature row vector T.
f) Universal Gas Constant, R.
for j = 1,
inputs will be 1st element from all the coefficient row vectors after these, Local_Low_Temp, Local_Mid_Temp, Local_High_Temp, T and R.
These values when submitted into the Cp function will create a row vector.
Cp(1,:) this is the variation of the specific heat of the 1st species given in the file for the entire temperature range T.
And so on for all 53 values of ‘j’ an array of order 53x100 will be obtained such that each row gives the variation of specific heat of one species.
The code for the Specific Heat function is mentioned below:
function[Cp]=Specific_Heat(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,LLT,LMT,LHT,T,R)
if T > LMT
Cp = R*(C1 + C2.*T + C3.*(T.^2) + C4.*(T.^3) + C5.*(T.^4));
else
Cp = R*(C8 + C9.*T + C10.*(T.^2) + C11.*(T.^3) + C12.*(T.^4));
end
Line 1, on the left side [Cp] is the output of this function, it is the Specific heat row vector. On the right side, the name of the function is given which is followed by the inputs needed for the execution of the function. These inputs are:
Numerical values of all the coefficients mentioned in the equations (C1-C14). The coefficients used in this function are not the row vectors which were obtained from the if-statements, but is the value of one of the elements from those row vectors.
The variable (or value) entered at the place of LLT is representing Local Low Temperature. Similarly, LMT and LHT are for Local Middle Temperature and Local High Temperature respectively.
T is the temperature range within which the Cp will be calculated for all values of in the row vector T.
R is for Universal Gas Constant.
When all the inputs are correctly provided, the function will move ahead. This function will calculate the Cp values for all the temperature values present in T.
Line 3, an if-statement is checked, in which the selected value of the is compared with the Local Middle Temperature. If T value is greater than LMT, then only the function will execute the equation written in line 5. Otherwise, function will skip that line and execute the equation provided in line 7.
The output from this function will be a row vector consisting of the Specific heat (Cp) values at different temperatures.
Line 124, the Enthalpy function is called in the code.
The output of line 124 is [H(j,:)], it is the jth row of the [H] array. This row is the variation of enthalpy of a species present at the jth location in the file THERMO.dat. If j = 1; the row will be for 1st species and if j = 53 then it is for the 53rd species present in the file.
The code for the enthalpy function is mentioned below:
function [H] = Enthalpy(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,LLT,LMT,LHT,T,R)
if T > LMT
H = R*(C1.*T + 0.5*C2.*(T.^2) + (1/3)*C3.*(T.^3) + 0.25*C4.*(T.^4) + 0.2*C5.*(T.^5) + C6);
else
H = R*(C8.*T + 0.5*C9.*(T.^2) + (1/3)*C10.*(T.^3) + 0.25*C11.*(T.^4) + 0.2*C12.*(T.^5) + C13);
end
Line 1, on the left side is the output that will be obtained [H]. The output will be a row vector which is stores the enthalpy of a species at different values of temperatures within the given temperature range T.
On the right side, there is function name ‘Enthalpy()’ using which the function is called into the main program.
Within the bracket are all the input variables which must be entered into the enthalpy equations for solving them. These inputs are same as that used for the specific heat function.
Line 3, if-statement is checked whether the element picked up fro T is greater od not. If yes then enthalpy of a species at that temperature is calculated using the line 4. If not, then it is calculated using the equation in line 6.
The output is [H] is row vector of enthalpy of a species consisting 100 values for 100 different temperatures.
Line 7, the function ends.
Line 128, the entropy function is called into the main program.
This function will calculate the entropy of the species for all values of the temperature within the limits of T.
On the left side ‘[S( j , : )]’ is the jth row of the entropy array, this row is entirely for only one species. j = 1; the row will be for 1st species and if j = 53 then it is for the 53rd species present in the file.
As the loop continues to execute, for each loop iteration new such rows will be created and finally an Entropy array will be formed (S) of the order (53x100) containing entropy variations w.r.t temperature for all the species.
The code for the entropy function is given below:
function [S] = Entropy(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,LLT,LMT,LHT,T,R)
if T > LMT
S = C1*log(T) + C7 + C2.*T + 0.5*C3.*(T.^2) + (1/3)*C4*(T.^3) + 0.25*C5.*(T.^4);
else
S = C8*log(T) + C14 + C9.*T + 0.5*C10.*(T.^2) + (1/3)*C11*(T.^3) + 0.25*C12.*(T.^4);
end
Line 1, on the left side the output of the function [S] is written, on the right side, the name of the function (Entropy), using this the function is called in the main program. Name is followed by the inputs to the function needed to calculate the entropy values. The inputs to this function are also same as that for specific heat and enthalpy function.
Line 3, function checks if the selected T value is greater than the Local Middle Temperature (LMT) value, if yes then the entropy is calculated using the relation written in line 4. If T is less than LMT then the equation written in the line 6 is used for entropy calculations.
Line 7 the function ends.
Line 130, the for loop finishes here.
Line 133, here the code askes the user to enter the name of the species of which user wants to see the data. This is done using the command ‘input’. This command will display a prompt on the screen and waits for the input by user.
The prompt, in this code, displayed on screen is Enter the Species Name:
The keyword ‘s’ tells that the input is a string datatype. And is stored as the variable named ‘get’.
‘name’ is a cell array containing the names of all the species given in the file.
In line 135, strcmpi will compare each element of the array name with the variable ‘get’. This will generate a row vector ‘MH’ with elements only 1 and 0. Where 0 is the logical value representing false, meaning that the element at that particular location is not same as that given in variable ‘get’. Similarly, 1 is logical value saying true, meaning that the element is same as the variable ‘get’.
Length of MH will be 53, with either all elements as o or all elements except one as 0.
Now, if there is an element 1 in the MH vector, its column index must be found out because the column number gives the location of species entered by the user.
This is done in line 137. In this line the MATLAB in-built function ‘find’ is used. This function will check if there is any element equal to 1 and if yes then the output of this function is a two-column row vector [r, c]. where ‘r’ gives the row index of the element and ‘c’ gives the column index of the element.
For e.g. let the 1 value be present in MH vector in 12th column, then the r = 1 and c = 12. This c value tells that the species entered by user is the 12th species in the file.
Also, Cp, H and S are arrays of order 53x100, where each row is a different species.
So, Cp (12), H (12) and S (12) all are variation of the specific heat, enthalpy and entropy respectively for the species at 12th place.
Hence Cp(c, :), H(c, :) and S(c, :) are also of the same species.
Line 141, checks if the input species is present in file or not.
If c ~= 0 means that when the value of c is not equal to zero, then only the following commands will be executed.
Line 142, the ‘disp’ command will show on the screen the text saying ‘Species Data Available’.
Line 144, the cth element of the cell row vector containing names of all species ‘name’ is drawn out and saved as variable ‘Sp_Name’.
Line 147, the ‘mkdir’ function is used to create a new directory named ‘Sp_Name’.
Line 148, cd command changes the current directory of MATLAB to the newly created directory ‘Sp_Name’.
Now all the files which will be generated will be saved in this directory.
PART 4.1: PLOTTING SPECIFIC HEAT, ENTHALPY AND ENTROPY V/S TEMPERATURE OF THE SPECIES ENTERED BY USER:
Line 154, new figure window is opened.
Line 155, plotting the specific heat values of the cth species Cp (c, :) w.r.t temperature T, using the plot command. The linewidth is changed to 2usng the keyword (‘linewidth’, 2) and the color is kept as blue by giving keyword (‘color’, ‘b’).
Line 156, grid lines are turned on.
Line 157, label to x-axis is set to ‘Temperature[K]’ using the xlabel command.
Line 157, label to y-axis is set to ‘Specific Heat[J/K]’ using the ylabel command.
Line 158, title ‘Sp_Name’ is given using ‘title’ command and that name will be the title.
Sp_Name is the name of the array storing the name of the species in form of string datatype.
Line 160, the getframe(gcf) command will save the plot generated by the name ‘image’.
Line 161, ‘imwrite’ is an in-built MATLAB function. This function will write the image (image.cdata) to the file named (T vs Cp.png).
Line 162, saveas (1, ‘Specific Heat’, ‘jpg’) will save the figure in file named ‘Specific Heat’ such that 1 will be the handle of this figure, with ‘jpg’ as the format of the figure.
Line 166, new figure window is opened, named figure 2.
Line 167, plotting the Enthalpy values of the cth species H (c, :) w.r.t temperature T, using the plot command. The linewidth is changed to 2 using the keyword (‘linewidth’, 2) and the color is kept as blue by giving keyword (‘color’, ‘b’).
Line 168, grid lines are turned on.
Line 169, label to x-axis is set to ‘Temperature[K]’ using the xlabel command.
Line 170, label to y-axis is set to ‘Enthalpy[J/K]’ using the ylabel command.
Line 171, title ‘Sp_Name’ is given using ‘title’ command.
Sp_Name is the name of the array storing the name of the species in form of string datatype and that name will be the title.
Line 172, the getframe(gcf) command will save the plot generated in figure 2 by the name ‘image’.
Line 1673, ‘imwrite’ will write the image (image.cdata) to the file named (T vs Cp.png).
Line 174, saveas (2, ‘Enthalpy’, ‘jpg’) will save the figure in file named ‘Enthalpy’ such that 2 will be the handle of this figure, with ‘jpg’ as the format of the figure.
Line 178, new figure window is opened, named figure 3.
Line 179, plotting the Enthalpy values of the cth species S (c, :) w.r.t temperature T, using the plot command. The linewidth is changed to 2 using the keyword (‘linewidth’, 2) and the color is kept as blue by giving keyword (‘color’, ‘b’).
Line 180, grid lines are turned on.
Line 181, label to x-axis is set to ‘Temperature[K]’ using the xlabel command.
Line 182, label to y-axis is set to ‘Entropy[J/K]’ using the ylabel command.
Line 183, title ‘Sp_Name’ is given using ‘title’ command.
Sp_Name is the name of the array storing the name of the species in form of string datatype and that name will be the title.
Line 184, the getframe(gcf) command will save the plot generated in figure 2 by the name ‘image’.
Line 185, ‘imwrite’ will write the image (image.cdata) to the file named (T vs S.png).
Line 186, saveas (3, ‘Entropy’, ‘jpg’) will save the figure in file named ‘Entropy’ such that 3 will be the handle of this figure, with ‘jpg’ as the format of the figure.
Line 188, cd . .
This command will change the current directory for current directory ‘Sp_Name’ to its parent directory.
PART 4.2: CALCULATING MOLECULAR WEIGHT OF THE GIVEN SPECIES:
In this section the molecular weight of the species entered by the user is calculated. This is done with the help of a function designed to calculate the molecular weight.
Line 192, [M] = Mol_wt(Sp_Name);
On the left side of the above command [M] is the output of the function. M will store a single value, which is the molecular weight of the species.
On the right side is, the function name, ‘Mol_wt’, followed by the input ‘(Sp_Name)’.
‘Sp_Name’ is variable storing the name of the species in string characters which was calculated earlier in line 144.
The code of the function Mol_wt is given below:
function [M] = Mol_wt(Sp_Name)
Elements = ['O','H','C','N','A'];
Atomic_Mass = [15.999,1.008,12.011,14.007,39.948];
Mol_wt = 0;
for i = 1:length(Sp_Name)
for j = 1:length(Elements)
if strcmpi(Sp_Name(i),Elements(j))
Mol_wt = Mol_wt + Atomic_Mass(j);
z=j;
end
end
t = str2double(Sp_Name(i));
if t > 1
Mol_wt = Mol_wt + (Atomic_Mass(z)*(t-1));
end
end
M = Mol_wt;
end
Line 1, on left the output of this function (Molecular Weight, M) is present. On the right, function name (Mol_wt), using which it is called in the main program and followed by the inputs to the function (Sp_Name).
Line 2, a row vector, named, ‘Elements’ is created.
Elements = [‘O’, ‘H’, ‘C’, ‘N’, ‘A’];
This will generate a character row vector containing these 5 characters in same order.
Line 3, a row vector, named, ‘Atomic_Mass’ is created. It stores the atomic mass values for 5 elements written in the row vector ‘Elements’ in the same order i.e., 15.999 is atomic mass of the O atom, 1.008 is the atomic mass of the H atom and so on for the other atoms.
Line 5, Mol_wt = 0; this initializes the value of ‘Mol_wt’ to zero.
Line 6, a for loop is initiated, such that the loop counter ‘i’ varied from i = 1 to i = length(Sp_Name). The length(Sp_Name) depends upon the species name, so the loop counter i will have different last values for different species.
Line 7, another for loop is initiated with loop counter ‘j’ varying from j = 1 to j = length(Elements), which is equal to 5. So, for one iteration of the first loop, the second loop will have to run 5 times and then the parent loop will move to next iteration.
Line 8, an if-statement is checked in this line. The statement checks whether the ith element of the ‘Sp_Name’ array and the jth element in the ‘Elements’ array is the same or not. If both are the same then only the lines 9 and 10 will be executed, otherwise, the inner loop iteration will end and the j value will increase and the new iteration of the inner loop will begin.
If the if statement condition holds true then in line 9, the jth element of the ‘Atomic_Mass’ vector is added to the variable ‘Mol_wt’.
Line 10, a new variable ‘z’ is introduced and set equal to j.
After line 11 the inner loop counter j will increment and again the if-statement will be checked and the value of Mol_wt will change according to the result of the if-statement.
For j = 5, inner loop will finish.
Line 13, t = str2double(Sp_Name(i));
In this line, for different values of i the element from the Sp_Name row vector is first chosen and then converted from the characters datatype into double datatype. basically, this is done to find the number present in the name of the species.
Using 'str2double' command will give a numerical output only if the character supplied as input, is a numeric character. If the character entered to it is an alphabet the output will be ‘NaN’ meaning not a number. The output is saved in the variable named ‘t’.
Line 14, if-statement is checked whether t is greater than 1. If true, Mol_wt is modified based on the equation given in line 15.
Line 16, if-statement ends.
Line 17, the for loop ends.
Line 18, the final value of the Mol_wt is saved as the variable named M.
Line 19, function ends.
Line 196, the ‘fprintf’ command will display a message on-screen ‘The Species entered is: ’ followed by the name of the species (Sp_Name). %s tells that the entity displayed next to the message is a string datatype.
Line 197, ‘fprintf’ in this line prints the molecular weight of the species. %f tells that entity is a floating-point data i.e., a number having digits after decimal point also.
Line 199, the else command is the counter-part of the if-statement mentioned in line 144 which checked if the c value is not equal to zero and if c is not zero if-statement commands are executed. But when the c value is equal to 0 then all the commands will be skipped till line 199.
And line 201 the ‘disp’ command will display the message on the screen ‘Species Data Unavailable’.
The outputs from the code are:
1. Name of the species entered by the user is displayed on the screen.
2. Molecular Weight of the species.
3. Three plots showing the variation of Cp, H, and S w.r.t temperature (T)of the given
species.
All the outputs are attached below.