Challenge

Project 1 - Parsing NASA thermodynamic data

close all
clear all
clc

%Opening the file that is stored in the file
Thermo = fopen('THERMO.dat','r')

%Getting the first line from the opened file
fgetl(Thermo);
f1 = fgetl(Thermo);
A = strsplit(f1, ' ');

Global_low_temperature = str2num(A{2});
Global_mid_temperature = str2num(A{3});
Global_high_temperature = str2num(A{4});

fgetl(Thermo);
fgetl(Thermo);
fgetl(Thermo);

for i = 1:53
    f2 = fgetl(Thermo);
    B = strsplit(f2, ' ');
    Species_name = (B{1});
    
    Local_low_temperature = str2num(B{end-3});
    Local_high_temperature = str2num(B{end-2});
    Local_mid_temperature = str2num(B{end-1});
    
    f3 = fgetl(Thermo);
    C = findstr(f3, 'E');
    Coeff1 = f3(1:12+3);
    Coeff2 = f3(16:30);
    Coeff3 = f3(31:45);
    Coeff4 = f3(46:60);
    Coeff5 = f3(61:75);
    
    f4 = fgetl(Thermo);
    D = findstr(f4,'E');
    Coeff6 = f4(1:15);
    Coeff7 = f4(16:30);
    Coeff8 = f4(31:45);
    Coeff9 = f4(46:60);
    Coeff10 = f4(61:75);
    
    f5 = fgetl(Thermo);
    E = findstr(f5,'E');
    Coeff11 = f5(1:15);
    Coeff12 = f5(16:30);
    Coeff13 = f5(31:45);
    Coeff14 = f5(46:60);
    
    %To calculate the various Thermodynamic Properties:
    T = [Local_low_temperature : Local_high_temperature];
    j=1;
    Specific_Heat = 0;
    Enthalpy = 0;
    Entropy = 0;
    
    %High_temp_coeffs = Coeff1, Coeff2, Coeff3, Coeff4, Coeff5, Coeff6, Coeff7
    %Low_temp_coeffs = Coeff8, Coeff9, Coeff10, Coeff11, Coeff12, Coeff13, Coeff14
    for t = T
        R = 8.314; %Universal Gas Constant
        if t <= Local_mid_temperature
            a1= str2num(Coeff8);
            a2= str2num(Coeff9);
            a3= str2num(Coeff10);
            a4= str2num(Coeff11);
            a5= str2num(Coeff12);
            a6= str2num(Coeff13);
            a7= str2num(Coeff14);
        else
            a1= str2num(Coeff1);
            a2= str2num(Coeff2);
            a3= str2num(Coeff3);
            a4= str2num(Coeff4);
            a5= str2num(Coeff5);
            a6= str2num(Coeff6);
            a7= str2num(Coeff7);
        end
    %Thermodynamic Property Formulae:
    Specific_Heat(j) = (a1 + (a2* t) + (a3* t^2) + (a4* t^3) + (a5* t^4))* R;
    Enthalpy(j) = (a1 + (a2 * t) / 2 + (a3 * t^2) / 3 + (a4 *t^3) / 4 + (a5* t^4) / 5 + (a6 / t)) * R * t;
    Entropy(j) = (a1 * log(t) + (a2 * t) + (a3 * t^2) / 2 + (a4 * t^3) / 3 + (a5 * t^4) / 4 + (a7)) * R;
    
    j = j+1;
    end
    
    %Plotting the results:
    plot(T, Specific_Heat, 'linewidth', 3, 'color', 'r')
    xlabel('Temperature (K)')
    ylabel('Specific Heat (J kg−1 K−1)')
    title(char(Species_name))
    url = sprintf('E:/MHEV/Project/%s/Specific Heat Plot.jpg',Species_name);
    saveas(gcf,url)
    
    plot (T, Enthalpy, 'linewidth', 3, 'color', 'b' )
    xlabel('Temperature (K)')
    ylabel('Enthalpy (J)')
    title(char(Species_name))
    url1 = sprintf('E:/MHEV/Project/%s/Enthalpy Plot.jpg',Species_name);
    saveas(gcf,url1)
    
    plot(T, Entropy, 'Linewidth', 3, 'color', 'g')
    xlabel('Temperature (K)')
    ylabel('Entropy (J/K)')
    title(char(Species_name))
    url2 = sprintf('E:/MHEV/Project/%s/Entropy Plot.jpg',Species_name);
    saveas(gcf,url2)
    
    %To calculate the Molecular weight of the Species:
    Q = 0;
    
    %All the atoms that are used in all the Species are mentioned here.
    Atoms = ['H', 'C', 'N', 'O', 'A', 'R', '(', ')', 'S']; 

    %Corresponding Atomic masses are stated here
    Atomic_mass = [1, 12, 14, 16, 40, 0, 0, 0, 0];
    
    for k= 1:length(Species_name)
        z = Species_name(k) == Atoms; %Creating a Logical Array under the variable of z.
        ind = find(z==1); %Finding the index where we can get
        z = z*z'; %z' = Z_transpose
        
        if z
            Q(k) = Atomic_mass(ind); %We take the Atomic Mass at the index where we get a 1 in the logical array.
        else
            Atomic_mass_index = find(Atoms==Species_name(k-1));
            Q(k-1) = str2num(Species_name(k)) * Atomic_mass(Atomic_mass_index);
        end
    end
    
    Molecular_weight(i) = sum(Q);
    
    %Species_Name = Species_name(i)
    fprintf('Molecular Weight of %s = %f u', Species_name, Molecular_weight(i))

end   
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.