Project

nasa file parsing

Main program to read the data from the given file:

each line is obtained from the file using fgetl line by line and is analysed. commanda like strsplit, strcmp are used to extract data from the separated lines. Using the formula given the separated coefficcients are applied to obtain Cp, S and H values for each molecular species.

% to read the given file 
f1 = fopen(\'nasa.dat\',\'r\');

% header of f1 
h1 = fgetl(f1);
h2 = fgetl(f1);

% removing lines from 3 to 5, since the next line after header is 3rd line,
% indexing from 1 to 3 will give lines 3 to 5
for l = 1:3
    fgetl(f1) 
end

%to separate the data provided of each species. there are 53 species in total
%1:53 loop since there are 53 species
for i=1:53
    l6 = fgetl(f1);     %line 6 because first species line is 6th line to simplify 
    s = strsplit(l6,\' \') %splitting of line 6 using space as a criterion
    sp = s(1)           %species name
    d = findstr(l6,\'.\'); %finding numbers using decimal in them 
    temprange = l6((d(1)-3):(d(1)+23)) %as 3 numbers are before \'.\' and 23 after the first number, we are getting entire set of numbers 
    temp = strsplit(temprange,\' \') %seperating the numbers using space as a criterion 
    
    locallowtemp = str2num(temp{1}) %{} is used to call the value from temp array to be stored in a cell. str2num converts the string to numerical value
    temp1 = locallowtemp;
    
    localhightemp = str2num(temp{2})
    temp2 = localhightemp;
    
    localmidtemp = str2num(temp{3})
    temp3 = localhightemp;
       
    %coefficients from line 7
    l7 = fgetl(f1);
    e = strsplit(l7,\' \'); %seperating the numbers using space as a criterion
    
    a1 = str2num(e{1});
   
    a2 = str2num(e{2});
    
    a3 = str2num(e{3});
    
    a4 = str2num(e{4});
    
    a5 = str2num(e{5});
    
    %coefficients from line 8
    l8 = fgetl(f1);
    f = strsplit(l8,\' \'); %seperating the numbers using space as a criterion

    a6 = str2num(f{1})
    
    a7 = str2num(f{2})
    
    b1 = str2num(f{3})
    
    b2 = str2num(f{4})
    
    b3 = str2num(f{5})

    %coefficients from line 9
    l9 = fgetl(f1);
    g = strsplit(l9,\' \'); %seperating the numbers using space as a criterion
    
    b4 = str2num(g{1})
    
    b5 = str2num(g{2})
    
    b6 = str2num(g{3})
    
    b7 = str2num(g{4})
     
    molecule_name = char(sp) %to idenitfy the molecular species
    
   
    mkdir([\'E:\\fileparse\\MOLECULE\',molecule_name]) %creating directory for each species in file fileparse 
    
    %to solve for Cp, H, and S
    M = linspace(temp1,temp2,20); %creating a range between low and high temperature
    T = int16(M(1:20)) %to get integer values of temperatures
    R = 8.314;
    
    k = 1:length(T); 
    for j=1:length(k)
        if T(k)<=1000 %equations of low temperature
            Cp = R*(b1+(b2*T(k))+(b3*T(k).^2)+(b4*T(k).^3)+(b5*T(k).^4))
            H = (R*T(k)).*(b1+(b2*T(k)./2)+(b3*T(k).^2/3)+(b4*T(k).^3/4)+(b5*T(k).^4/5)+(b6/T))
            S = R*(b1*ln(T(k))+(b2*T(k))+(b3*T(k).^2/2)+(b4*T(k).^3/3)+(b5*T(k).^4/4)+b7)
            
        else %equations of low temperature
            Cp = R*(a1+(a2*T(k))+(a3*T(k).^2)+(a4*T(k).^3)+(a5*T(k).^4))
            H = (R*T(k)).*(a1+(a2*T(k)./2)+(a3*T(k).^2/3)+(a4*T(k).^3/4)+(a5*T(k).^4/5)+(a6./T(k)))
            S = R*(a1*log(double(T(k)))+double((a2*T(k))+(a3*T(k).^2/2)+(a4*T(k).^3/3)+(a5*T(k).^4/4)+a7))
        end
    end
    
    %plots
    figure(1)
    plot(M,H,\'-*\');
    title(molecule_name);
    xlabel(\'temperature\')
    ylabel(\'enthalpy\')
    
    figure(2)
    plot(M,Cp,\'o\')
    title(molecule_name);
    xlabel(\'temperature\')
    ylabel(\'specific heat\')
    
    figure(3)
    plot(M,S,\'-\')
    title(molecule_name);
    xlabel(\'temperature\')
    ylabel(\'entropy\')
    
    
    directory = ([\'E:\\fileparse\\MOLECULE\',molecule_name])
    cd(directory) %to change the current directory
    saveas(1,\'Enthalpy.jpg\'); %saving the plots in respective folder for each molecular species
    saveas(2,\'Specific heat.jpg\');
    saveas(3,\'Entropy.jpg\');
    cd(\'E:\\fileparse\') %changing the directory back to file fileparse so as to create another molecular species file in fileparse 
    weigth(i) = molecular_mass(molecule_name); %calling the function molecular_mass to calculate molecular mass of each species 
end

molecular_mass function called in the above program:

characters of molecular species are compares with the array created and their respective atomic weights are added.

function [mm] = molecular_mass(molecule_name) %defining the function 

fprintf(\'molecular weight of \'); 
fprintf(molecule_name); %molecule_name to get each species
fprintf(\' : \');
atoms = [\'H\',\'C\',\'N\',\'O\',\'A\']; %defining the present atoms array

aw = [1 12 14 16 40]; %defining the respective atomic weights of each species
mm = 0;

for i = 1:length(molecule_name)
    
    for j = 1:length(atoms)
        if strcmpi(molecule_name(i),atoms(j)) %comparing each species of the compound with the defined atoms array
            mm = mm + aw(j); %adding of atomic weights each species
            position = j; 
        end
    end
    
    n = str2num(molecule_name(i)) %to check if any atom has a multiple
    
    if n > 1
        mm = mm + aw(position)*(n-1) %to multiply the weight of molecuar species 
    end
    
    fprintf(\'%d\',mm);
    disp(\' \');
end

Screenshot of folders for each molecular species:

Plots of:

O2

N2

CO2

molecular mass of all the species:

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.