Challenge

Project 1 - Parsing NASA thermodynamic data

% MAIN CODE

clear all
close all 
clc

% OPENING THE FILE AND READING
f1 = fopen('THERMO.dat','r');

%USING FOR LOOP AND FGETL TO SKIP LINES

for i = 1:5
    a = fgetl(f1);
end
    
%USING FOR LOOP TO REPEAT THE PROCESS FOR ALL 53 SPECIES
for i = 1 :53
    
    % READING HEADER , ASSIGNING & REAING VALUES 
header = fgetl(f1);
b = strsplit(header,' ');
species_name = b{1};
endl = length(b);
local_low_temperatue = str2double(b(endl-3));
local_mid_temperature = str2double(b(endl - 1));
local_high_temperature = str2double(b(end - 2));

%GETTIGN COEFFICIENTS FOR EACH SPECIES

line_1 = fgetl(f1);
pos_e1 = strfind(line_1,'E');
a1=str2double(line_1(1:pos_e1(1)+3));
a2=str2double(line_1(pos_e1(1)+4:pos_e1(2)+3));
a3=str2double(line_1(pos_e1(2)+4:pos_e1(3)+3));
a4=str2double(line_1(pos_e1(3)+4:pos_e1(4)+3));
a5=str2double(line_1(pos_e1(4)+4:pos_e1(5)+3));

line_2 = fgetl(f1);
pos_e2 = strfind(line_2,'E');
a6=str2double(line_2(1:pos_e2(1)+3));
a7=str2double(line_2(pos_e2(1)+4:pos_e2(2)+3));
a8=str2double(line_2(pos_e2(2)+4:pos_e2(3)+3));
a9=str2double(line_2(pos_e2(3)+4:pos_e2(4)+3));
a10=str2double(line_2(pos_e2(4)+4:pos_e2(5)+3));

line_3 = fgetl(f1);
pos_e3 = strfind(line_3,'E');
a11=str2double(line_3(1:pos_e3(1)+3));
a12=str2double(line_3(pos_e3(1)+4:pos_e3(2)+3));
a13=str2double(line_3(pos_e3(2)+4:pos_e3(3)+3));
a14=str2double(line_3(pos_e3(3)+4:pos_e3(4)+3));

%USING LINSPACE COMMAND TO GRNERATE A TEMPERATURE ARRAY
T = linspace(local_low_temperatue,local_high_temperature,1000);
R = 8.314;

%CALLING FUNCTION TO GET CP AND GIVING REQUIRED INPUTS
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temperature,T,R);

%CALLING FUNCTION TO GET H AND GIVING REQUIRED INPUTS
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,local_mid_temperature,T,R);

%CALLING FUNCTION TO GET H AND GIVING REQUIRED INPUTS
S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,local_mid_temperature,T,R);


     %plotting   
        figure(1)
        plot(T,Cp,'Linewidth',2,'color','k');
        xlabel('Temperature');
        ylabel('Specific Heat');
        title(species_name,'T vs Cp');
        grid on
        
        figure(2)
        plot(T,H,'Linewidth',2,'color','k');
        xlabel('Temperature');
        ylabel('Enthalpy');
        title(species_name,'T vs H');
        grid on
        
        figure(3)
         plot(T,S,'Linewidth',2,'color','k');
        xlabel('Temperature');
        ylabel('Entropy');
        title(species_name,'T vs S');
        grid on
        
        %CREATING DIRECTORY 
mkdir(species_name)
cd(species_name)
saveas(1,'T vs Cp','jpg')
saveas(2,'T vs H','jpg')
saveas(3,'T vs S','jpg')
cd ..

%DISPLAYING MOLECULAR WEIGHT IN COMMAND WINDOW FOR EACH SPECIES
mol_weight = molecular_weight(species_name);
fprintf('molecular weight of %s =  %f', species_name, mol_weight)
disp(' ')

%THE FOR LOOP USED TO REPEAT THE PROCESS FOR ALL SPECIES ENDS HERE
end

%FINALLY CLOSING THE FILE THAT WE WORKED UPON 
fclose(f1);
%FUNCION FOR CALCULATING MOLECULAR WEIGHT
function[molecular_weight_calculation] = molecular_weight(species_name)

species = (species_name);

elements = ['H', 'C', 'O', 'N', 'A'];
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948];
molecular_weight_calculation = 0;

for i = 1 : length(species_name)
    for j = 1 : length(elements) 
        if strcmp(species_name(i),elements(j))
            molecular_weight_calculation =  molecular_weight_calculation+atomic_weight(j);
            a=j;
        end
    end
n = str2double(species_name(i));
if n>1;
    molecular_weight_calculation = molecular_weight_calculation+atomic_weight(a)*(n-1);
end
end
%FUNCION FOR CALCULATING ENTROPY
function [S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,local_mid_temperature,T,R)

if T<= local_mid_temperature
    S = ((a8*log(T)) +( a9*T) + (a10*(T.^2)/2) + (a11*(T.^3)/3) +(a12*(T.^4)/4) + a14)*R;
else
      S = ((a1*log(T)) +( a2*T) + (a3*(T.^2)/2) + (a4*(T.^3)/3) +(a5*(T.^4)/4) + a7)*R;
end

end
%FUNCION FOR CALCULATING ENTHALPY
function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,local_mid_temperature,T,R)

if T<= local_mid_temperature
     H  = (a8 + a9*(T/2) + a10*((T.^2)/3) + a11*((T.^3)/4) + a12*((T.^4)/5) + a13./T)*R.*T;
else
      H = (a1+((a2*T)./2)+((a3*T.^2)./3)+((a4*T.^3)./4)+((a5*T.^4)./5)+(a6./T))*R.*T;
end

end
%FUNCION FOR CALCULATING SPECIFIC HEAT
function [Cp] = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temperature,T,R)

if T

Explanation of all codes and functions:

  • The above code is divided into two parts.
    • MAIN CODE
      • The main code performs all the functions like...
      • reading the data from the file
      • opening and closing the file
      • getting the coefficients for all the species
      • Calling the functions from the function code to calculate the desired values
      • Creating a directory to save all the outputs in the desired folder
      • Plotting all the Graphs that would be stored in the directory
    • FUNCTION CODE
      • The function code is created to perform a specific function with ease that would take up a lot of effort instead.
      • In this project, we have written 3 such function codes and then we call them in the main code whenever needed.
      • While creating the function code, we keep in mind that the name of the function code and the file name should be the same,
      • we write down the equations in the function code and the input values required to solve that equation.
      • Next, we save the function and call it using the same name used to save it.
      • All the input values needed to solve the equation should be present in the main code.
  • COMMANDS used and their function.
    • fopen:- To open the file from which we need to read the data
    • fgetl :- To return the next line of the specified file, removing the newline characters
    • strsplit:-To Split a string or character vector at the specified delimiter.
    • str2double:- To convert the text in str to double-precision values.
    • strfind:- to find a specific piece of sting
    • linspace:- To generate an evenly spaced array from any start value to end value with a specified number of values to be printed
    • plot:- To plot a graph
    • mkdir:- to create a folder and folder name to save any output data from the code.
  • Errors
    • There were many errors thrown in the command window.
    • most of them were simply some typing mistakes in the syntax.
    • once such example screenshot is attached below where the error was just because of using curved brackets instead of curly brackets.
    • The error was removed by changing the brackets.
  • Refrences
    • Most of the references i used while working on this project were from matlab pdf provided by skill lync.
    • Also I referred the projects done by other students of skill lync, which are simply available on google.
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.