|
|
|
A good way to "automatically" execute
a sequence of often-used commands without having to go to the trouble of typing
them in each time is to use an m-file. An m-file is a plain text file that
you can create with any text editor. In it, you can put the same kind of commands that
you type in at a MATLAB prompt.
Typing the m-file's name at a prompt simply successively
executes the commands in the file. It is just as if you had typed them in at
individual MATLAB prompts.
d2r = pi/180
a = sin(30*d2r)
b = (1 + a)/4
tester
There will be occasions when you
will need to define your own specialized functions which are not built into
MATLAB. You do this by using a special kind of "function" m-file.
As with standard functions like sin(x), a function m-file accepts input arguments
and returns outputs.
An example function m-file is given below.
It accepts the input
argument x and returns y = fun(x). Note that the first line contains
the key word "function" and describes inputs and outputs. The output
value must be assigned within the "program" to the variable y
which appears before the equals sign on the first line of the file.
The m-file must be given the same name
as the function, namely fun.m.
User-defined functions can be plotted as easily as built-in ones. We will use the plot commands we learned in Part 5 of this Tutorial.
You can define functions that take inputs of more than one variable. Output can likewise be more than one variable. Both input and output variables can be vectors.
|
|
|
modules at math.duke.edu | Copyright CCP and the author(s), 2000 |