Challenge

Project 1 - Parsing NASA thermodynamic data

Aim and Objective:



This project aims to develop a MATLAB program that can extract thermodynamic coefficients from NASA thermodynamic data and use them to calculate and visualize thermodynamic properties such as specific heats (Cp), enthalpy (H), and entropy (S) for various species. The program will automate the process of extracting coefficients, calculating molecular weights, generating plots, and saving them as images in separate folders for each species.



Introduction:



Thermodynamic properties play a crucial role in various engineering applications, especially in the design and analysis of combustion systems, chemical processes, and aerospace vehicles. NASA has provided comprehensive data sets containing thermodynamic coefficients for over 1000 species, enabling accurate evaluation of thermodynamic properties over a wide range of temperatures.



Our project aims to leverage this data by developing a MATLAB program that can efficiently extract coefficients, calculate thermodynamic properties, and visualize them for different species. By automating these tasks, our program will streamline the process of analysing thermodynamic behaviours and facilitate insights into the performance of various substances under different conditions.



Through this project, we aim to demonstrate the utility of MATLAB in handling complex thermodynamic data, conducting calculations, and generating informative visualizations. The resulting program will provide engineers and researchers with a powerful tool for studying thermodynamic phenomena and optimizing system performance in diverse applications.

function thermodata()

% Call the main function with the filename

calculate_thermo_properties('thermo.dat');

end

function calculate_thermo_properties(filename)

% Open the file

fid = fopen(filename, 'r');

% Read the comment lines

for i = 1:5

fgetl(fid);

end

% Initialize a cell array to store species data

species_data = {};

% Extracting all species data

while ~feof(fid)

% Extracting name of gas species

species_name = fgetl(fid);

z = findstr(species_name, ' ');

species_name = species_name(1:z(1)-1);

% Extracting local temperatures of gas species

t1 = str2double(species_name((z(1)-3):z(1)+3));

t2 = str2double(species_name(z(2)-4:z(2)+3));

t3 = str2double(species_name(z(3)-4:z(3)+3));

% Extracting co-efficient of gas species

coeffs = zeros(1, 14);

for j = 1:4

line = fgetl(fid);

coeffs(j) = str2double(line(1:14));

coeffs(j+4) = str2double(line(15:28));

end

% Skip the last line

fgetl(fid);

% Store species data

species_data{end+1} = struct('name', species_name, 'coeffs', coeffs, 't1', t1, 't2', t2, 't3', t3);

end

% Close the file

fclose(fid);

% Loop through each species data and calculate properties

for i = 1:numel(species_data)

calculate_and_plot_properties(species_data{i});

end

end

function calculate_and_plot_properties(species)

% Constants

R = 8.314; % J/(mol K)

% Extract coefficients and temperature range

high_coeffs = species.coeffs(1:7);

low_coeffs = species.coeffs(8:14);

T_low = species.t1;

T_mid = species.t2;

T_high = species.t3;

% Calculate molecular weight (assuming atomic composition is provided)

% Molecular weight calculation logic can be added here

% Calculate properties over the temperature range

T = linspace(T_low, T_high);

Cp = calculate_Cp(R, high_coeffs, low_coeffs, T);

H = calculate_H(R, high_coeffs, low_coeffs, T);

S = calculate_S(R, high_coeffs, low_coeffs, T);

% Plotting

figure;

subplot(3,1,1);

plot(T, Cp);

xlabel('Temperature (K)');

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

title(['Cp vs Temperature for ', species.name]);

subplot(3,1,2);

plot(T, H);

xlabel('Temperature (K)');

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

title(['Enthalpy vs Temperature for ', species.name]);

subplot(3,1,3);

plot(T, S);

xlabel('Temperature (K)');

ylabel('Entropy (J/mol K)');

title(['Entropy vs Temperature for ', species.name]);

% Save the plot in a folder

folder_name = ['Plots_', species.name];

if ~exist(folder_name, 'dir')

mkdir(folder_name);

