Project

File Parsing

% File Parsing - NASA Polynomials - Project 1
clear all
close all
clc

f1=fopen(\'therm.dat\',\'r\');

Title=fgetl(f1);

% Extracting Global_low, Global_mid and Global_high temperatures
l1=fgetl(f1);
splitline=strsplit(l1,\' \');
Global_low_temp =str2double(splitline{2});
Global_mid_temp =str2double(splitline{3});
Global_high_temp =str2double(splitline{4});

% Skipping the comment lines
for k=1:3
    skip=fgetl(f1);
end

t=linspace(200,3500,50);
 
f=\'Results\';
mkdir(f)

% Extracting Data
for i=1:53
   [element,E1,E2,E3,e1,e2,e3,h1,l1,h2,h3,h4,h5,h6,h7,l2,l3,l4,l5,l6,l7]= coef_extract(f1);

    ht=[h1;h2;h3;h4;h4;h6;h7];
    lt=[l1;l2;l3;l4;l5;l6;l7];

    % Calculating Cp, H, S & Molecular Weight
    Cp=cp_func(ht,lt,t,Global_mid_temp);
    H=h_func(ht,lt,t,Global_mid_temp);
    S=s_func(ht,lt,t,Global_mid_temp);
    molecular_weight= mw_func(element);


    plot_func(t,Cp,H,S,element,molecular_weight,f);    % Plotting the Graphs 
end
fclose(f1)

Function:
% Function to extract coefficients of each species

function [element,E1,E2,E3,e1,e2,e3,h1,l1,h2,h3,h4,h5,h6,h7,l2,l3,l4,l5,l6,l7]= coef_extract(f1)

tline=fgetl(f1);
element=tline(1:7);

E1 = fgetl(f1);
    E2 = fgetl(f1);
    E3 = fgetl(f1);
    e1 = strfind(E1,\'E\');
    e2 = strfind(E2,\'E\');
    e3 = strfind(E3,\'E\');
    
   % high temp coefficients
    h1 = str2double(E1(1:e1(1)+3));
    h2 = str2double(E1(e1(1)+4:e1(2)+3));
    h3 = str2double(E1(e1(2)+4:e1(3)+3));
    h4 = str2double(E1(e1(3)+4:e1(4)+3));
    h5 = str2double(E1(e1(4)+4:e1(5)+3));
    h6 = str2double(E2(1:e2(1)+3));
    h7 = str2double(E2(e2(1)+4:e2(2)+3));
    
    % low temp coefficients
    l1 = str2double(E2(e2(2)+4:e2(3)+3));
    l2 = str2double(E2(e2(3)+4:e2(4)+3));
    l3 = str2double(E2(e2(4)+4:e2(5)+3));
    l4 = str2double(E3(1:e3(1)+3));
    l5 = str2double(E3(e3(1)+4:e3(2)+3));
    l6 = str2double(E3(e3(2)+4:e3(3)+3));
    l7 = str2double(E3(e3(3)+4:e3(4)+3));

  
end

2. % Function to calculate specific heat
function [Cp]=cp_func(ht,lt,t,Global_mid_temp)

    for i=1:length(t)
        temp1=ht(1);
        temp2=ht(2)*t(i);
        temp3=ht(3)*t(i)^2;
        temp4=ht(4)*t(i)^3;
        temp5=ht(5)*t(i)^4;
        temp6=lt(1);
        temp7=lt(2)*t(i);
        temp8=lt(3)*t(i)^2;
        temp9=lt(4)*t(i)^3;
        temp10=lt(5)*t(i)^4;
    
        if t(i)>=Global_mid_temp
            Cp(i)= temp1 +temp2 +temp3 +temp4 +temp5;
        else 
            Cp(i)= temp6 +temp7 +temp8 +temp9 +temp10;
        end
    end
end

3.% Function to calculate Enthalpy
function [H]=h_func(ht,lt,t,Global_mid_temp)

    for i=1:length(t)
        temp1=ht(1);
        temp2=ht(2).*t(i)/2;
        temp3=ht(3).*(t(i)^2)/3;
        temp4=ht(4).*(t(i)^3)/4;
        temp5=ht(5).*(t(i)^4)/5;
        temp6=ht(6)./t(i);
        temp7=lt(1);
        temp8=lt(2).*t(i)/2;
        temp9=lt(3).*(t(i)^2)/3;
        temp10=lt(4).*(t(i)^3)/4;
        temp11=lt(5).*(t(i)^4)/5;
        temp12=lt(6)./t(i);

        if t(i)>=Global_mid_temp
            H(i)=temp1+temp2+temp3+temp4+temp5+temp6;
        else
            H(i)=temp7+temp8+temp9+temp10+temp11+temp12;
        end
    end
end

4.% Function to calculate Entropy
function [S]=s_func(ht,lt,t,Global_mid_temp)
    
    for i=1:length(t)
    	temp1=ht(1)*log(t(i));
        temp2=ht(2).*t(i);
        temp3=ht(3).*(t(i)^2)/2;
        temp4=ht(4).*(t(i)^3)/3;
        temp5=ht(5).*(t(i)^4)/4;
        temp6=ht(7);
        temp7=lt(1)*log(t(i));
        temp8=lt(2).*t(i);
        temp9=lt(3).*(t(i)^2)/2;
        temp10=lt(4).*(t(i)^3)/3;
        temp11=lt(5).*(t(i)^4)/4;
        temp12=lt(7);
    
        if t(i)>=Global_mid_temp
            S(i)=temp1+temp2+temp3+temp4+temp5+temp6;
        else 
            S(i)=temp7+temp8+temp9+temp10+temp11+temp12;
        end
    end
end

5.% Function to calculate molecular weight

function [molec_weight] = mw_func(element)

    a = [\'H\',\'C\',\'N\',\'O\',\'A\'];
    at_w = [1 ,12, 14, 16, 40]; 
    molec_weight=0;
 
    for i=1:length(element)
        for j=1:length(a)
            if strcmp(element(i),a(j))
                molec_weight=molec_weight+at_w(j); 
                J=j ;
            end
        end
        n=str2num(element(i));
            if n>1
                molec_weight=molec_weight+(at_w(J)*n)-at_w(J);
            end
    end
end

6. %function to plot the Solutions

function plot_func(t,Cp,H,S,element,molecular_weight,f)

    p=pwd;
    cd(f)

    fd=sprintf(\'%s\',element);
    mkdir(fd);

    % Generating plots for Cp vs. Temp
    a=figure(1);
    set(a,\'visible\',\'off\')
    plot(t,Cp,\'-ko\',\'markeredgecolor\',\'r\',\'linewidth\',1,\'markersize\',5,\'markerfacecolor\',\'c\')
    title(sprintf(\'Specific heat vs. Temprature\\n Species - %s Molecular Weight - %d\',element,molecular_weight),\'Fontsize\',13)
    xlabel(\'Temperature\',\'FontSize\',10)
    ylabel(\'Cp/R\',\'FontSize\',10)

    % Generating plots for H vs. Temp
    a = figure(2);
    set(a, \'visible\', \'off\');
    plot(t,H,\'-ks\', \'markeredgecolor\',\'g\',\'linewidth\',1 ,\'markersize\',5,\'markerfacecolor\',\'y\') 
    title(sprintf(\'Enthalpy vs. Temprature\\nSpecies - %s Molecular weight - %d\',element,molecular_weight),\'FontSize\',13)     
    xlabel(\'Temperature\',\'FontSize\',10)
    ylabel(\'H/RT\',\'FontSize\',10)

    % Generating plots for S vs. Temp
    a = figure(3);
    set(a, \'visible\', \'off\') 
    plot(t,S,\'-kd\', \'markeredgecolor\',\'b\',\'linewidth\',1,\'markersize\',5,\'markerfacecolor\',\'m\') 
    title(sprintf(\'Entropy vs. Temprature\\nSpecies - %sMolecular weight - %d\',element,molecular_weight),\'FontSize\',13)     
    xlabel(\'Temperature\',\'FontSize\',10)
    ylabel(\'S/R\',\'FontSize\',10)

    cd(fd)
    saveas(1,\'Cp.png\')
    saveas(2,\'H.png\')
    saveas(3,\'S.png\')
    cd(p)
end

Nasa_File_Parsing,

Using the fgetl, fopen, strfind and coverting the string to number which str2double function the project was done. Basically, read the file and using the given formulas created a function call and calculated the enthalpy, entropy and specific heat for the required elements from the file.

Results:

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.