Project 1 - Parsing NASA thermodynamic data
%main program
clear all
close all
clc
% read file
f1=fopen('THERMO .dat','r');
g=fgetl(f1);
A=fgetl(f1);%this line
gt=strsplit(A,' ');
temp_min=str2num(gt{2});
temp_mid=str2num(gt{3});
temp_max=str2num(gt{4});
temp=linspace(temp_min,temp_max,500);
R=8.314;
g=fgetl(f1);
g=fgetl(f1);
g=fgetl(f1);
n=input(' enter element serial number from file=')
for i=1:4:(n-2)
species=fgetl(f1);
A=fgetl(f1);
B=fgetl(f1);
C=fgetl(f1);
end
%extract species from file
species=strsplit(species,' ');
species=(species{1});
a=findstr(A,'E');
%extract coefficient from file
a1=A(1:a(1)+3);
a1=str2num(a1)
a2=A(a(1)+4:a(2)+3);
a2=str2num(a2)
a3=A(a(2)+4:a(3)+3);
a3=str2num(a3)
a4=A(a(3)+4:a(4)+3);
a4=str2num(a4)
a5=A(a(4)+4:a(5)+3);
a5=str2num(a5)
b=findstr(B,'E');
a6=B(1:b(1)+3);
a6=str2num(a6)
a7=B(b(1)+4:b(2)+4);
a7=str2num(a7)
a8=B(b(2)+4:b(3)+3);
a8=str2num(a8)
a9=B(b(3)+4:b(4)+3);
a9=str2num(a9)
a10=B(b(4)+4:b(5)+3);
a10=str2num(a10)
c=findstr(C,'E');
a11=C(1:c(1)+3);
a11=str2num(a11)
a12=C(c(1)+4:c(2)+3);
a12=str2num(a12)
a13=C(c(2)+4:c(3)+3);
a13=str2num(a13)
a14=C(c(3)+4:c(4)+3);
a14=str2num(a14)
figure(1)%plot specific heat vs temp
cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,R,temp_mid);
plot(temp,cp);
title('specific heat plot')
xlabel('temprature in k')
ylabel('specific heat kj/kg-k')
figure(2)%plot enthalpy vs temp
H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,R,temp_mid);
plot(temp,H);
title('enthalpy plot')
xlabel('temprature in k')
ylabel('enthalpy kj/kg')
figure(3)%plot entropy vs temp
s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,temp,temp_mid);
plot(temp,s);
title('entropy plot')
xlabel('temprature in k')
ylabel('entropy kj/kg-k')
%molecular weight of the species
M=molecular_weight(species);
fprintf('molecular weight %s:',species);
fprintf('%d\n',M);
%molecular weight function defined
function M=molecular_weight(species)
atomic_name=['H','C','O','N','Ar'];%the species elemente selected is compared to these element
atomic_weigth=[1 12 16 14 40];%atomic weight of the above element
M=0;
for i=1:length(species)
for j=1:length(atomic_name)
if strcmpi(species(i),atomic_name(j))%atomic name and species are compared if there is any match then moved to next step
M=M+atomic_weigth(j);%comapered atomic name position is take to know he atomic weight corresponding to that element
position=j;
end
end
n = str2num(species(i))%if there are more than 1 same element present then moves to this part
if n>1
M=M+atomic_weigth(position)*(n-1);
end
end
end
%%entrop function defined
function s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,temp,temp_mid)
if temp<=temp_mid;%this staight forword formula based function
s=R*[(a8*log(temp))+(a9*temp)+(a10*((temp).^2)/2)+(a11*((temp).^3)/3)+(a12*((temp).^4)/4)+a14];
else
s=R*[(a1*log(temp))+(a2*temp)+(a3*((temp).^2)/2)+(a4*((temp).^3)/3)+(a5*((temp).^4)/4)+a7];
end
function H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,R,temp_mid)
%this straight forword formula based function
if temp<=temp_mid;
H=R*temp.*[a8+(a9*temp/2)+(a10*((temp).^2)/3)+(a11*((temp).^3)/4)+(a12*((temp).^4)/5)+(a13./temp)];
else
H=R*temp.*[a1+(a2*temp/2)+(a3*((temp).^2)/3)+(a4*((temp).^3)/4)+(a5*((temp).^4)/5)+(a6./temp)];
end
end
%specific heat function defined
function cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,R,temp_mid)
%this straight forword formula based function
if temp<=temp_mid
cp=R*[a8+(a9*temp)+(a10*(temp).^2)+(a11*(temp).^3)+(a12*(temp).^4)];
else
cp=R*[a1+(a2*temp)+(a3*(temp).^2)+(a4*(temp).^3)+(a5*(temp).^4)];
end
end










Aim:-
1.write a matlab function to extract 14 coefficent of a element using fgetl function and other amtlab command
2.write a function to calculate the specific heat,molecular weight,entropy and enthaly to find
3.plot the specific heat, entropy and enthalpy graph produce against temp
Objective of the priject :-
1.read the data from diffrent file using fopen command
2. use fgetl command to extract data from from diffrent file saved in same directory
3.write a function to calculate the specific heat,molecular weight,entropy and enthaly to find
body of the content:-
matlab program explaination
intially functions are defined
for specific heat, enthapy and entropy are direct formula based function but before that cheack the temp if it is less than mid temp then claculate uing
second half constant whear as for more temp use first hal constant
for malecular weight calculation function
intially atomic name and thier atomic numbers are defined
when speices is enterd the species is compared using string compare command with atomic number if they match the atomic weight is taken coresponding to that atomic name
if more than 1 same elemrnt are present the using oif command the other element moleculara weight is calculated
now comes to the main program
the file is opened using foen command which needs to be read
then using fgetl command line is extracted
fgetl extract the first line if we use again hen it will extract next line
here unwanted extracted lines are saved as g
where as required lines were saved accordingly
to extract 14 constan and species
wrote for loop from 1 to (n-2)/4
where n is the element serial no in that file
using below command species is extracted
species=strsplit(species,' ');
species=(species{1});
then for coefficient extraction below command is used
a=findstr(A,'E');
a1=A(1:a(1)+3);
a1=str2num(a1)
in above code initially using findstr command is used to locate the E string once the cosition of E is found using
this A(1:a(1)+3) command constant is exracted but ths is still a string using str2num command this is coverted to number
simmilar process done to extract all other coefficient
once all the coeffucients are found using speciic heat function spacific heat is is calculated
usingthe data present the graph is ploted
once all the coeffucients are found using entholpy function entholpy is is calculated
usingthe data present the graph is ploted
once all the coeffucients are found using entropy function entropy is is calculated
usingthe data present the graph is ploted and lastly by calling molecular weight function molecular weight is calculeted
conclusion:-
using matlab a file can be read and write