Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

To write a program in Matlab to that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file, plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species and Calculate the molecular weight of each species and display it in the command window.

SOFTWARE USED:

Matlab

GOVERNING EQUATIONS:


OBJECTIVES:

  1. To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
  2. Calculate the molecular weight of each species and display it in the command window.
  3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
  4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
  5. Plots for all species will be shown.

BODY OF CONTEXT:

PROGRAM CODE:

% PROJECT: File Parsing - NASA thermodynamic data

clear all; % clear all variables in workspace

close all;% close all plots

clc;%clear command window

% opening file

f1 = fopen('THERMO.dat','r'); %Initialising to f1 the Thermo.dat values and assigning in read mode property

% skipping first line

% a=fgetl(f1);

%

% % global temperatures

% temps = fgetl(f1);

% temps = str2num(temps);

%

% global_low_temp = temps(1);

% global_mid_temp = temps(2);

% global_high_temp = temps(3);

% creating array for temperature

% T = linspace(global_low_temp,global_high_temp,500);

% universal gas constant

R = 8.314;

% skipping next 3 lines

for i=1:5

fgetl(f1);% calling next line

end

% storing starts

%Taking data of 53 species in file

for i=1:53

% creating array for species

tline = fgetl(f1); %Initialising tline with first line after 5 lines

A = strsplit(tline,' ');%Strings are split with ' ' and saved to A

% species

species = A{1};%assigning A{1} with Species name,{} are used to understand same set of variables

%local temperatures

tlow = str2num(A{end-3}); % Assigning t low by converting string to number at respected psoition

thigh = str2num(A{end-2}); % Assigning t high by converting string to number at respected psoition

tmid = str2num(A{end-1}); % Assigning t midby converting string to number at respected psoition

T = linspace(tlow,thigh,500); %Initilaising T with tlow and thigh as end limits and divide into 500 equal divisions

% finding coefficients

% line 1

tline1 = fgetl(f1); %Initializing tline1 with next line

pos1 = findstr(tline1,'E'); %Findinding position of 'E' in the lines and storing location in pos1

% higher coefficients

a1 = str2num(tline1(1:pos1(1)+3));%Storing First coefficient

a2 = str2num(tline1(pos1(1)+4:pos1(2)+3));%Storing Second coefficient

a3 = str2num(tline1(pos1(2)+4:pos1(3)+3));%Storing Third coefficient

a4 = str2num(tline1(pos1(3)+4:pos1(4)+3));%Storing Fourth coefficient

a5 = str2num(tline1(pos1(4)+4:pos1(5)+3));%Storing Fifth coefficient

% line 2

tline2 = fgetl(f1);%Moving to next line

pos2 = findstr(tline2,'E');%Storing position of 'E' in pos2

a6 = str2num(tline2(1:pos2(1)+3));%Storing Sixth coefficient

a7 = str2num(tline2(pos2(1)+4:pos2(2)+3)); %Storing Seventh coefficient

% lower coefficients

a8 = str2num(tline2(pos2(2)+4:pos2(3)+3)); %Storing Ninenth coefficient

a10 = str2num(tline2(pos2(4)+4:pos2(5)+3)); %Storing Tenth coefficient

% line 3

tline3 = fgetl(f1);%Moving to next line

pos3 = findstr(tline3,'E'); %Storing position of 'E' in pos3

a11 = str2num(tline3(1:pos3(1)+3)); %Storing eleventh coefficient

a12 = str2num(tline3(pos3(1)+4:pos3(2)+3)); %Storing Twelth coefficient

a13 = str2num(tline3(pos3(2)+4:pos3(3)+3));%Storing Thirteenth coefficient

a14 = str2num(tline3(pos3(3)+4:pos3(4)+3));%Storing Fourteenth coefficient

% specific heat

cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,tmid,T,R);%Calling Specific_heat function and assigning output values to cp

% enthalpy

H = enthalpy_H(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,tmid,R);%Calling enthalpy_h function and assigning output values to H

% entropy

S = entropy_S(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,tmid,T,R);%Calling entropy_s function and assigning output values to S

% changing directory

% creating newfolder for species

mkdir(species)

% changing directory

cd(species)

% plotting

figure(1)%Plotting first figure

plot(T,cp)%Plot between T and cp

xlabel('Temperature')%Marking x axis as 'Temperature'

ylabel('Specific heat')%Marking y axis as 'Specific Heat'

title('Specific heat vs Temperature of ',sprintf('%s',species)) %Giving title name and sprint f command is used to return string of species name

