# Matlab Scripts and Functions

Matlab is at its best when used as a scripting language. Yes, it's nice to be able to throw in a couple of matrices and see what happens, but typically we want to use Matlab to solve more complicated problems, involving several methodical steps of calculations. For such problems, it is best to write a script or a function - that is: a program.

In its simplest form, a script is simply a list of Matlab commands that should be executed sequentially. For example, a simple script to plot a sine function follows:

x=-pi:.1:pi; y=sin(x); plot(x,y)

If you intended to run this often (not that you would) you could use a text editor (such as gedit) to type these commands into a file called "plotsine.m". After that, inside Matlab, you would need only to type

> plotsine

to see the plot of the sine function.

More often, scripts are used to automate and abbreviate repetitive work. For that reason it is customary to use special logic in scripts to perform tasks iteratively, and to allow the scripts to make decisions based on the values of variables. A brief summary of such "flow control" commands may be found elsewhere on this site. There is also information on "publishing" scripts to HTML elsewhere.

Sometimes we want to use a script as part of a larger computation - we
want it to manipulate a couple of arguments and return a value that we
will use for further work. When that is the aim, we usually write a *function*
instead of a simple script. The differences between Matlab functions and
scripts are summarized in the following table.

Script |
Function |

Has access to and uses all variables from the Matlab session. |
Has access to only the variables passed as arguments. |

Gives the Matlab session access to all variables it creates. |
Gives the Matlab session access to only the variables it returns. |

Basically, a script is just a shortcut for a Matlab session, saving us some typing. A function is a self-contained program that can be reused from one session to another, or even called from other scripts and functions.

Since scripts and functions are really quite similar, we need only one
line to change a script into a function. The line tells what variables
in the script are to be passed as arguments, and what variables are to
be returned to the calling program. In particular, to make a script into
a function, a line such as the following must be added to the *top*
of the script.

function [return1,return2,return3]=functionname(arg1,arg2)

You may have any number of arguments, and any number of returned variables. The returned variables must all have the same dimension. The function name can be anything you pick except a name that has already been used by Matlab - do not try to name your function "inv". For example, consider a function that takes two vectors and computes their dot product.

function prod=innerproduct(v1,v2) if size(v1)==size(v2), v1 = v1(:); v2 = v2(:); prod = v1'*v2; else error('The vector inputs must be the same size'); end

Note that this must be saved in a file called "innerproduct.m". Since the vectors v1 and v2 are not returned from the function, we can change them in any way we like inside the function without worrying that we will mess up work in the calling program (C programmers would say that the arguments are "passed by value".). Thus we feel free to be sure that both vectors are column vectors before we try to compute their inner product.

We would call this function from a Matlab session or another Matlab script simply by creating vectors by any name but with the same dimension, and then calling innerproduct(). For example, to compute the mutual dot products of three vectors, consider the following.

> x=ones(1,10); > y=1:10; > z=20:2:40; > p1 = innerproduct(x,y); > p2 = innerproduct(x,z); > p3 = innerproduct(y,z);

A solution for the
final is available.