Challenge

Project 1 - Parsing NASA thermodynamic data

Function Code:-

%nasa file parsing

clear all
close all
clc

%input for universal gas constant
R = 8.314;

%reading the file
f = fopen('THERMO.dat','r');

%reading the header 
for i=1:5
    fgetl(f)
end
for j=1:53

    %extracting data from species block
    line_1 = fgetl(f);
    a = strsplit(line_1,' ');
    b = length(a);

    %to extract species name
    species_name = a{1};

    %extracting local temperature
    local_low_temp = a{b-3};
    local_high_temp = a{b-2};
    local_mid_temp = a{b-1};

    %extracting local temperature
    local_low_temp = str2num(local_low_temp);
    local_high_temp = str2num(local_high_temp);
    local_mid_temp = str2num(local_mid_temp);

    %extraction of coefficients
    coeff_line_1 = fgetl(f);

    %reading position of E in coeff line
    posE1 = strfind(coeff_line_1,'E')

    a1 = str2num(coeff_line_1(1:posE1(1)+3))
    a2 = str2num(coeff_line_1(posE1(1)+4:posE1(2)+3))
    a3 = str2num(coeff_line_1(posE1(2)+4:posE1(3)+3))
    a4 = str2num(coeff_line_1(posE1(3)+4:posE1(4)+3))
    a5 = str2num(coeff_line_1(posE1(4)+4:posE1(5)+3))

    coeff_line_2 = fgetl(f)
    posE2 = strfind(coeff_line_2,'E')

    a6 = str2num(coeff_line_2(1:posE1(1)+3))
    a7 = str2num(coeff_line_2(posE2(1)+4:posE2(2)+3))
    a8 = str2num(coeff_line_2(posE2(2)+4:posE2(3)+3))
    a9 = str2num(coeff_line_2(posE2(3)+4:posE2(4)+3))
    a10 = str2num(coeff_line_2(posE2(4)+4:posE2(5)+3))

    coeff_line_3 = fgetl(f)
    posE3 = strfind(coeff_line_3,'E')

    a11 = str2num(coeff_line_3(1:posE3(1)+3))
    a12 = str2num(coeff_line_3(posE3(1)+4:posE3(2)+3))
    a13 = str2num(coeff_line_3(posE3(2)+4:posE3(3)+3))
    a14 = str2num(coeff_line_3(posE3(3)+4:posE3(4)+3))

    %take input for temp
    T = linspace(local_low_temp,local_high_temp,500)

    %calculation of Cp,H,S
    for i=1:length(T)
        if T(i)>local_mid_temp   %high tempp coefficient used
            Cp(i) = R.*(a1+a2.*T(i)+a3.*T(i).^2+a4.*T(i).^3+a5.*T(i).^4);
            H(i) = R.*T(i).*(a1+(a2.*T(i))./2+(a3.*T(i).^2)./3+(a4.*T(i).^3)./4+(a5.*T(i).^4)./5+(a6./T(i)));
            s(i) = R.*((a1.*log(T(i))+a2.*T(i)+(a3.*T(i).^2)./2+(a4.*T(i).^3)./3+(a5.*T(i).^4)./4+a7));
        else T(i)<=local_mid_temp;
            Cp(i) = R.*(a8+a9.*T(i)+a10.*T(i).^2+a11.*T(i).^3+a12.*T(i).^4);
            H(i) = R.*T(i).*(a8+(a9.*T(i))./2+(a10.*T(i).^2)./3+(a11.*T(i).^3)./4+(a12.*T(i).^4)./5+(a13./T(i)));
            s(i) = R.*((a8.*log(T(i))+a9.*T(i)+(a10.*T(i).^2)./2+(a11.*T(i).^3)./3+(a12.*T(i).^4)./4+a14));
        end
    end


    %plotting and saving of the results


    %saving the file and creating the folders
    cd('C:\D Drive\MATLAB CHALLENGES\Challenges_Matlab\Challenge15\Project1-Parsing NASA thermodynamic data')
    mkdir(species_name)
    cd(species_name)

    %plotting and saving the results
    %plotting temperature vs specific heat
    figure(1)
    plot(T,Cp,'linewidth',2,'color','b')
    xlabel('Temperature in kelvin(k)')
    ylabel('Specific heat(Cp)')
    grid on
    title('specific heat vs temp') 
    saveas(figure(1),'Specific heat.jpg')
    
    %plotting temperature vs enthalpy
    figure(2)
    plot(T,H,'linewidth',2,'color','r')
    xlabel('Temperature in kelvin(k)')
    ylabel('Enthalpy(H)')
    grid on
    title('Enthalpy vs temp')
    saveas(figure(2),'Enthalpy.jpg')


    %plotting temperature vs entropy
    figure(3)
    plot(T,s,'linewidth',2,'color','g')
    xlabel('Temperature in kelvin(k)')
    ylabel('Entropy(s)')
    grid on
    title('Entropy vs temp')
    saveas(figure(3),'Entropy.jpg')

    cd .. 

    %calculation of molecular weight
    atomic_name = ['H','C','O','N','A'];
    atomic_weight = [1,12,16,14,39];
    molecular_weight = 0;

    for j=1:length(species_name)
        for k=1:length(atomic_name)
            if strcmp(species_name(j),atomic_name(k))
                molecular_weight = molecular_weight + atomic_weight(k);
                position = k;
            end
        end
        n = str2num(species_name(j));
        if(n>1)
            moleccular_weight = molecular_weight + atomic_weight(position)*(n-1);
        end
    end
    k = fprintf('molecular weight=species_name,molecular_weight');
    disp(k)


end
   

Output :-

Google drive link :-

https://drive.google.com/drive/folders/1p2IKdNviTGXR9ppyzSp-AtT6U-SbgMKf?usp=share_link

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.