saveas(figure(1),'cp vs T.jpg') %save figure(1) as 'cp vs T.jpg'

figure(2)%Plotting Second figure

plot(T,H)%Plot between T and H

xlabel('Temperature')%Marking x axis as 'Temperature'

ylabel('Enthalpy')%Marking y axis as 'Enthalpy'

title('Enthalpy vs Temperature of ',sprintf('%s',species))%Giving title name and sprint f command is used to return string of species name

saveas(figure(2),'H vs T.jpg')%save figure(2) as 'H vs T.jpg'

figure(3)%Plotting Third figure

plot(T,S)%Plot between T and S

xlabel('Temperature')%Marking x axis as 'Temperature'

ylabel('Entropy')%Marking y axis as 'Entropy'

title('Entropy vs Temperature of ',sprintf('%s',species))%Giving title name and sprint f command is used to return string of species name

saveas(figure(3),'S vs T.jpg')%save figure(3) as 'S vs T.jpg'

% coming back to program directory

cd ..

% calculating molecular weight of species

M = mol_wt(species);

end

% PROJECT: File Parsing - NASA thermodynamic data
clear all; % clear all variables in workspace 
close all;% close all plots 
clc;%clear command window
% opening file
f1 = fopen('THERMO.dat','r'); %Initialising to f1 the Thermo.dat values and assigning in read mode property

% skipping first line
% a=fgetl(f1);
% 
% % global temperatures
% temps = fgetl(f1);
% temps = str2num(temps);
% 
% global_low_temp = temps(1);
% global_mid_temp = temps(2);
% global_high_temp = temps(3);

% creating array for temperature
% T = linspace(global_low_temp,global_high_temp,500);

% universal gas constant
R = 8.314;

% skipping next 3 lines
for i=1:5
    fgetl(f1);% calling next line
end

% storing starts
%Taking data of 53 species in file
for i=1:53

    % creating array for species
    tline = fgetl(f1); %Initialising tline with first line after 5 lines
    A = strsplit(tline,' ');%Strings are split with ' ' and saved to A
    % species
    species = A{1};%assigning  A{1} with Species name,{} are used to understand same set of variables
    
    %local temperatures
    tlow = str2num(A{end-3}); % Assigning t low by converting string to number at respected psoition
    thigh = str2num(A{end-2}); % Assigning t high by converting string to number at respected psoition
    tmid = str2num(A{end-1}); % Assigning t midby converting string to number at respected psoition

    T = linspace(tlow,thigh,500); %Initilaising T with tlow and thigh as end limits and divide into 500 equal divisions

    % finding coefficients
    % line 1
    tline1 = fgetl(f1); %Initializing tline1 with next line
    pos1 = findstr(tline1,'E'); %Findinding position of 'E' in the lines and storing location in pos1
    
    % higher coefficients
    a1 = str2num(tline1(1:pos1(1)+3));%Storing First coefficient
    a2 = str2num(tline1(pos1(1)+4:pos1(2)+3));%Storing Second coefficient
    a3 = str2num(tline1(pos1(2)+4:pos1(3)+3));%Storing Third coefficient
    a4 = str2num(tline1(pos1(3)+4:pos1(4)+3));%Storing Fourth coefficient
    a5 = str2num(tline1(pos1(4)+4:pos1(5)+3));%Storing Fifth coefficient
    
    % line 2
    tline2 = fgetl(f1);%Moving to next line
    pos2 = findstr(tline2,'E');%Storing position of 'E' in pos2
    a6 = str2num(tline2(1:pos2(1)+3));%Storing Sixth coefficient
    a7 = str2num(tline2(pos2(1)+4:pos2(2)+3)); %Storing Seventh coefficient
    
    % lower coefficients
    a8 = str2num(tline2(pos2(2)+4:pos2(3)+3)); %Storing Ninenth coefficient
    a10 = str2num(tline2(pos2(4)+4:pos2(5)+3)); %Storing Tenth coefficient
    % line 3
    tline3 = fgetl(f1);%Moving to next line
    pos3 = findstr(tline3,'E'); %Storing position of 'E' in pos3
    a11 = str2num(tline3(1:pos3(1)+3)); %Storing eleventh coefficient
    a12 = str2num(tline3(pos3(1)+4:pos3(2)+3)); %Storing Twelth coefficient
    a13 = str2num(tline3(pos3(2)+4:pos3(3)+3));%Storing Thirteenth coefficient
    a14 = str2num(tline3(pos3(3)+4:pos3(4)+3));%Storing Fourteenth coefficient
    
    % specific heat
    cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,tmid,T,R);%Calling Specific_heat function and assigning output values to cp

    % enthalpy
    H = enthalpy_H(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,tmid,R);%Calling enthalpy_h function and assigning output values to H

    % entropy
    S = entropy_S(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,tmid,T,R);%Calling entropy_s function and assigning output values to S
    
    % changing directory

    
    % creating newfolder for species
    mkdir(species)

    % changing directory
    cd(species)
    
    % plotting

    figure(1)%Plotting first figure
    plot(T,cp)%Plot between T and cp
    xlabel('Temperature')%Marking x axis as 'Temperature'
    ylabel('Specific heat')%Marking y axis as 'Specific Heat'
    title('Specific heat vs Temperature of ',sprintf('%s',species)) %Giving title name and sprint f command is used to return string of species name
    saveas(figure(1),'cp vs T.jpg') %save figure(1) as 'cp vs T.jpg'

    figure(2)%Plotting Second figure
    plot(T,H)%Plot between T and H
    xlabel('Temperature')%Marking x axis as 'Temperature'
    ylabel('Enthalpy')%Marking y axis as 'Enthalpy'
    title('Enthalpy vs Temperature of ',sprintf('%s',species))%Giving title name and sprint f command is used to return string of species name
    saveas(figure(2),'H vs T.jpg')%save figure(2) as 'H vs T.jpg'

    figure(3)%Plotting Third figure
    plot(T,S)%Plot between T and S
    xlabel('Temperature')%Marking x axis as 'Temperature'
    ylabel('Entropy')%Marking y axis as 'Entropy'
    title('Entropy vs Temperature of ',sprintf('%s',species))%Giving title name and sprint f command is used to return string of species name
    saveas(figure(3),'S vs T.jpg')%save figure(3) as 'S vs T.jpg'
    
    % coming back to program directory
    cd ..
    
    % calculating molecular weight of species
    M = mol_wt(species);

