Link Search Menu Expand Document

Set up MATLAB for working with the course codes

Table of contents

  1. Overview
  2. Goals
  3. Download some of the course codes
  4. Launch MATLAB and configure the path
    1. Launch MATLAB from the Windows Start menu
    2. Select the path tool from the top of the main MATLAB window
    3. Add folders to the path
    4. Save the Path
    5. Repeat for other folders
  5. Summary
    1. Keeping organized

Overview

This page gives recommendations for setting up MATLAB to use the finite-difference and finite-volume codes for the course. The main goals are to create a library folder for storing the codes after downloading them, and setting up MATLAB so that code library is always included in the search path.


Goals

As a result of working with MATLAB in the first weeks of the class you should be able to

  1. Launch MATLAB and run the PDE codes
  2. Make simple modifications to the PDE codes
    • Select different methods for solving the same PDE
    • Change initial conditions
    • Change boundary condition values and types
  3. Create summary plots of trends such as
    • Dependence of results on mesh refinement
    • Convergence rate of truncation errors
    • Physical quantities of boundary heat transfer rate or overall pressure drop

Download some of the course codes

In this tutorial, we will work with one set of the course codes – the finite-difference schemes for solving the 1D heat equation. You will repeat these steps for the other sets of course codes. I strongly recommend that you store each set in a separate folder. For now, just follow the steps for the one set of codes.

Download mfiles for the 1D Heat Equation Models:

  1. Download heat1D_mfiles.zip archive
  2. Expand the zip archive in a good place. Hint: home\class\ME448\mfiles
  3. Clean up the archive
    • Delete the __MACOSX folder. It contains metadata for the Macintosh OS, which was used to create the zip archive.
    • Move files to a new heat1D folder. Using this shorter name is convenient, but not necessary.
  4. Make at least on other folder, say homework, for storing files that you create.

The goal is to create a library of files that you don’t change, and also create at least one folder of files that you create for homework or research.

Launch MATLAB and configure the path

Launch MATLAB from the Windows Start menu

Select the path tool from the top of the main MATLAB window

Select the path tool

Add folders to the path

Click Add with Subfolders …

Select add with subfolders

Navigate to the folder you created for storing mfiles. Click Select Folder.

Navigate to your mfile folder

Save the Path

Click Save to save the path

Click save to save the path

If you are working on the computers in the CECS network (e.g. in the EB 420 lab), MATLAB will give you an error message because you don’t have permission to save the pathdef.m in the directory used by the system-wide MATLAB installation.

Error message about saving the path

NOTE: You will not see this error if you are working on a computer where you have administrator privileges. For example, if you are using a student version of MATLAB installed on your personal computer, and if you are logged onto an account that allows you to install software, you will not see this error message.

Don’t panic. Click Yes. Save the pathdef.m file in your local MATLAB folder, i.e. in \Documents\MATLAB.

Saving pathdef.m in your local MATLAB folder

Now that the pathdef.m file is saved, you can close the Path Tool

Saving pathdef.m in your local MATLAB folder

Repeat for other folders

I recommend that you create other folders for MATLAB codes that you create. For example, a homework folder (or folders, one for each assignment) can store mfiles created for homework assignments. Another folder, practice could store temporary mfiles used to explore new ideas or follow along on tutorials.

Summary

The preceding steps have completed two main tasks. First, you created a folder in your account where the heat1D mfiles are stored. These mfiles are MATLAB functions that implement three different schemes for solving the one-dimensional heat equation. We will work with these codes for the first two weeks of the course.

The second main task was to configure MATLAB so that the files in the heat1D folder will always be available when you run MATLAB. The “always available” means that the heat1D folder is always in your MATLAB path, no matter what other folder you happen to be running MATLAB in.

In computer programming terms, the heat1D codes are a library that you use without changing the contents. To take advantage of this setup, you should never (or rarely ever) change the functions in the heat1D folder. Instead, you write your own MATLAB functions that use the functions in the heat1D folder.

Keeping organized

I strongly recommend that you create separate folders for each set of mfiles I have provided in the course. Those mfiles sets include almost 90 files and are organized into four groups

  • heat1D – numerical solutions to the one-dimensional heat equation
  • convection1D – numerical solution to the one-dimensional advection-diffusion equation
  • pipeflow1D – finite volume solution to fully-developed flow in a pipe
  • CVFD2D – codes implementing the finite volume method on a 2D cartesian mesh
    • Solution to the Poisson equation
    • Solution to the 2D advection-diffusion equation
    • Demonstration of iterative methods for large sparse systems of equations

Each time you download one of the zip archives, create a new folder (in your \class\ME448\mfiles folder) for the files in that zip archive.

Furthermore, create a separate folder (or more!) for your own homework and post-processing codes.