Challenge

Project 1 - Parsing NASA thermodynamic data

clear ()
close all
clc
%file reading

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

l1=fgetl(f1);
l2=fgetl(f1);
for l=3:5
    %basically we have to use for loop three times (l=1:3) for 
    % reading next three header line. 
    fgetl(f1)
end
for i=1:53% this is done 53 times because there are 53 species present in file.
%line 6 operation
l6=fgetl(f1);

B=strsplit(l6,' ');%strsplit means string split

species=B(1);%it is a molecule species

%we will seprate here local temprature ranges

D=strfind(l6,'.');                   %strfind is used to find string

temp_range= l6((D(1)-3):(D(1)+23));

temprature=strsplit(temp_range,' ');

local_low_temp= str2double(temprature{1}); 
%{} bracket is necessary herebecause this is the cell matrix,which mean it
%stores the value with specific identity.

temp_1=local_low_temp;

local_highest_temp= str2double(temprature{2});

temp_2=local_highest_temp;

local_mid_temp= str2double(temprature{3});
temp_3=local_mid_temp;

%line 7 operation
l7=fgetl(f1);
F=strfind(l7,'E');

first_coefficient_at_high_temp= l7(1:F(1)+3);


fcaht= str2double(first_coefficient_at_high_temp);%str2num could also be used here 

% i have used str2double istead of str2num


a1 =fcaht;

second_coefficient_at_high_temp= l7(F(1)+4:F(2)+3);
scaht= str2double(second_coefficient_at_high_temp);%scaht mean 
                                                   %second_coefficient_at_high_temp.
a2=scaht;

third_coefficient_at_high_temp= l7(F(2)+4:F(3)+3);
tcaht= str2double(third_coefficient_at_high_temp);%tcaht mean 
                                                  %third_coefficient_at_high_temp.
a3=tcaht;

fourth_coefficient_at_high_temp= l7(F(3)+4:F(4)+3);fourth_caht= str2double(fourth_coefficient_at_high_temp);%fourth_caht mean 
                                            %fourth_coefficient_at_high_temp.
a4= fourth_caht;

fifth_coefficient_at_high_temp= l7(F(4)+4:F(5)+3);
fif_caht= str2double(fifth_coefficient_at_high_temp);
a5= fif_caht;

%line 8 operation
l8=fgetl(f1);
G=strfind(l8,'E');

sixth_coefficient_at_high_temp= l8(1:G(1)+3); str2double(sixth_coefficient_at_high_temp);
a6= sixth_caht;

seventh_coefficient_at_high_temp= l8(G(1)+4:G(2)+3);seventh_caht=str2double(seventh_coefficient_at_high_temp);%seventh_caht mean seventh_coefficient_at_high_temp
a7= seventh_caht;

eighth_coefficient_at_low_temp= l8(G(2)+4:G(3)+3);ecalt= str2double(eighth_coefficient_at_low_temp);%ecalt mean eighth_coefficient_at_low_temp.
b1= ecalt;

ninth_coefficient_at_low_temp= l8(G(3)+4:G(4)+3);ncalt=str2double(ninth_coefficient_at_low_temp);%ncalt mean ninth_coefficient_at_low_temp.
b2= ncalt;

tenth_coefficient_at_low_temp= l8(G(4)+4:G(5)+3);tcalt=str2double(tenth_coefficient_at_low_temp);%tcalt mean tenthth_coefficient_at_low_temp.
b3= tcalt;

%line 9 operation
l9=fgetl(f1);
K=strfind(l9,'E');
eleventh_coefficient_at_low_temp= l9(1:K(1)+3);
eleventh_calt=str2double(eleventh_coefficient_at_low_temp);
%eleventh_calt mean eleventh_coefficient_at_low_temp.

b4= eleventh_calt;

twelth_coefficient_at_low_temp= l9(K(1)+4:K(2)+3);
twelth_calt=str2double(twelth_coefficient_at_low_temp);
%twelth_calt mean twelth_coefficient_at_low_temp.

b5=twelth_calt;

thirteenth_coefficient_at_low_temp= l9(K(2)+4:K(3)+3);
thirteenth_calt=str2double(thirteenth_coefficient_at_low_temp);
%thirteenth_calt mean thirteenth_coefficient_at_low_temp.

b6=thirteenth_calt;

fourteenth_coefficient_at_low_temp= l9(K(3)+4:K(4)+3);
fourteenth_calt=str2double(fourteenth_coefficient_at_low_temp);
%fourteenth_calt mean fourteenth_coefficient_at_low_temp.

