Challenge

Project 1 - Parsing NASA thermodynamic data

%Nasa thermodynamic data

%main function

clear all
close all
clc %erases all the precious loaded data in thhe command window
% n=0
% while~feof(f1)
% L=fgetl(f1);
% ls=strsplit(L,' ')
% a=length(ls);
% if strcmpi(ls{a},'1');
% n=n+1;
% end
% disp(n)
% end % THE ABOVE commented part was to determine no. of species in the
% file as each 1st line, 1st char. determines the species name.. as per
% discussion with your member he told to commented the following item.
f1=fopen('THERMO.dat','r');%again opening the thermo.dat file
for i=1:5 %as 1st 5 lines are not That imp
fgetl(f1)%getting 1st five line in a for loop
end
% note a1 to a7 are high temp coeff, and a8 to a14 low temp coeff.
for i=1:53 %there are 53 species, therefore iterating it 53 times
header=fgetl(f1)%header line of species
hd=strsplit(header,' ')%the 1st line is the secies line marked as header
spname=hd{1} %as the 1st char. represents the species name,allocating the same to spname
%localtemperatures
lt=str2double(hd(end-3))
ht=str2double(hd(end-2))
mt=str2double(hd(end-1))%in the 1st line, high ,medium ,and low temp are present in 5,6 and 7th position
%coeff
line1=fgetl(f1); %there after extraction of each line is executed individually
l1=findstr(line1,'E');%as 'E' is common in each line we try to find out 'E' in each of the lines)
L=strsplit(line1,' ');%the extracted line is in single cell, therefore we seperate them into different cells characters for easier identification
a1=str2double(line1(1:l1(1)+3));%assigning the 1st five variables of 1st line after species as a1 to a5
a2=str2double(line1(l1(1)+4:l1(2)+3));
a3=str2double(line1(l1(2)+4:l1(3)+3));
a4=str2double(line1(l1(3)+4:l1(4)+3));
a5=str2double(line1(l1(4)+4:l1(5)+3));

line2=fgetl(f1);
l2=findstr(line2,'E');%same as above
L2=strsplit(line2,' ');%the extracted line is in single cell, therefore we seperate them into different cells characters for easier identification
a6=str2double(line2(1:l2(1)+3));%assigning the 1st five variables of 1st line after species as a6 to a10
a7=str2double(line2(l2(1)+4:l2(2)+3));%+4 represents the 4th character after E, +3 represents 3 characters after E
a8=str2double(line2(l2(2)+4:l2(3)+3));
a9=str2double(line2(l2(3)+4:l2(4)+3));
a10=str2double(line2(l2(4)+4:l2(5)+3));

line3=fgetl(f1);
l3=findstr(line3,'E');
L3=strsplit(line3,' ');%same as above
a11=str2double(line3(1:l3(1)+3));
a12=str2double(line3(l3(1)+4:l3(2)+3));
a13=str2double(line3(l3(2)+4:l3(3)+3));
a14=str2double(line3(l3(3)+4:l3(4)+3));
R=8.314;%universal gas constant
trange=linspace(lt,ht,mt);%spacing the temp values in equal intervals i.e medium temp
if trange<mt
cp=R*(a8+a9.*trange +a10.*trange.^2 +a11.*trange.^3 +a12.*trange.^4) ;%calc cp for low tem range
else
cp=R*(a1+a2.*trange +a3.*trange.^2 +a4.*trange.^3 +a5.*trange.^4) ;%calc cp for high tem range
end

if trange<mt
h= R*trange.*(a8+a9.*trange/2+(a10.*trange.^2)/3+(a11.*trange.^3)/4+(a12.*trange.^4)/5+(a13.*trange.^5)/6);%calc enthalpy for low tem range
else
h=R*trange.*(a1+a2.*trange/2+(a3.*trange.^2)/3+(a4.*trange.^3)/4+(a5.*trange.^4)/5+(a6.*trange.^5)/6);%calc enthalpy for high tem range
end

if trange<mt
S=R*trange.*(a8.*log(trange)+a9.*trange+(a10.*trange.^2)/2+(a11.*trange.^3)/3+(a12.*trange.^4)/4 +a14);%calc entropy for low tem range
else
S=R*trange.*(a1.*log(trange)+a2.*trange+(a3.*trange.^2)/2+(a4.*trange.^3)/3+(a5.*trange.^4)/4 +a7);%calc entropy for high tem range
end
%directory
% cd('C:\Users\Oswald\Desktop\skill lync codes\week 5 nasa')% directory
%other then matlab
mkdir(spname)%making folder as per species name
cd(spname) %entering into the same folder
% startingFolder=('C:\Users\Oswald\Desktop\skill lync codes\week 5 nasa')

figure(1)%creating a figure

plot(trange,cp)%plotting graph
grid on
xlabel('Temperature')%labaling x and y axis
ylabel('Specific heat')
legend('cp')
title(spname)%title of graph will be same as spname

saveas(1,'specific heat','jpg')%saving the figure 1 as specific heat in jpg format

figure(2)%creating another figure window

plot(trange,h)%plotting
grid on
xlabel('Temperature')
ylabel('Enthalpy')
legend('H')
title(spname)

saveas(2,'enthalpy','jpg')%same as above


figure(3)

plot(trange,S)
grid on
xlabel('Temperature')
ylabel('Entropy')
legend('S')
title(spname)

saveas(3,'entropy','jpg')

cd ..% closing the directory

[weight(i)]=molwt(spname)% function call

end
fclose(f1)% closing the file opene at start of program

%function call

function [wt]=molwt(spname)
atoms=['H','C','N','O','A'];%the number of atoms present in each of the combination are sereted in each column
mass=[1,12,14,16,40];%atomic masses mentioned in the same order as in atoms are arranged
weight=0;%initializing the weight to zero
y=0;%an random variable to store
for i=1:length(spname)%calculating the length of species tofind molecular weight
for j=1:length(mass)%to compare with atoms present
if strcmp(spname(i),atoms(j))%compairing each char in spname with atoms if matched then the mass will be added
weight=weight+mass(j);
y=j;%the location is being stored
end
end
n=str2num(spname(i));%converting the char into no. if the species contains more no. of atoms of same tye
if n>1
weight=weight+(n-1)*mass(y);%if atoms are more then one mass of(j)th location wil be added
end
end
disp('molecular Weight of')
disp(spname)%displaying the weight of each species
[wt]=weight %disp. weight



%logic description
% 1.extracting thermo.dat file from the req folder
% 2.as 1st 5 line are irrelevant extracting it using for loop
% 3.the 6th line is the species header extracting the particular line using fgetl command
% 4. extractng the species name and local low medium and high tem values with strsplit command
% 5.using for loop to extract the data for given species,the 3 lines after species represents the coeff i.e a1 t0 a7 high temp coeff and a8 to a14 low temp coeff.
% 6.for each of species the loop will be executed
% 7.for the atoms present the masses of respective atoms should be present in the similar way.
% 8.for the 1st for loop in the fuction , it 1stly calculates the length and then compare of individual char is done with atoms with the 2nd for loop,
% if both the characters are matched the masses are added accordingly, if it contains any numeric valuein the character
% it is converted to numeric value with help of str2num and the again in if statement the masses are added accordingly.

%erroes faced

1.unable to plot graph:

graph was not getting plotted , the isuue was solved with the help of your project guide

2.multiplication error:

since the values are scalar .* , the dot was missing

3.due to same save as value , the entopy graph was overritten by enthalpy

4.was not able to calculate molecular weight having more then 1 no. of same atom, isuue solved by your end.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.