Challenge

Project 1 - Parsing NASA thermodynamic data

The errors thrown are shown in images and they are overcome with the help of videos in youtube and staff members.

faced a lot of errors in this program.

molecular weight of O : 15
molecular weight of O2 : 30
molecular weight of H : 1
molecular weight of H2 : 2
molecular weight of OH : 16
molecular weight of H2O : 17
molecular weight of HO2 : 31
molecular weight of H2O2 : 32
molecular weight of C : 12
molecular weight of CH : 13
molecular weight of CH2 : 14
molecular weight of CH2(S) : 14
molecular weight of CH3 : 15
molecular weight of CH4 : 16
molecular weight of CO : 27
molecular weight of CO2 : 42
molecular weight of HCO : 28
molecular weight of CH2O : 29
molecular weight of CH2OH : 30
molecular weight of CH3O : 30
molecular weight of CH3OH : 31
molecular weight of C2H : 25
molecular weight of C2H2 : 26
molecular weight of C2H3 : 27
molecular weight of C2H4 : 28
molecular weight of C2H5 : 29
molecular weight of C2H6 : 30
molecular weight of CH2CO : 41
molecular weight of HCCO : 40
molecular weight of HCCOH : 41
molecular weight of H2CN : 28
molecular weight of HCN : 27
molecular weight of HNO : 30
molecular weight of N : 14
molecular weight of NNH : 29
molecular weight of N2O : 43
molecular weight of NH : 15
molecular weight of NH2 : 16
molecular weight of NH3 : 17
molecular weight of NO : 29
molecular weight of NO2 : 44
molecular weight of HCNO : 42
molecular weight of HOCN : 42
molecular weight of HNCO : 42
molecular weight of NCO : 41
molecular weight of CN : 26
molecular weight of HCNN : 41
molecular weight of N2 : 28
molecular weight of AR : 39
molecular weight of C3H8 : 44
molecular weight of C3H7 : 43
molecular weight of CH3CHO : 43
molecular weight of CH2CHO : 42