b7= fourteenth_calt;
 

molecule_name=char(species);

%making directory
mkdir(['C:\Users\Asus\OneDrive\Desktop\MATLAB\scilab',molecule_name])

%solving the specific heat(Cp),enthalpy(H) and entropy(S) equations for coeff at local high temprature and local low temprature

temp=linspace(temp_1,temp_2,20);
%selecting a range between low local temprature to high local temprature

R=8.314;  %the gas constant. 
K=1:length(temp);
for j=1:length(K)
if temp(K)<=1000
%equations at low temprature.
Cp= R*(b1+ b2*(temp(K)) + b3*(temp(K)).^2 + b4*(temp(K)).^3 + b5*(temp(K)).^4);

H= (R*temp(K)).*(b1+ b2*(temp(K)./2) + b3*((temp(K)).^2/3) + b4*((temp(K)).^3/4) + b5*((temp(K)).^4)/5 + b6./temp(K));

S= R*(b1*log(temp(K))+ b2*(temp(K)) + b3*((temp(K)).^2/2) + b4*((temp(K)).^3/3) + b5*((temp(K)).^4)/4 + b7);
else
%equations at high temprature.

Cp= R*(a1+ a2*(temp(K)) + a3*(temp(K)).^2 + a4*(temp(K)).^3 + a5*(temp(K)).^4);

H= (R*temp(K)).*(a1+ a2*(temp(K)/2) + a3*(((temp(K)).^2)/3) + a4*(((temp(K)).^3)/4) + a5*(((temp(K)).^4)/5) + a6./temp(K));

S= R*(a1*log(temp(K))+ a2*(temp(K)) + a3*((temp(K)).^2/2) + a4*((temp(K)).^3/3) + a5*((temp(K)).^4)/4 + a7);

end
end

figure(1);
plot(temp,H,'-*','linewidth',1,'color','g');
title(molecule_name);
xlabel('temprature');
ylabel('Enthalpy');

figure(2);
plot(temp,Cp,'-*','linewidth',1,'color','r');
title(molecule_name);
xlabel('temprature');
ylabel('Specific heat');


figure(3);
plot(temp,S,'-*','linewidth',1,'color','b');
title(molecule_name);
xlabel('temprature');
ylabel('Entropy');

saveas(1,'Enthalpy.jpg');
saveas(2,'Specific heat.jpg');
saveas(3,'Entropy.jpg');


weight(i)=molecular_mass(molecule_name);
end
fclose(f1);

function

function [M]=molecular_mass(molecule_name);

fprintf('Molecular weight of ');
fprintf(molecule_name);
fprintf(' : ');
atoms=['h','C','N','O','R'];
%R is for argon molecule 

atom_weight=[1 12 14 16 40];
M=0;
for i=1:length(molecule_name);
  
        for j=1:length(atoms)
        if strcmpi(molecule_name(i),atoms(j));
            %strcmp means string comparision .it is used for checking that both elements are same or not. and by use strcmpi here,it take doesn't matter about elements are capital letter or small Example'h' =='H'means case insensitive condition.
            
           
            M=M+atom_weight(j);
           position=j;
          
       
        end
        end
        n= str2double(molecule_name(i));
        %this all proces is doing for a number multiplier in molecule name.so the value of n will be come same as a number value present in that string name  
       
        if n > 1
            M=M+atom_weight(position)*(n-1);
            %here we are taking (n-1) in multiply instead of n because we are input second time coding  M=M+atom_weight.so by this two times mass add so to over come this we are reducing n to n-1 time
        


        end

end
fprintf('%d',M);%d is for digit
disp(' ')
end

out put for N2 O2 and CO2

coding explained:-

-Initially we will opening the given THERMO.dat file using fopen command and then we will extract all the information provided in the file using fgetl command.

-First we we will get the global temperature by using strslplit command and then we skip some lines using the fget command.

-Now we make use of for loop command to get all temperature and coefficients that are present in our file.

-For getting temperature and coefficients we use strfind command inorder to input the accurate spot and specifying it so that we can get all the data.

-Now the datas which are obtanied from the file will be of string format which we need to convert into numbers by using str2double command.and by this we will get the temperature and coefficient of all the species

-Now the specific heat,enthalpy ,entropy function is calculated and gets plotted.

-The three figures are plotted using plot command and each species name is displayed using title command.And further it will named using xlabel and ylabel command. we will save the plots as our images using save command.

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.