end

saveas(gcf, fullfile(folder_name, [species.name, '_properties_plot.png']));

end

function Cp = calculate_Cp(~, ~, ~, ~)

% Cp calculation logic

end

function H = calculate_H(~, ~, ~, ~)

% Enthalpy calculation logic

end

function S = calculate_S(~, ~, ~, ~)

% Entropy calculation logic

end

MATLAB Errors and Explanations

Common Errors in MATLAB Programming and Explanations

1. Undefined Function or Variable:

- Explanation: This error occurs when you try to use a function or variable that MATLAB doesn't recognize. It can happen if the function or variable is misspelled, not defined in the current scope, or not included in the MATLAB path.

2. Index Out of Bounds:

- Explanation: MATLAB arrays are indexed starting from 1. If you try to access an element using an index that is outside the array bounds (e.g., accessing index 6 in a 5-element array), MATLAB will throw an "Index out of bounds" error.

3. Syntax Error:

- Explanation: Syntax errors occur when your code violates MATLAB syntax rules. This can include missing parentheses, brackets, or semicolons, or using incorrect operators.

4. Mismatched End Statements:

- Explanation: MATLAB uses `end` to close loops, functions, and conditionals. Forgetting an `end` statement or having mismatched `end` statements (e.g., closing a loop before closing an if statement) can lead to errors.

5. Matrix Dimensions Mismatch:

- Explanation: MATLAB requires matrix operations to have compatible dimensions. If you try to perform operations on matrices with incompatible dimensions (e.g., adding matrices of different sizes), MATLAB will raise a dimension mismatch error.

6. Division by Zero:

- Explanation: Attempting to divide a number by zero is mathematically undefined. If your code contains division by zero (e.g., x = 5/0;), MATLAB will throw an error.

7. Function Input/Output Argument Mismatch:

- Explanation: Errors occur when the number of input or output arguments in a function call does not match the function definition. This can lead to unexpected behaviour or errors.

8. File I/O Errors:

- Explanation: Issues related to reading or writing files, such as trying to open a non-existent file, lacking proper permissions, or incorrect file paths, can result in file I/O errors.

9. Memory Issues:

- Explanation: Running out of memory due to large data structures or inefficient memory usage can lead to memory-related errors, causing MATLAB to halt execution.

10. Plotting Errors:

- Explanation: Problems with plotting data, such as incorrect data dimensions for plotting functions, missing plot elements, or invalid plot settings, can result in plotting errors in MATLAB.

Steps Taken to Overcome it.

Undefined Function or Variable:

Steps to overcome: Check the spelling and scope of the function or variable. Ensure it is defined or included in the MATLAB path.

Index Out of Bounds:

Steps to overcome: Review the indexing of arrays to ensure they are within bounds. Make sure the index starts from 1 for MATLAB arrays.

Syntax Error:

Steps to overcome: Review the code for missing parentheses, brackets, or semicolons. Ensure correct usage of operators.

Mismatched End Statements:

Steps to overcome: Check for missing or mismatched end statements. Ensure each end statement corresponds to the appropriate opening statement.

Matrix Dimensions Mismatch:

Steps to overcome: Verify the dimensions of matrices being operated on. Ensure compatibility for matrix operations.

Division by Zero:

Steps to overcome: Avoid division by zero. Ensure the denominator is not zero or implement checks to handle such cases.

Function Input/Output Argument Mismatch:

Steps to overcome: Match the number of input or output arguments in function calls with the function definition.

File I/O Errors:

Steps to overcome: Check for correct file paths, permissions, and existence of the file. Verify the file operations for correctness.

Memory Issues:

Steps to overcome: Optimize memory usage, use efficient data structures, and avoid creating excessively large data structures.

Plotting Errors:

Steps to overcome: Review the data dimensions for plotting functions. Ensure correct data formatting and plot settings.

These steps are general guidelines to address each type of error. Actual debugging may require additional steps depending on the specific context of the code and error.

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.