% write a program to read and extract nasa file and plot the graphs
clear all %clears all values in the workspace
close all % closes all previous results
clc       % clears the command window
f1=fopen('THERMO.dat','r');  %opens thermo file to read and assign it to f1
first_line=fgetl(f1);    % get firat line from f1
%global minimum,maximum and medium temp
global_temp=fgetl(f1);  %gets the next line from f1 and assigns it to global temp
temp_range=strsplit(global_temp,' '); % the temp is being splitted using blank space
%obtain temp values from splitted values 
global_min_temp= str2num(temp_range{2}); % converts string to number
global_med_temp= str2num(temp_range{3}); % converts string to number
global_max_temp= str2num(temp_range{4});  % converts string to number
% creating a vector of 400 alloted temp points 
T=linspace(global_min_temp,global_max_temp,400); % assign 400 values to temperature from min to max values
R=8.314;    % universal gas constant
comments1=fgetl(f1); %gets the next line from f1
comments2=fgetl(f1); %gets the next line from f1
comments3=fgetl(f1);  %gets the next line from f1
for i=1:53   % for loop is initialized for 1 to 53 times
    line1=fgetl(f1);  %gets the next line from f1and assign in line 1
    elements_name=strtok(line1); %gets the selected parts of the string
    
    line2=fgetl(f1);  %gets the next line from f1and assign in line 2
    A=strfind(line2,'E'); % used to find E in the string
    
    line3=fgetl(f1); %gets the next line from f1and assign in line 3
    B=strfind(line3,'E');  % used to find E in the string
    
    line4=fgetl(f1); %gets the next line from f1and assign in line 4
    C=strfind(line4,'E'); % used to find E in the string
    
    a1=str2num(line2(1:A(1)+3)); % first 4 string is converted in to number and saved into a1
    a2=str2num(line2(A(1)+4:A(2)+3)); % next 4 string is converted in to number and saved into a2
    a3=str2num(line2(A(2)+4:A(3)+3)); % next 4 string is converted in to number and saved into a3
    a4=str2num(line2(A(3)+4:A(4)+3)); % next 4 string is converted in to number and saved into a4
    a5=str2num(line2(A(4)+4:A(5)+3));  % next 4 string is converted in to number and saved into a5
     
     a6=str2num(line3(1:B(1)+3));   % first 4 string is converted in to number and saved into a6
     a7=str2num(line3(B(1)+4:B(2)+3));  % next 4 string is converted in to number and saved into a7
     
     b1=str2num(line3(B(2)+4:B(3)+3));  % next 4 string is converted in to number and saved into b1
     b2=str2num(line3(B(3)+4:B(4)+3));  % next 4 string is converted in to number and saved into b2
     b3=str2num(line3(B(4)+4:B(5)+3));  % next 4 string is converted in to number and saved into b3
     
     b4=str2num(line4(1:C(1)+3));
     b5=str2num(line4(C(1)+4:C(2)+3));
     b6=str2num(line4(C(2)+4:C(3)+3));
     b7=str2num(line4(C(3)+4:C(4)+3));
     %specific heat call
     cp=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_med_temp); % specific_heat function is called
     %enthalphy call
     h=enthalphy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_med_temp);  % enthalphy function is called
    %entrophy call 
    s=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_med_temp);  % entropy function is called
    %molecular weight call
    molecular_wt(i)= molecular_weight(elements_name);   % molecular_wt function is called
    
    % to save and plot the data.
    cd('C:\Users\pradeep.p\Desktop\Test run\Nasa_file')   % values and data are saved in the required directory
    dir_name=elements_name; % saved with the respective names
    mkdir(dir_name);
    
    figure(1)    % first figure is being drawn
    plot(T,cp,'linewidth',3,'color','r') % plot between temp and cp is drawn
    title(sprintf('Specific heat vs Temperature for %s',elements_name)); % title is given  
        xlabel('Temperature'); % xlabel is given
        ylabel('Specific heat');  % ylabel is given
        
        figure(2) % second figure is being drawn
    plot(T,h,'linewidth',3,'color','b') % plot between temp and enthalphy is drawn
    title(sprintf('Enthalphy vs Temperature for %s',elements_name));   % title is given  
        xlabel('Temperature'); % xlabel is given
        ylabel('Enthalphy');  % ylabel is given
        
        figure(3)
    plot(T,s,'linewidth',3,'color','g')  % plot between temp and enthalphy is drawn
    title(sprintf('Entrophy vs Temperature for %s',elements_name));  % title is given
        xlabel('Temperature'); % xlabel is given
        ylabel('Entrophy'); % ylabel is given
        
        pause(0.001) %to see the graph in the real time 
        cd(dir_name); % in the same directory images are saved
        saveas(1,'specific_heat.jpg');
        saveas(2,'Enthalphy.jpg');
        saveas(3,'Entrophy.jpg');
end             % terminate the loop
fclose(f1);  % closes the file 

%to calculate the molecular weight(function)
function MW = molecular_weight(elements_name)
atoms=['H','C','N','O','A'];
fprintf('molecular weight of ');
fprintf(elements_name);
fprintf(' : ');
atomic_weight=[1 12 14 15 39];
MW=0;
for i=1:length(elements_name);
    for j=1:length(atoms);
        if strcmp(elements_name(i),atoms(j)); %used to compare element with space
            MW=MW+atomic_weight(j);
            position=j;
        end 
    end
    n=str2num(elements_name(i));
    if n > 1
        MW=MW+atomic_weight(position)*(n-1);
    end
end
fprintf('%d',MW);
disp(' ');
end

%function to calculate specific heat
function cp=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_med_temp);
if T> global_med_temp
    cp=R.*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
else
    cp=R.*(b1+b2.*T+b3.*T.^2+b4.*T.^3+b5.*T.^4);
end

%function to calculate enthalphy
function h=enthalphy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_med_temp);
if T> global_med_temp
    h=R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5)./5+a6);
else
    h=R.*(b1.*T+(b2.*T.^2)./2+(b3.*T.^3)./3+(b4.*T.^4)./4+(b5.*T.^5)./5+b6);
end

%function to calculate entropy
function s=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_med_temp);
if T<= global_med_temp
    s=R.*(a1.*log(T)+(a2.*T)+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7);
else
    s=R.*(b1.*log(T)+b2.*T+b3.*(T.^2)./2+b4.*(T.^3)./3+b5.*(T.^4)./4+b7);
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.