end

FUNCTION CODE:1

function [Cp]=Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,tmid,temp,r)

%First 7 coefficients are high temperature coefficients and next 7 are low

%temperature coefficients

if(temp<=tmid)

Cp=r*(x8+(x9*temp)+(x10*(temp).^2)+(x11*(temp).^3)+(x12*(temp).^4));

else

Cp=r*(x1+(x2*temp)+(x3*(temp).^2)+(x4*(temp).^3)+(x5*(temp).^4));

end

end

function [Cp]=Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,tmid,temp,r)

%First 7 coefficients are high temperature coefficients and next 7 are low
%temperature coefficients
if(temp<=tmid)
    Cp=r*(x8+(x9*temp)+(x10*(temp).^2)+(x11*(temp).^3)+(x12*(temp).^4));
else
    Cp=r*(x1+(x2*temp)+(x3*(temp).^2)+(x4*(temp).^3)+(x5*(temp).^4));
end
end

FUNCTION CODE:2

function [H]=enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,tmid,r)

%First 7 coefficients are high temperature coefficients and next 7 are low

%temperature coefficients

if(temp<=tmid)

H=r*temp.*(x8+((x9*temp)/2)+((x10*(temp).^2)/3)+((x11*(temp).^3)/4)+((x12*(temp).^4)/5)+((x13./temp)));

else

H=r*temp.*(x1+((x2*temp)/2)+((x3*(temp).^2)/3)+((x4*(temp).^3)/4)+((x5*(temp).^4)/5)+((x6./temp)));

end

end

function [H]=enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,tmid,r)

%First 7 coefficients are high temperature coefficients and next 7 are low
%temperature coefficients
if(temp<=tmid)
    H=r*temp.*(x8+((x9*temp)/2)+((x10*(temp).^2)/3)+((x11*(temp).^3)/4)+((x12*(temp).^4)/5)+((x13./temp)));
else
    H=r*temp.*(x1+((x2*temp)/2)+((x3*(temp).^2)/3)+((x4*(temp).^3)/4)+((x5*(temp).^4)/5)+((x6./temp)));
end
end

FUNCTION CODE:3

function [S]=entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,tmid,temp,r)

%First 7 coefficients are high temperature coefficients and next 7 are low

%temperature coefficients

if(temp<=tmid)

S=(x8*log(temp)+(x9*temp)+(x10*(temp.^2)/2)+(x11*(temp.^3)/3)+(x12*(temp.^4)/4)+(x14))*r;

else

