Challenge

Project 1 - Parsing NASA thermodynamic data

- Main Code :
close all
clear all
clc
input_file = fopen("THERMO.dat", "r");
 R = 8.3145; % Universal Gas constant J/Mol*Km 
  for o = 1:218
      Y{o} = fgetl(input_file);
  end
%--------------------------------Passing the element Name ---------------%
prompt = "Enter species name O2, N2 or CO ? ";%Enter the element in the list : O2, N2 and CO
x = input(prompt);
%---------------------------------Functions------------------------------------%
coeff_value = coeff(x) %  -- Coefficent extractor function
local_temp = loc_temp(x) % local temperature extractor using function
temp_t = linspace(local_temp(1),local_temp(2),1000);
%-------------------------------- Calculating Cp H S----------------------%
temp_low =[];
cp_low =[]; % specific heat -- a1 to a7
H_low =[]; % Enthalpy -- a1 to a7
S_low =[]; % Entropy -- a1 to a7
temp_high=[];
cp_high =[]; % specific heat -- a8 to a14
H_high =[]; % Enthalpy -- a8 to a14
S_high =[]; % Entropy -- a8 to a14
for z = temp_t
    if z < local_temp(3)
    temp_low(end+1) = z;
    cp_low(end+1) = (coeff_value(1)+(coeff_value(2).*z)+(coeff_value(3).*z.^2)+(coeff_value(4).*z.^3)+(coeff_value(5).*z.^4))*R;
    H_low(end+1) = (coeff_value(1)+(coeff_value(2).*z/2)+(coeff_value(3).*(z.^2)/3)+(coeff_value(4).*(z.^3)/4)+(coeff_value(5).*(z.^4)/5)+coeff_value(6).*z)*R;
    S_low(end+1) = (coeff_value(1)*log(z)+(coeff_value(2).*z)+(coeff_value(3).*(z.^2)/2)+(coeff_value(4).*(z.^3)/3)+(coeff_value(5).*(z.^4)/4)+coeff_value(7))*R;
    %high
    elseif z >= local_temp(3)
    temp_high(end+1) = z;
    cp_high(end+1) = (coeff_value(8)+(coeff_value(9).*z)+(coeff_value(10).*z.^2)+(coeff_value(11).*z.^3)+(coeff_value(12).*z.^4))*R;
    H_high(end+1) = (coeff_value(8)+(coeff_value(9).*z/2)+(coeff_value(10).*(z.^2)/3)+(coeff_value(11).*(z.^3)/4)+(coeff_value(12).*(z.^4)/5)+coeff_value(13).*z)*R;
    S_high(end+1) = (coeff_value(8)*log(z)+(coeff_value(9).*z)+(coeff_value(10).*(z.^2)/2)+(coeff_value(11).*(z.^3)/3)+(coeff_value(12).*(z.^4)/4)+coeff_value(14))*R;
    end
end
figure(1)
hold on
title('Temperature Vs Cp')
plot(temp_low,cp_low,'linewidth',3,'color','r')
plot(temp_high,cp_high,'linewidth',3,'color','b')
xlabel('Temperature')
ylabel('Cp')
legend('Low Temperature', 'High Temperature')
saveas(gcf,'Temperature_Vs_Cp.png')
figure(2)
hold on
title('Temperature Vs Enthalpy(H)')
plot (temp_low,H_low,'linewidth',3,'color','r')
plot (temp_high,H_high,'linewidth',3,'color','b')
xlabel('Temperature')
ylabel('H')
legend('Low Temperature', 'High Temperature')
saveas(gcf,'Temperature_Vs_H.png')
figure(3)
hold on
title('Temperature Vs Entropy(S)')
plot (temp_low,S_low,'linewidth',3,'color','r')
plot (temp_high,S_high,'linewidth',3,'color','b')
xlabel('Temperature')
ylabel('S')
legend('Low Temperature', 'High Temperature')
saveas(gcf,'Temperature_Vs_S.png')
hold off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Coeff Function %%%%%%%%%%%%%%%%%%%%%
%function to extract the coeff Value for the given species name
function coeff = string(species_name)
input_file = fopen("THERMO.dat", "r");
 R = 8.3145; % Universal Gas constant J/Mol*Km 
  for i = 1:218
      L{i} = fgetl(input_file);
  end
    a1 =[];
    a2 =[];
    a3 =[];
    a4= [];
    a5 =[];
%----------------------------- For accessing first line a1 to a5---------%
    for j = 7:4:217
        A = str2num(L{j});
        a1(end+1) = A(1);
        a2(end+1) = A(2);
        a3(end+1) = A(3);
        a4(end+1) = A(4);
        a5(end+1) = A(5);
    end
%----------------------------- For accesing second line a6 to a10---------%
    a6 =[];
    a7 =[];
    a8 =[];
    a9= [];
    a10 =[];
%
    for k = 8:4:217
        B = str2num(L{k});
        a6(end+1) = B(1);
        a7(end+1) = B(2);
        a8(end+1) = B(3);
        a9(end+1) = B(4);
        a10(end+1) =B(5);
    end
%----------------------------------For accesing 3rd line a11 to a14------%
    a11 =[];
    a12 =[];
    a13 =[];
    a14= [];
    %
    for m = 9:4:217
        C = str2num(L{m});
        a11(end+1) = C(1);
        a12(end+1) = C(2);
        a13(end+1) = C(3);
        a14(end+1) = C(4);
    end
    if species_name == 'O2'
       p=2;
       coeff = [a1(p),a2(p),a3(p),a4(p),a5(p),a6(p),a7(p),a8(p),a9(p),a10(p),a11(p),a12(p),a13(p),a14(p)];
    elseif species_name == 'N2'
        p=48;
        coeff = [a1(p),a2(p),a3(p),a4(p),a5(p),a6(p),a7(p),a8(p),a9(p),a10(p),a11(p),a12(p),a13(p),a14(p)];
    elseif species_name == 'CO'
        p=15;
        coeff = [a1(p),a2(p),a3(p),a4(p),a5(p),a6(p),a7(p),a8(p),a9(p),a10(p),a11(p),a12(p),a13(p),a14(p)];
    else
        disp("invalide Element name")
    end
        
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Local Temp Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%

function loc_temp = string(species_name)
     input_file = fopen("THERMO.dat", "r");
    R = 8.3145; % Universal Gas constant J/Mol*Km 
  for u = 1:218
      L{u} = fgetl(input_file);
  end
  low_temp=[];
  high_temp=[];
  mid_temp = [];
  for m = 6:4:217
        A = (L{m});
        low_temp(end+1) =str2num (L{m}(48:51));
        high_temp(end+1) =str2num (L{m}(58:61));
        mid_temp(end+1) =str2num (L{m}(68:71));
  end
  if species_name == 'O2'
       p=2;
       loc_temp = [low_temp(p), high_temp(p),mid_temp(p)];
    elseif species_name == 'N2'
        p=48;
        loc_temp = [low_temp(p), high_temp(p),mid_temp(p)];
    elseif species_name == 'CO'
        p=15;
        loc_temp = [low_temp(p), high_temp(p),mid_temp(p)];
    else
        disp("invalide Element name")
    end
end

user input Prompt:

Output:

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.