Challenge

Project 1 - Parsing NASA thermodynamic data

clear all
close all
clc

f1 = fopen('THERMO.dat','r');
fgetl(f1);

G_temp = str2num(fgetl(f1));

%Global temparture 

G_low = G_temp(1);
G_mid = G_temp(2);
G_high= G_temp(3);

for i = 1:3
    fgetl(f1);
end



R = 8.3145; %J/Mol*Km (units)

for j = 1:53
    
line = fgetl(f1);

A = strsplit(line,' ');
 
% species 
species =(A{1});


% Local_temparture
Low = str2num(A{end-3});
High = str2num(A{end-2});
Mid = str2num(A{end-1});

%co-efficients 
a_line = fgetl(f1);
a = findstr(a_line,'E'); %#ok<*FSTR>

A1 = a_line(1:a(1)+3);
A1 = str2num(A1); %#ok<*ST2NM>
A2 = a_line(a(1)+4:a(2)+3);
A2 = str2num(A2);
A3 = a_line(a(2)+4:a(3)+3);
A3 = str2num(A3);
A4 = a_line(a(3)+4:a(4)+3);
A4 = str2num(A4);
A5 = a_line(a(4)+4:a(5)+3);
A5 = str2num(A5);


b_line = fgetl(f1);
b = findstr(b_line,'E');

A6 = b_line(1:b(1)+3);
A6 = str2num(A6);
A7 = b_line(b(1)+4:b(2)+3);
A7 = str2num(A7);
A8 = b_line(b(2)+4:b(3)+3);
A8 = str2num(A8);
A9 = b_line(b(3)+4:b(4)+3);
A9 = str2num(A9);
A10 = b_line(b(4)+4:b(5)+3);
A10 = str2num(A10);

c_line = fgetl(f1);
c = findstr(c_line,'E');

A11 = c_line(1:c(1)+3);
A11 = str2num(A11);
A12 = c_line(c(1)+4:c(2)+3);
A12 = str2num(A12);
A13 = c_line(c(2)+4:c(3)+3);
A13 = str2num(A13);
A14 = c_line(c(3)+4:c(4)+3);
A14 = str2num(A14);

%Specific heat, Enthalpy, Entropy @ High temparture
T_H = linspace(Low,Mid,100);
  
cp = (A1+(A2.*T_H)+(A3.*T_H.^2)+(A4.*T_H.^3)+(A5.*T_H.^4))*R;
H = (A1+(A2.*T_H/2)+(A3.*(T_H.^2)/3)+(A4.*(T_H.^3)/4)+(A5.*(T_H.^4)/5)+A6.*T_H)*R;
S = (A1*log(T_H)+(A2.*T_H)+(A3.*(T_H.^2)/2)+(A4.*(T_H.^3)/3)+(A5.*(T_H.^4)/4)+A7)*R;
    
%Specific heat, Enthalpy, Entropy @ Low temparture
T_L = linspace(Mid,High,100);

cp1 = R*(A8+(A9.*T_L)+(A10.*T_L.^2)+(A11.*T_L.^3)+(A12.*T_L.^4));
H1 = R*(A8+(A9.*T_L/2)+(A10.*(T_L.^2)/3)+(A11.*(T_L.^3)/4)+(A12.*(T_L.^4)/5)+A13.*T_L);
S1 = R*(A8*log(T_L)+(A9.*T_L)+(A10.*(T_L.^2)/2)+(A11.*(T_L.^3)/3)+(A12.*(T_L.^4)/4)+A14);

% To create a folder
str = sprintf('D:\\virat\\Nasa thermodynamics data\\%s',species);
mkdir(str);
cd(str);

figure()
plot(T_H,cp)
hold on 
plot(T_L,cp1)
hold off
xlabel('Temparature')
ylabel('Specific heat') 
legend('High temparture','low Temparature')
saveas (gcf,'Specific heat')

figure()
plot(T_H,H)
hold on
plot(T_L,H1)
hold off
xlabel('Temparature')
ylabel('Enthalpy') 
legend('High temparture','low Temparature')
saveas(gcf,'Enthalpy')

figure()
plot(T_H,S)
hold on 
plot(T_L,S1)
hold off
xlabel('Temparature')
ylabel('Entropy')
legend('High temparture','low Temparature')
saveas(gcf,'Entropy')


�lculating Molecular weight
elements = ['H','C','N','O','AR','(S)'];
Atomic_weight = [1,12,14,16,40,0,0,0,0];

molecular_wt = 0;
for k = 1:length(species)
  [num,status] = str2num(species(k));
  if status == 0
    ind = find(elements == species(k));
    molecular_wt(k) = Atomic_weight(ind);
  else 
  molecular_wt(k-1) = num*molecular_wt(k-1);
  end
end
t_M=sum(molecular_wt);
K = sprintf('Molecular weight of %s is %f',species,t_M);
disp(K)
end


 CODE EXPLINATION

Starting with clear all, close all and clc 
By using fopen command we are opening the file of THERMO.dat and 'r' repersents to read the file
fgetl: is used to read the line in the file of which we have given and for every fgetl it will be reading the next line in the file So the fgetl is used to read the line in the file 

By using fgetl we will be reading the first line of the file which is THERMO and for next fgetl we ill be getting the second line which is the Global_temperature and we are assinging to the G_temp 
G_low = G_temp(1)
G_mid = G_temp(2)
G_high= G_temp(3)
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.