S=(x1*log(temp)+(x2*temp)+(x3*(temp.^2)/2)+(x4*(temp.^3)/3)+(x5*(temp.^4)/4)+(x7))*r;

end

end

function [S]=entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,tmid,temp,r)

%First 7 coefficients are high temperature coefficients and next 7 are low
%temperature coefficients
if(temp<=tmid)
    S=(x8*log(temp)+(x9*temp)+(x10*(temp.^2)/2)+(x11*(temp.^3)/3)+(x12*(temp.^4)/4)+(x14))*r;
else
    S=(x1*log(temp)+(x2*temp)+(x3*(temp.^2)/2)+(x4*(temp.^3)/3)+(x5*(temp.^4)/4)+(x7))*r;
end
end

FUNCTION CODE:4

%function to calculate molecular mass

function mw=mol_wt(sp_name)

%storing value

compound_name=(sp_name);

%creating an array of elements

key_at=['C','H','O','N','A'];

%storing atomic mass value in the array

value_at=[12 1 16 14 40];

%assigning molecular mass as 0

mw=0;

%creating loop

for i=1:length(compound_name)

for j=1:length(key_at)

%comparing string value

if strcmp(compound_name(i),key_at(j))

%if above comparison is true, value of molecular mass is

%calculated

mw=mw+value_at(j);

%storing the position for further use

pos=j;

end

end

%converting the string to number

k=str2num(compound_name(i));

%comparing if the value of k is greater than executed, the further

%statements

if(k>1)

%molecular mass calculation

mw=mw+value_at(pos)*(k-1);

end

end

%command to print the molecular mass of the molecule

fprintf('Molecular weight of the species %s is %16.0f \n',compound_name,mw)

end

%function to calculate molecular mass
function mw=mol_wt(sp_name)

%storing value
compound_name=(sp_name);

%creating an array of elements
key_at=['C','H','O','N','A'];

%storing atomic mass value in the array
value_at=[12 1 16 14 40];

%assigning molecular mass as 0
mw=0;

%creating loop
for i=1:length(compound_name)
    for j=1:length(key_at)
        
        %comparing string value
        if strcmp(compound_name(i),key_at(j))
            
            %if above comparison is true, value of molecular mass is
            %calculated
            mw=mw+value_at(j);
            
            %storing the position for further use
            pos=j;
        end
    end
    
    %converting the string to number
    k=str2num(compound_name(i));
    
    %comparing if the value of k is greater than executed, the further
    %statements
    if(k>1)
        
        %molecular mass calculation
        mw=mw+value_at(pos)*(k-1);
    end
end

%command to print the molecular mass of the molecule
fprintf('Molecular weight of the species %s is %16.0f \n',compound_name,mw)
end
           

SYNTAX FOR PROGRAM WITH EXPLANATION:

<-- opening file

f1 = fopen('THERMO.dat','r'); <--Initialising to f1 the Thermo.dat values and assigning in read mode property

<-- skipping first line

<-- a=fgetl(f1);

<--

<-- <-- global temperatures

<-- temps = fgetl(f1);

<-- temps = str2num(temps);

<--

<-- global_low_temp = temps(1);

<-- global_mid_temp = temps(2);

<-- global_high_temp = temps(3);

<-- creating array for temperature

<-- T = linspace(global_low_temp,global_high_temp,500);

<-- universal gas constant

R = 8.314;

<-- skipping next 3 lines

for i=1:5

fgetl(f1);<-- calling next line

end

<-- storing starts

<--Taking data of 53 species in file

for i=1:53

<-- creating array for species

tline = fgetl(f1); <--Initialising tline with first line after 5 lines

A = strsplit(tline,' ');<--Strings are split with ' ' and saved to A

<-- species

species = A{1};<--assigning A{1} with Species name,{} are used to understand same set of variables

<--local temperatures

tlow = str2num(A{end-3}); <-- Assigning t low by converting string to number at respected psoition

thigh = str2num(A{end-2}); <-- Assigning t high by converting string to number at respected psoition

tmid = str2num(A{end-1}); <-- Assigning t midby converting string to number at respected psoition

T = linspace(tlow,thigh,500); <--Initilaising T with tlow and thigh as end limits and divide into 500 equal divisions

<-- finding coefficients

<-- line 1

tline1 = fgetl(f1); <--Initializing tline1 with next line

pos1 = findstr(tline1,'E'); <--Findinding position of 'E' in the lines and storing location in pos1

<-- higher coefficients

a1 = str2num(tline1(1:pos1(1)+3));<--Storing First coefficient

