MATLAB Lesson 4 - Functions

Estimated time required:

30 minutes

Recap

In Lesson 3 you learnt how to work with vectors, both row vectors and column vectors.

 

Functions

Functions provide an essential mechanism for grouping together a sequence of frequently used MATLAB commands to perform a specific task. Examples include all the standard mathematical functions like sin, cos, tan, exp, log, sqrt, abs and many more. MATLAB provides all the functions you would find on a scientific calculator. MATLAB also includes the ability to define your own functions. In this module we will only see the simplest way to do this by using anonymous functions. This is essential for problems like solving nonlinear equations, integrating a function, and minimizing or maximising a function. One of the great powers MATLAB is its ability apply functions to vectors (and matrices, but that will come later) with any number of elements in a single call.

Objectives

The aim of this module is to understand how to use functions and to define anonymous functions in MATLAB.

What you should know by the end of this module:

Instructions

Work your way through the pages of this lesson using the links in the blue bar above or at the bottom of each page. You should do them in order initially but you can return to any of them when you need to refresh your memory. You can print the pages by using the printable copy icon in the Resources menu bar to the right.

Hint: Have both these lessons open in a web browser and MATLAB open in another window so you can try all the examples as you work through the lessons.

It is recommended that you attempt the self-test exercises that you will find on each page. There is a quiz at the end of the module for which you will receive a grade.  You final mark for this quiz will be the highest mark you achieve before the due date for the quiz.

For further information on the topics covered in this module, read sections 2.3 and 2.6.2 of the Introduction to MATLAB notes.