Skip to main content

Section 16.1 MATLAB Built-In Function

As mentioned above, a MATLAB function is very similar to a mathematical function. You have already been using MATLAB functions throughout this book! I have been careful to distinguish functions from other bits of code by appending a set of parenthesis on the end whenever we are discussing functions (i.e. sin()). Let’s investigate a few MATLAB built-in functions to understand how functions work:
Try It!
It is worth taking a moment and reading the help text for the 3 functions listed above. To do so, open MATLAB and type in help function_name into the command window. You should replace function_name with the function of interest (e.g. help max). You do not need to memorize anything but go ahead and read through the help texts to get an idea of how they work.
sin() Function
The sin() function is perhaps the easiest to understand because you are likely already familiar with trigonometric functions. The sin() function takes an input, angle in radians, and returns an output, the ratio of the length of the side of a right triangle that is opposite that angle to the length of the longest side of the triangle.
An example of how to use the sin() function would look like this (make sure you are trying it for yourself in MATLAB!):

Example 16.1.1. sin() Built-In Function.

Here is an interactive code box that functions identically to the MATLAB command window.
Notice how the output of the sin() function is assigned to the ratio variable.
Now the real question is, how does MATLAB calculate that ratio? Well, MATLAB engineers have created thousands of numerical algorithms stored in functions that they supply with the product. This brings us to the central idea behind functions: functions exist to make it easy to run snippets of code with different inputs.
Key Concept: Functions exist to make it easy to run snippets of code with different inputs.
An example would be the sine trigonometric function. Most engineers and scientists do not want to have to program the algorithm that calculates the sine of an angle every time they need to calculate sine. That is too much work! So you can create a function that calculates the sine of an angle, name it something that makes sense (in this case sin()), and allow it to work for any input!
mean() Function
Recall that the mean() function accepts an array as in input, and returns the average value of the values inside the array as the output.
An example of how to use the mean() function could look like this:

Example 16.1.2. mean() Built-In Function.

Here is an interactive code box that functions identically to the MATLAB command window.
When you try this example, you should notice that the value stored in the variable average is 4.8, the mean of the array! Keep in mind that you could have used a few different functions to calculate the exact same value. Recall: (\(average=\frac{sum}/{count}\)). Therefore:
avg_long_way = sum(array)/length(array)
Again give it a shot. You will notice that the value in avg is the same as avg_long_way which is unsurprising. However, you can imagine that if you had to constantly type out the long way, it would get tedious. The other thing to consider is that sum() and length() are functions themselves! Take those away and this problem of calculating a mean gets really complicated.
Key Concept: Functions make your code easier to read and easier to type.
The key concept here is that functions make your code easier to read and easier to type. So when you have an algorithm that is likely to get run over and over again, it is a good idea to make a function for it!

Checkpoint 16.1.3. Checking Equality of Variables.

max() Function
Next up, lets consider the built-in function max(). This particular function is interesting because of its flexibility. The function accepts an array as an input and returns the maximum value in that array as its output. That in itself is cool, but not that exciting (hopefully you read the help text for max() and know where this is going). The exciting part is that max() can optionally return the index where the maximum value is located within the array!
To see this in action, let’s try an example. We can use the array variable still located in our workspace from the previous example:

Example 16.1.4. max() Built-In Function.

Here is an interactive code box that functions identically to the MATLAB command window.
That will store the number 9 inside the biggest variable. Cool, but not exciting. Take an account of your workspace variables. Now try:
[maximum, index] = max(array)
Look at your workspace now. Notice that it created two new variables, one called maximum that has the number 9 stored in. The execution of that code also created a new variable, index with the number 1 stored in it. The 9 is the biggest number in the array, and the 1 is the index of where the number is located.
The first key concept to learn from this example is that MATLAB functions can accept multiple inputs and can return any number of outputs.
Key Concept: Functions can accept multiple inputs and can return any number of outputs.
To be specific, when you are creating a function:
  • You can require any number of inputs. For example, you can require your user to specify at least one array, or you can say that a minimum of two arrays is required, etc.
  • You can make any number of inputs optional. For example, you can have one required, and one optional input.
  • The only output that is not optional is the first output. All other outputs from a function are optional. That means that you can not force your user to accept two outputs from your function. This is an important thing to consider when designing your own functions.
Let’s continue with the max() function to investigate our next key concept. If you have been doing your brain workouts, you read through the max() help text. See figure 16.1.5 below, specifically the highlighted region.
Figure 16.1.5. A portion of the MATLAB help text for max().
Notice how well this function is described! It shows an example of how to use the function ([M,I] = max(X)) and gives a nice description of how it works.
The second key concept is that well-written help text is critical to writing a good function. If the documentation for this function was poorly written or confusing, no one would use it!
Key Concept: Well-written help text is critical to writing a good function.
A good function is one that is well documented, not only for your users but for yourself! Variable names and algorithms always make sense when you are working on them, but when you put them down for a while, you need good documentation to ensure that your code is still usable.
You have attempted of activities on this page.