a2 = str2num(tline1(pos1(1)+4:pos1(2)+3));<--Storing Second coefficient

a3 = str2num(tline1(pos1(2)+4:pos1(3)+3));<--Storing Third coefficient

a4 = str2num(tline1(pos1(3)+4:pos1(4)+3));<--Storing Fourth coefficient

a5 = str2num(tline1(pos1(4)+4:pos1(5)+3));<--Storing Fifth coefficient

<-- line 2

tline2 = fgetl(f1);<--Moving to next line

pos2 = findstr(tline2,'E');<--Storing position of 'E' in pos2

a6 = str2num(tline2(1:pos2(1)+3));<--Storing Sixth coefficient

a7 = str2num(tline2(pos2(1)+4:pos2(2)+3)); <--Storing Seventh coefficient

<-- lower coefficients

a8 = str2num(tline2(pos2(2)+4:pos2(3)+3)); <--Storing Ninenth coefficient

a10 = str2num(tline2(pos2(4)+4:pos2(5)+3)); <--Storing Tenth coefficient

<-- line 3

tline3 = fgetl(f1);<--Moving to next line

pos3 = findstr(tline3,'E'); <--Storing position of 'E' in pos3

a11 = str2num(tline3(1:pos3(1)+3)); <--Storing eleventh coefficient

a12 = str2num(tline3(pos3(1)+4:pos3(2)+3)); <--Storing Twelth coefficient

a13 = str2num(tline3(pos3(2)+4:pos3(3)+3));<--Storing Thirteenth coefficient

a14 = str2num(tline3(pos3(3)+4:pos3(4)+3));<--Storing Fourteenth coefficient

<-- specific heat

cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,tmid,T,R);<--Calling Specific_heat function and assigning output values to cp

<-- enthalpy

H = enthalpy_H(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,tmid,R);<--Calling enthalpy_h function and assigning output values to H

<-- entropy

S = entropy_S(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,tmid,T,R);<--Calling entropy_s function and assigning output values to S

<-- changing directory

<-- creating newfolder for species

mkdir(species)

<-- changing directory

cd(species)

<-- plotting

figure(1)<--Plotting first figure

plot(T,cp)<--Plot between T and cp

xlabel('Temperature')<--Marking x axis as 'Temperature'

ylabel('Specific heat')<--Marking y axis as 'Specific Heat'

title('Specific heat vs Temperature of ',sprintf('<--s',species)) <--Giving title name and sprint f command is used to return string of species name

saveas(figure(1),'cp vs T.jpg') <--save figure(1) as 'cp vs T.jpg'

figure(2)<--Plotting Second figure

plot(T,H)<--Plot between T and H

xlabel('Temperature')<--Marking x axis as 'Temperature'

ylabel('Enthalpy')<--Marking y axis as 'Enthalpy'

title('Enthalpy vs Temperature of ',sprintf('<--s',species))<--Giving title name and sprint f command is used to return string of species name

saveas(figure(2),'H vs T.jpg')<--save figure(2) as 'H vs T.jpg'

figure(3)<--Plotting Third figure

plot(T,S)<--Plot between T and S

xlabel('Temperature')<--Marking x axis as 'Temperature'

ylabel('Entropy')<--Marking y axis as 'Entropy'

title('Entropy vs Temperature of ',sprintf('<--s',species))<--Giving title name and sprint f command is used to return string of species name

saveas(figure(3),'S vs T.jpg')<--save figure(3) as 'S vs T.jpg'

<-- coming back to program directory

cd ..

<-- calculating molecular weight of species

M = mol_wt(species);

end

SYNTAX FOR FUNCTION 1 WITH EXPLANATION:

function [Cp]=Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,tmid,temp,r)

<--First 7 coefficients are high temperature coefficients and next 7 are low

<--temperature coefficients

if(temp<=tmid)

Cp=r*(x8+(x9*temp)+(x10*(temp).^2)+(x11*(temp).^3)+(x12*(temp).^4));

else

Cp=r*(x1+(x2*temp)+(x3*(temp).^2)+(x4*(temp).^3)+(x5*(temp).^4));

end

end

SYNTAX FOR FUNCTION 2 WITH EXPLANATION:

function [H]=enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,tmid,r)

ßFirst 7 coefficients are high temperature coefficients and next 7 are low

ßtemperature coefficients

if(temp<=tmid)

H=r*temp.*(x8+((x9*temp)/2)+((x10*(temp).^2)/3)+((x11*(temp).^3)/4)+((x12*(temp).^4)/5)+((x13./temp)));

