Challenge

Project 1 - Parsing NASA thermodynamic data

Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. You will be reading this file

Report:

1) Firstly, the NASA Thermodynamic Data 'Thermo.dat' file was read by using fopen command.

2) Then to navigate through the data fget command was used.

3) The temperature line '300 1000 5000' was stored as a single string in a variable. To split the string into 3 strings strsplit command was used. To convert the string values into numerical values str2num command was used. The three temperature values min, mid and max were then stored in seperate variables.

4) After that the three comment lines were skipped by using fgetl command.

5) The 14 coefficients for each species were calculated by using for loop. Each species contains 3 lines in which 14 coefficients are present.

6)Based upon the 14 co-efficients and the value of R the values of Specific heat, Enthalpy and Entropy were calculated for each species by using the following formulaes

7) The graphs were plotted of Temperature vs Entropy, Temperature vs Enthalpy and Temperature vs Specific heat and saved in the seperate created folders for each species.

8) Finally, the values of molecular weights were calculated for each species. For this pupose a different function was created and then called in the parsing code to display the molecular weight in command window.

clear all
close all
clc
%oponing file
f1 = fopen('THERMO.dat','r')
r = 8.314;

heading = fgetl(f1)

global_temp = fgetl(f1)
x = strsplit(global_temp,' ')

%converd starting three values in str to num

global_low_temp = str2num(x{2})
global_mid_temp = str2num(x{3})
global_high_temp = str2num(x{4})

%ignor three lines
fgetl(f1)
fgetl(f1)
fgetl(f1)

mkdir('c:UsershpDesktopmatlab_programNew folderspecies ')

for i = 1:53
    line1 = fgetl(f1); % where line one sellect gor loop 
    sp_name = line1(1:6); % where chose L 1/90
    local_temp1 = line1(49:55); % where chose 200.000 between 49 to 55
    ltemp = str2num(local_temp1)
    
    local_temp3 = line1(58:65); %where chose 3500.000 between 58 to 65
    htemp = str2num(local_temp3)
    
    local_temp2 = line1(68:75); %where chose 1000.000 between 68 to 75
    mtemp = str2num(local_temp2)
    
    line2 = fgetl(f1);
    line3 = fgetl(f1);
    line4 = fgetl(f1);
    
    a = findstr(line2,'E');
    b = findstr(line3,'E');
    c = findstr(line4,'E');
    
    a1 = str2num(line2(1:a(1)+3));
    a2 = str2num(line2(a(1)+4:a(2)+3));
    a3 = str2num(line2(a(2)+4:a(3)+3));
    a4 = str2num(line2(a(3)+4:a(4)+3));
    a5 = str2num(line2(a(4)+4:a(5)+3));
    
    a6 = str2num(line3(1:b(1)+3));
    a7 = str2num(line3(b(1)+4:b(2)+3));
    a8 = str2num(line3(b(2)+4:b(3)+3));
    a9 = str2num(line3(b(3)+4:b(4)+3));
    a10 = str2num(line3(b(4)+4:b(5)+3));
    
    a11 = str2num(line4(1:c(1)+3));
    a12 = str2num(line4(c(1)+4:c(2)+3));
    a13 = str2num(line4(c(2)+4:c(3)+3));
    a14 = str2num(line4(c(3)+4:c(4)+3));
    
    
    t = linspace(ltemp,htemp,500);
    
    %evelvate the vlue of cp , h, s
    
    for j = 1:500
        if t>=mtemp
            cp = (a1+a2.*t+(a3.*t.^2)+(a4.*t.^3)+(a5.*t.^4)).*r;
            h = (a1+(a2.*t./2)+(a3.*(t.^2)./3)+(a4.*(t.^3)./4)+(a5.*(t.^4)./5)+(a6./t)).*r.*t;
            s = (a1*log(t)+a2.*t+(a3.*(t.^2)./2)+(a4.*(t.^3)./3)+(a5.*(t.^4)./4)+a7).*r;
        else
            cp = (a8+a9.*t+(a10.*t.^2)+(a11.*t.^3)+(a12.*t.^4)).*r;
            h = (a8+(a9.*t./2)+(a10.*(t.^2)./3)+(a11.*(t.^3)./4)+(a12.*(t.^4)./5)+(a13./t)).*r.*t;
            s = (a8*log(t)+a9.*t+(a10.*(t.^2)./2)+(a11.*(t.^3)./3)+(a12.*(t.^4)./4)+a14).*r;
        end
    end
    oldfolder = cd('c:UsershpDesktopmatlab_programNew folderspecies');
    

    
    %ploting graph
    
    figure(1);
    plot(t,s,'linewidth',2,'color','r');
    xlabel('Temperature');
    ylabel('Entropy');
    title(sp_name);
    
    figure(2);
    plot(t,cp,'linewidth',2,'color','k');
    xlabel('Temperature');
    ylabel('specific_heat');
    title(sp_name);
    
    figure(3);
    plot(t,h,'linewidth',2,'color','b');
    xlabel('Temperature');
    ylabel('Enthalpy');
    title(sp_name);
    
    mkdir(['c:UsershpDesktopmatlab_programNew folderspecies ', sp_name]);
    cd(['c:UsershpDesktopmatlab_programNew folderspecies ', sp_name]);
    

    
    saveas(figure(1),'1.jpeg');
    saveas(figure(2),'2.jpeg');
    saveas(figure(3),'3.jpeg');
    
    %creating new directry
    cd(oldfolder) 
    moleculer_W = moleculer_weight(sp_name);
    k = sprintf('Moleculer weight of %s is %f',sp_name,moleculer_W);
    disp(k)
end

function program

%function program to get molecular weight
function M = molecular_weight(sp_name)
species = (sp_name)
elements = ['H','C','N','O','A'];
atomic_weight = [1,12,14,16,40];
M = 0;
for i = 1:length(species)
    for j = 1:length(elements)
        if strcmp(sp_name(i),elements(j))
            M=M+atomic_weight(j);
            x=j;
        end
    end
    n = str2num(sp_name(i));
    if n>1
        M=M+(atomic_weight(x)*(n-1));
    end
end
end

outputs:

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.