Challenge

Project 1 - Parsing NASA thermodynamic data

1. Reading the Data File

function processThermoData(fileName)

% Open the file

fid = fopen(fileName, 'r');

if fid == -1

error('Error opening the file.');

end

% Read the file line by line

tline = fgetl(fid);

while ischar(tline)

% Process the line (details below)

% ...

tline = fgetl(fid);

end

% Close the file

fclose(fid);

end

2. Extracting Coefficients and Temperature Ranges

function [species, coeffs, tempRanges] = extractData(fid)

species = {};

coeffs = [];

tempRanges = [];

% Read the file line by line

tline = fgetl(fid);

while ischar(tline)

if contains(tline, 'THERMO')

% Skip the header

for i = 1:3

tline = fgetl(fid);

end

end

% Read species name

name = strtrim(tline(1:18));

species{end+1} = name;

% Read temperature ranges

tempRange = sscanf(tline(46:end), '%f %f %f');

tempRanges = [tempRanges; tempRange'];

% Read coefficients

highCoeffs = zeros(1, 7);

lowCoeffs = zeros(1, 7);

tline = fgetl(fid);

highCoeffs(1:5) = sscanf(tline, '%f %f %f %f %f');

tline = fgetl(fid);

highCoeffs(6:7) = sscanf(tline, '%f %f');

lowCoeffs(1:4) = sscanf(tline(1:52), '%f %f %f %f');

tline = fgetl(fid);

lowCoeffs(5:7) = sscanf(tline, '%f %f %f');

coeffs = [coeffs; highCoeffs, lowCoeffs];

% Read the next species

tline = fgetl(fid);

end

end

3. Calculating Thermodynamics Properties

function [Cp, H, S] = calculateThermoProperties(T, coeffs, R)

Cp = R * (coeffs(1) + coeffs(2)*T + coeffs(3)*T.^2 + coeffs(4)*T.^3 + coeffs(5)*T.^4);

H = R * T .* (coeffs(1) + coeffs(2)*T/2 + coeffs(3)*T.^2/3 + coeffs(4)*T.^3/4 + coeffs(5)*T.^4/5 + coeffs(6)./T);

S = R * (coeffs(1)*log(T) + coeffs(2)*T + coeffs(3)*T.^2/2 + coeffs(4)*T.^3/3 + coeffs(5)*T.^4/4 + coeffs(7));

end

4. Plotting and Saving the Results

function plotAndSaveResults(species, T, Cp, H, S)

for i = 1:length(species)

spName = species{i};

mkdir(spName);

figure;

plot(T, Cp);

title(['Specific Heat Capacity of ', spName]);

xlabel('Temperature (K)');

ylabel('Cp (J/(mol*K))');

saveas(gcf, fullfile(spName, 'Cp.png'));

figure;

plot(T, H);

title(['Enthalpy of ', spName]);

xlabel('Temperature (K)');

ylabel('H (J/mol)');

saveas(gcf, fullfile(spName, 'H.png'));

figure;

plot(T, S);

title(['Entropy of ', spName]);

xlabel('Temperature (K)');

ylabel('S (J/(mol*K))');

saveas(gcf, fullfile(spName, 'S.png'));

end

end

5. Main Program

function main()

fileName = 'THERMO.dat';

R = 8.314; % Universal gas constant

fid = fopen(fileName, 'r');

if fid == -1

error('Error opening the file.');

end

[species, coeffs, tempRanges] = extractData(fid);

fclose(fid);

for i = 1:length(species)

Tlow = tempRanges(i, 1);

Thigh = tempRanges(i, 2);

T = linspace(Tlow, Thigh, 100);

highCoeffs = coeffs(i, 1:7);

lowCoeffs = coeffs(i, 8:14);

[Cp, H, S] = calculateThermoProperties(T, highCoeffs, R);

plotAndSaveResults(species(i), T, Cp, H, S);

end

end

% Execute the main function

main();

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.