else

H=r*temp.*(x1+((x2*temp)/2)+((x3*(temp).^2)/3)+((x4*(temp).^3)/4)+((x5*(temp).^4)/5)+((x6./temp)));

end

end

SYNTAX FOR FUNCTION 3 WITH EXPLANATION:

function [S]=entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,tmid,temp,r)

ßFirst 7 coefficients are high temperature coefficients and next 7 are low

ßtemperature coefficients

if(temp<=tmid)

S=(x8*log(temp)+(x9*temp)+(x10*(temp.^2)/2)+(x11*(temp.^3)/3)+(x12*(temp.^4)/4)+(x14))*r;

else

S=(x1*log(temp)+(x2*temp)+(x3*(temp.^2)/2)+(x4*(temp.^3)/3)+(x5*(temp.^4)/4)+(x7))*r;

end

end

SYNTAX FOR FUNCTION 4 WITH EXPLANATION:

<--function to calculate molecular mass

function mw=mol_wt(sp_name)

<--storing value

compound_name=(sp_name);

<--creating an array of elements

key_at=['C','H','O','N','A'];

<--storing atomic mass value in the array

value_at=[12 1 16 14 40];

<--assigning molecular mass as 0

mw=0;

<--creating loop

for i=1:length(compound_name)

for j=1:length(key_at)

<--comparing string value

if strcmp(compound_name(i),key_at(j))

<--if above comparison is true, value of molecular mass is

<--calculated

mw=mw+value_at(j);

<--storing the position for further use

pos=j;

end

end

<--converting the string to number

k=str2num(compound_name(i));

<--comparing if the value of k is greater than executed, the further

<--statements

if(k>1)

<--molecular mass calculation

mw=mw+value_at(pos)*(k-1);

end

end

<--command to print the molecular mass of the molecule

fprintf('Molecular weight of the species <--s is <--16.0f \n',compound_name,mw)

end

CONCLUSION:

Molecular weight of the species O is 16

Molecular weight of the species O2 is 32

Molecular weight of the species H is 1

Molecular weight of the species H2 is 2

Molecular weight of the species OH is 17

Molecular weight of the species H2O is 18

Molecular weight of the species HO2 is 33

Molecular weight of the species H2O2 is 34

Molecular weight of the species C is 12

Molecular weight of the species CH is 13

Molecular weight of the species CH2 is 14

Molecular weight of the species CH2(S) is 14

Molecular weight of the species CH3 is 15

Molecular weight of the species CH4 is 16

Molecular weight of the species CO is 28

Molecular weight of the species CO2 is 44

Molecular weight of the species HCO is 29

Molecular weight of the species CH2O is 30

Molecular weight of the species CH2OH is 31

Molecular weight of the species CH3O is 31

Molecular weight of the species CH3OH is 32

Molecular weight of the species C2H is 25

Molecular weight of the species C2H2 is 26

Molecular weight of the species C2H3 is 27

Molecular weight of the species C2H4 is 28

Molecular weight of the species C2H5 is 29

Molecular weight of the species C2H6 is 30

Molecular weight of the species CH2CO is 42

Molecular weight of the species HCCO is 41

Molecular weight of the species HCCOH is 42

Molecular weight of the species H2CN is 28

Molecular weight of the species HCN is 27

Molecular weight of the species HNO is 31

Molecular weight of the species N is 14

Molecular weight of the species NNH is 29

Molecular weight of the species N2O is 44

Molecular weight of the species NH is 15

Molecular weight of the species NH2 is 16

Molecular weight of the species NH3 is 17

Molecular weight of the species NO is 30

Molecular weight of the species NO2 is 46

Molecular weight of the species HCNO is 43

Molecular weight of the species HOCN is 43

Molecular weight of the species HNCO is 43

Molecular weight of the species NCO is 42

Molecular weight of the species CN is 26

Molecular weight of the species HCNN is 41

Molecular weight of the species N2 is 28

Molecular weight of the species AR is 40

Molecular weight of the species C3H8 is 44

Molecular weight of the species C3H7 is 43

Molecular weight of the species CH3CHO is 44

Molecular weight of the species CH2CHO is 43

>>

SCREENSHOTS OF FOLDER







GOOGLE DRIVE LINK TO ALL OUTPUT FOLDER:

https://drive.google.com/drive/folders/1bIxqV3LdmZXlv6PWLEmKAEamcaYJzMYC?usp=sharing

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.