Week 2 - MATLAB Functions and Applications in Data Analysis

MATLAB functions allow creation of reusable code that can be assembled into larger and more complex analysis. Even if you are using relatively simple analysis, your code will be easier to write and maintain if you use functions.

In this class meeting we will build skills with creating and using functions. Before getting into functions in detail, we will first review the basic numeric data types used in MATLAB: scalars, vectors and matrices. It's important to understand data types because functions are usually designed to accept specific types of variables as inputs and return specific types of variables as outputs.

Reading

The chapter sections are the same for the 5th and 6th edition of the textbook. There is a slight difference in page numbers in the reading from Chapter 7

  • Chapter 2, pp. 33 - 51, which is a repeat from week 1 plus pp. 44 - 51
  • Chapter 7, Sections 7.1 - 7.2.1, pp. 161 - 168 5th edition or pp. 161-171 in 6th edition:

Don't get hung up on the Newton's method script at bottom of p. 162. It uses programming constructs while, if-else-end that we have not discussed yet. We will cover these constructs and it would be good to read and think about that code. At least, write and test the f.m and df.m functions from the middle of page 162. Start reading carefully on page 163.

  • Chapter 9, pp. 197 - 205, 230 - 231: Sections 9.1.1-9.1.10, 9.8

The material from Chapter 7 focuses on the technical requirements of function input and output parameters. The material from Chapter 9 focuses on plotting.

Lecture Notes and Slides


Four-step process to using numerical methods in MATLAB

Very often, when using MATLAB to solve engineering problems, we use a four-step process

  1. Given a practical engineering problem, formulate a model of a practical problem that can be solved numerically. Select an appropriate numerical method
  2. Write a small program to set-up or define the problem to be solved.
  3. Write another small program that passes the problem-defition code developed in step 2 to a built-in MATLAB program that solves the problem
  4. Use the numerical solution returned in step 3 to answer the engineering problem.

Step 1 is often the hardest. It gets easier with engineering and problem-solving experience.

Steps 2 and 3 require an understanding of the problem being solved, an understanding of the numerical method being used, and skill at writing what are usually short MATLAB programs. As your skill with MATLAB programming increases, steps 2 and 3 become easier. In ME 350 you should make a lot of progress developing the skills to help with steps 2 and 3. In addition, I will provide examples of steps 2 and 3 for the numerical methods we study in ME 350.

Step 4 can be easy or hard, depending on the problem. Sometimes, the numerical solution is a value that can be directly used in in engineering decisions. Examples of this directly usable information are values of the stress in a mechanical part, or the pressure drop in a piping system, or the torque needed to drive a mechanism. Other times, the results of the numerical analysis lead to more questions, and possibly a need to rethink or repeat the problem-solving process with a different model or a different combination of physical parameters.

At this point in ME 350, one key idea in the four-step process is the importance of step 2, that is, to be able to write MATLAB functions that describe the problem being solved. Often these functions are short and amount to evaluating one or more engineering formulas.

Because step 2 is crucial to the four step process outlined above, and because writing MATLAB functions is central to completing step 2, we will be continuously practice writing functions.

The Importance of Writing MATLAB Functions

As described in the preceding section, being able to write MATLAB functions is a crucial skill in using numerical methods in MATLAB.

  1. MATLAB's built-in numerical methods often require you to write a small function that describes the problem you want to solve.

  2. In general, writing functions with well-defined input and output parameters enables you to create modular solutions to larger problems. Writing modular code is good technique with many benefits.

  3. Creating functions to perform specific task creates an abstract (or somewhat abstract) version of the problem at hand. Working at a more abstract level allows you to see the structure of the problem being solved.

  4. When specific tasks are isolated into functions, it becomes easier and less error prone to rearrange the main flow of the analysis or to substitute components with different capabilities, efficiency, accuracy.

  5. Functions can often be reused. This saves time in the future.

  6. Functions can often be improved in isolation from the larger problem. For example, creating a more efficient or more accurate version of a function without changing the input and output parameters to that function allows the benefits to propagate to any analysis that uses that function. Furthermore, as long as the input and output parameters do not change the benefits are obtained with no further change in the main program.

Refer to the Lecture Slides (via the links above).

Numeric Data Types in MATLAB

In order to write MATLAB functions, you need to understand how to define and manipulate MATLAB variables.

  • All variables are matrices
  • A scalar is a matrix with one row and one column
  • A row vector is matrix with one row and columns
  • A column vector is a "matrix" with rows and one column
  • In general a matrix has rows and columns.

Matrices do not need to be square, i.e. it is OK to have .


Document updated 2016-10-03.

Go back to the Lab page.