All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
This MATLAB script is for plotting cp,H, and S vs temperature provided NASA Thermodynamics data for the different species first run \'species_info.m\' which will ask user to enter the species name and it will give the graph and other related information. if their is no data availabel then it will run…
nakum mohan
updated on 13 Jul 2019
This MATLAB script is for plotting cp,H, and S vs temperature provided NASA Thermodynamics data for the different species
first run \'species_info.m\' which will ask user to enter the species name and it will give the graph and other related information. if their is no data availabel then it will run \'thermo.m\' which will ask user to enter the file name which contains the NASA thermodynamics data which is \'THERMO.dat\'.
if file name is wrong then it will show the error. after providing the file name it will creat all the graphs for all the species and other information. It will make a directory in current working directory for different species and save all the particular graph as a image file, So explaination for the script is given below
firstwedeclare the univarsal constant R=8.314 and then we will create file id names \'fid\' which will take input from the user to enter the file name which contains NASA thermodynamics data and it creats file id.for that we have used \'fopen\' and \'input \' commands In case file is not found or invalid name it will show the error.in that case fid will be equal to -1 now we use \'fgetl\' command to skip the first line which is title and then we save second line in \'line\' which contains the global temps as min,mid and max seperated by space(\' \'). so we use \'strsplit\' command to split the string and save in \'a\' and then save global temps by converting from string to number by command \'str2num\'.Here we have used {} because it is cell array Now we use for loop to skip next 3 lines which is discription, we just use fgetl command now we create another for loop which scans other remaining lines which contains species name and local temperature range data till end. to do that first we load line and we compare it with \'END\' ,which is written at the end of the data file user have entered. for that we have used \'strcmp\' command which compares two strings. By using \'if\' conditon and if \'END\' is found it breaks the for loop. Now we split next line,here first cell is name of the species so we save it in the \'speciesname\' as character array by using \'char\' command. name array saves a string which we will use later for indexing the names , which is again a cell array. At the end of this line it contains localtemp data but in that, last cell is just indexing ,second from last cell is midtemp,third from last cell is high temp and fourth last is low temp , all these are local temp and we have to make plot according to these localtemps. For that length(a) is last cell which is just line no. so we used used \'len)-1\' cell which contains mid temp and so on for min and max temp which is conveted to number. then we have created temperature range by using local temp ranges using \'linspace\' command Now next line contains 5 constants for the higher temp to extract that on base fo scientific notation we have to find \'E\' from that line so we have used \'srtfind\' command to find \'E\' and next three digit are also included in constant. after first \'E\' and four digit the second constats starts and we have find second \'E\' and then continue for five constants. For first constant which start from first character and ends at the location of first \'E\' + 3 characters. For second constant it starts form the location of first \'E\' + 4 characters and ends at the location of the second \'E\' + 3 characters. and so on for the other character also.second line contains two high temnp constats and five low temp constats we find it in same way in next line. we have hc1,hc2,hc3,hc4,hc5,hc6,hc7 for the high temp coefficient and lc1,lc2,lc3,lc4,lc5,lc6,lc7 for the low temp coefficient finding cp,H and S we have different formula for low and high temp and for each temp point we calculate these properties formula for both are given below . ---for low temp---. cp = R*(lc1 + lc2*T + lc3*T^2 + lc4*T^3 + lc5*T^4);
H = R*T*(lc1 + lc2*T/2 + lc3*(T^2)/3 + lc4*(T^3)/4 + (lc5*T^4)/5 + lc6/T);
S= R*(lc1*log(T) +lc2*T + (lc3*T^2)/2 + (lc4*T^3)/3 + (lc5*T^4)/4 + lc7); -
-- for high temp---
cp = R*(hc1 + hc2*T + hc3*T^2 + hc4*T^3 + hc5*T^4); H = R*T*(hc1 + hc2*T/2 + hc3*(T^2)/3 + hc4*(T^3)/4 + (hc5*T^4)/5 + hc6/T);
S= R*(hc1*log(T) + hc2*T + (hc3*T^2)/2 + (hc4*T^3)/3 + (hc5*T^4)/4 + hc7);
Here, hc stands for high temp coeficient and lc stands for low temp coeff. Now we calculate the molarmass for each species by using \'molarmass\' function and print it\'s value in command window. for that explaination is given in that part Now we save current directoy path in \'currentdir\' by using \'pwd\' command which gives the path of the current directory. Then we make new directory named by the species name. And change the directory to that new directory by using \'cd\' command. Now we create the new figure and plot temp vs cp data for that particuler species then provide speciefic x and y label fot that and then title with species name and it\'s molarmass shown at the title using \'strcat\' command Now figure2 is created for plotting the values of temp vs enthalpy(H) with proper name and title and figure3 for temp vs entropy(S) Now we save these 3 figure by using \'saveas\' command as a *.png file with proper naming and then we go back to the working directory by using \'cd\' command. Here is the end of for loop we close all the figure and file. and we make firstrun = 1 so that once the thermo.m has run successfully there is no need to run further more and data is saves in the workspace
% To directly see the species info run the code \'species_info.m\'
clear all
close all
clc
% Declaring the constat R=8.314
R = 8.31;
% Asking user to enter to file name
fid = fopen(input(\'Enter the NASA thermodynamics file name with extension-\',\'s\'));
% Incase user enter invalid filename it shows error
if fid == -1
error(\'enter the valid file name\')
end
% First line is comment and second line contains global temps
fgetl(fid);
line = fgetl(fid);
a = strsplit(line,\' \');
% aquring the global temp from the line
global_min_temp = str2num(a{2});
global_mid_temp = str2num(a{3});
global_max_temp = str2num(a{4});
% we have to skip 3 lines which is discription of the file
for i=1:3
line = fgetl(fid);
end
% Scannig whole file till end to extract all the species
for i = 1:inf
%1st species from here
line = fgetl(fid);
% If file scannig ends then we break the for loop
if strcmp(line,\'END\')== 1
break;
end
% extracting the species name and local temps
a = strsplit(line,\' \');
name(i) = cellstr(a(1));
speciesname = char(name(i));
locallowtemp = str2num(char(a(length(a)-3)));
localhightemp = str2num(char(a(length(a)-2)));
localmidtemp = str2num(char(a(length(a)-1)));
% Temperature range
T = linspace(locallowtemp,localhightemp,500);
% Extracting the high and low and high temp co-efficient by finding
% \'E\'from next three lines
line = fgetl(fid);
a = strfind(line,\'E\');
hc1 = str2num(line(1:a(1)+3));
hc2 = str2num(line(a(1)+4:a(2)+3));
hc3 = str2num(line(a(2)+4:a(3)+3));
hc4 = str2num(line(a(3)+4:a(4)+3));
hc5 = str2num(line(a(4)+4:a(5)+3));
line = fgetl(fid);
a = strfind(line,\'E\');
hc6 = str2num(line(1:a(1)+3));
hc7 = str2num(line(a(1)+4:a(2)+3));
lc1 = str2num(line(a(2)+4:a(3)+3)); %#ok<*ST2NM>
lc2 = str2num(line(a(3)+4:a(4)+3));
lc3 = str2num(line(a(4)+4:a(5)+3));
line = fgetl(fid);
a = strfind(line,\'E\');
lc4 = str2num(line(1:a(1)+3));
lc5 = str2num(line(a(1)+4:a(2)+3));
lc6 = str2num(line(a(2)+4:a(3)+3));
lc7 = str2num(line(a(3)+4:a(4)+3));
% For each temp value we find specific heat,enthalpy and entropy for
% different lower and higher temps on basic of middle temp
for j = 1:length(T)
% for lower temp
if T(j)
thismatlab file will ask user to enter the species name for which he/she want to see the graphs and shows the graph for the given species. for that we have to have a data for the all species, so we have not used the clear all command. Because we will use thermo.m file to calculate all the properties required for the graph. So if user is firt time running this program then we have no variable exist named \'firstrun\' so it will run the thermo.m file first time and load all the data and creates folder and graphs and shows moleculer weigth in the command window and now we have firstrun = 1. Details for the thermo.m file is also given read that section also Now user have already run the program and he/she just wants to see as speciefic graph for species then he/she will enter the name of the species.from now variable first run exist and is equal to 1. next we have used input command to take name of the species from the user and we convert them into upper case using command \'upper()\',so our program will be case INSETIVE Now we will compare this string with all the name we have available from thermo.m file and workspace. for that we have used for loop which goes 1 to length of the name and compare each string if name is found then we plot all the graph from the data we have availabel in the work space.if species name is not found then it will show the error to enter the valid name. if there is no data available then we will run thermo.m file to draw all the graphs.
close all
clc
% if user had run the thermo.m file already then no need to run that again
if exist(\'firstrun\')
% Ask user to enter species name
k = input(\'Enter the name of the specimen for which you want to see detail-\',\'s\');
k = upper(k);
% compare enterd name and created names of species to providing graph
for l = 1:length(name)
if strcmp(name(l),k)==1
figure(1);
plot(T,cp(l,:));
xlabel(\'Temp in (K)\');
ylabel(\'specific heat(cp) in (kj)\');
title(strcat(\'specific heat(cp) vs temperature for -\',\' \' , name(l)));
figure(2);
plot(T,H(l,:));
xlabel(\'Temp in (K)\');
ylabel(\'Enthalpy(H) in (kj/mol)\');
title(strcat(\'Enthalpy(H) vs temperature for -\',\' \' , name(l)));
figure(3);
plot(T,S(l,:));
xlabel(\'Temp in (K)\');
ylabel(\'entropy(S) in (kj/mol k)\');
title(strcat(\'entropy(S) vs temperature for -\',\' \' , name(l)));
break;
elseif l==length(name)
% in case user enters invalid name it shows the error
error(\'please enter valid species name\')
end
end
% In case user have cleared the data
else
thermo
end
this function will take a species name as input and gives the molar mass of the species. now here we have basic 5 atom in the specieses. so we have declared them in a character array named \'s\' and we know their molarmass. so we have declared their masses in the \'weight\' arrary. we declare initial molarmass \'m\' as zero. Now species may contain no. also which says that this atom is more than one time., so we have detect that no. also. So we have to remember the character before that number so we have declared variable \'k\' as one. Now we will compare the each character of the name and our basic molecule names if it is found then we add it\'s weight with it\'s corresponding weight from the weight array. Now their is argon molecule which have two character name so we will break loop directly if we found \'A\'. Some species name have their optional name contained in \'( )\' so we have to ignore them and we break loop for that also. if match is found then we add corresponding weight , now next character may contain number so we have to remember currnet character index so we have declared k = j here. now if we have compared each character and we have not found match then definetly it is number so we convert it into no. from string and then add to weight. Now initially we have added that weight once already when we have found that character so now we have to multiply with (that no. - 1) times with weight corresponding to character that we have rememberd before and here is the end the for loop we go ahead to find next character and continue.
function [ w ] = molarmass( name )
% basic molar mass are given first
s = {\'H\',\'C\',\'N\',\'O\',\'A\'};
% and their weight
weight = [1,12,14,16,40];
% declaring initial weight to be zero
w = 0;
% we have to remember character before number :used later
k = 1;
for i = 1:length(name)
% for name that includes other data and \'A\' or \'(\'
if(name(i) == \'A\' )
w = 40;
break;
elseif(name(i) == \'(\')
break;
end
% compare each name with basic names and adding weight
for j = 1:length(s)
if strcmp(name(i),s(j))
w = w + weight(j);
k = j;
break;
% if there is a no. included then we have to multiply it
elseif j == length(s)
w = w + weight(k) * (str2num(name(i)) - 1);
end
end
end
end
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Airfoil(NACA0017) flow analysis in solidwork
OBJECTIVE Here we will perform a flow analysis on NACA0017 airfoil with different angle of attack and compare the result of drag force and lift force for each case to model the airfoil we need x,y,z points in solidwork and link to this file can be found in link given below http://airfoiltools.com/airfoil/naca4digit …
02 Dec 2019 05:28 AM IST
Flow over cylinder with different reynolds no. in solidwork
OBJECTIVEHere we will disxuss the about flow over the cylinder with different reynold no. PROCEDURE -first of all we have to make the model of the cylindercylinder have a diameter of 4 mm and length of 50 mm .after saving the model we have to go to flow sumulation and using wizard we will give different parameters. -Here…
26 Nov 2019 09:44 PM IST
Pipe flow simulation in solidwork
OBJECTIVE Here we are going to simulate the pipe flow in solidworks with flow simulation. We will discuss it for different reynold number 100 , 1000 and 10000 for water. We will make line probe at 85 , 90 and 95 % of its length. at each line probe we are going to make a cutplot of the section and graph of the velocity…
23 Nov 2019 03:58 AM IST
frequency analysis of rotating shaft
AIM to perform the frequency analysis on the rotating shaft and find the critical frequencies Model dimension is given below for shaft design (all dimension are given in meter) based on the given sketch we can make a model of the shaft by revovlve feature in solidwork material properties can be shown in the…
11 Nov 2019 05:19 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.