Introduction to MATLAB Programming II
Grady Wright
Department of Mathematics
University of Utah
Based on a similar idea from
Kristian Sandberg
Department of Applied Mathematics
University of Colorado
In this tutorial, we will study more advanced MATLAB programming. The main goal of this tutorial is to demonstrate how to write functions in MATLAB. In the previous tutorial, MATLAB Programming I, we learned how to write very simple programs that could, at best, request input from the user. When writing larger programs, it is necessary split the problem up into functions that can be passed input values and return the necessary output values. This is what we cover in this tutorial. Note that, it is important to implement the examples of this tutorial, run them, and carefully compare the output with the code.
Functions in MATLAB
You have already encountered functions that are built in to MATLAB. The sin() is a function that takes an argument and returns an output. As you start to write your own programs in MATLAB you will need to create your own functions that take one or more input arguments, operate on them, and return the result. For example, when you write your own non-linear root finder (e.g. Newton's method), you will most certainly need a loop that calls the non-linear function and it's derivative several times. It is certainly possible to write this function explicitly in your loop. However, that is not good programming practice since you would have to modify your general solver for every new specific function you want to input.
To introduce programming with functions in MATLAB, we will use the following example: Create a general program for graphing a given function over a given range.
Preliminaries
Before we begin, we must first look at the syntax for a function in MATLAB.
function y=<function_name>(argument list)commands
The code above must be written in a separate m-file! The name of the file should coincide with the name of the function, i.e. <function_name>.m (remember you must save the file with a .m after the file name.
We illustrate the syntax for the body of a MATLAB function by implementing the mathematical function
f(x)= x2 + (1 - exp(x))2-4
in a MATLAB function.
Example 1.
% Filename: f.mfunction y = f(x)
y = x.^2 + (1 - exp(x)).^2 - 4; % Note the use of the dot in the formula
This code must be saved in your working directory as a separate m-file with the name f.m. Note how we have not used the clear command. This is because all variables inside functions are local to that function (unless specified otherwise by using the GLOBAL command).
Programming with Functions
Now, we illustrate how we can actually program with functions by solving the problem posed by the above example. We want a general MATLAB function that plots a given mathematical function over the domain [a,b]. This can be accomplished with the following MATLAB function.
% Filename: fplotter.mfunction fplotter(g,a,b)
x = linspace(a,b,501); % x-values over the range [a,b] at which to evaluate g
y = feval(g,x); % evaluate the function g at all the values in x
plot(x,y); % plot the function g
xlabel('x'); % label the x-axis
ylabel('f(x)'); % label the y-axis
This function must be saved as a separate m-file with the name fplotter.m. It must also be saved in the same directory as f.m. Note that this is a very naive implementation of the function plotter. If we were going to make it more robust, we would want to put in code for checking the range of (a,b) and checking whether or not the function g exists. Also, it would be advantageous to base the number of points that are used to divide up the plot interval on the values of a and b.
To plot the function in f.m using fplotter, first go to the MATLAB command window then change to the working directory that corresponds to the directory containing the two functions. Next, type the command
fplotter('f',-1.5,1.5)
This will call the function fplotter with the function f and plot it over the range [-1.5,1.5]. Make sure you understand how this example works! It will help you to write a non-linear root finder.
Exercise 1.Make a plot of the function exp(-x^2)*cos(x) for x between -4 and 4. (Hint: Do you need to change the file fplotter.m at all?)
Note that it is possible to have functions return more than one value.
1