Function Z Yplusx (Y, X) Z y + X
Function Z Yplusx (Y, X) Z y + X
Function Z Yplusx (Y, X) Z y + X
M-files are macros of MATLAB commands that are stored as ordinary text files with
the extension "m", that is filename.m. An M-file can be either a function with input
and output variables or a list of commands. All of the MATLAB examples in this
textbook are contained in M-files that are available at the MathWorks ftp site,
ftp.mathworks.com in the directory pub/books/heck.
The M-files associated with this textbook should be downloaded from the MathWorks
ftp site and copied to a subdirectory named "\MATLAB\KAMEN" and then this
directory should be added to the path. The M-files that come with MATLAB are
already in appropriate directories and can be used from any working directory.
function z = yplusx(y,x)
z = y + x;
The following commands typed from within MATLAB demonstrate how this M-file
is used:
x = 2;
y = 3;
z = yplusx(y,x)
MATLAB M-files are most efficient when written in a way that utilizes matrix or
vector operations. Loops and if statements are available, but should be used sparingly
since they are computationally inefficient. An example of the use of the
command for is
for k=1:10,
x(k) = cos(k);
end
This creates a 1x10 vector x containing the cosine of the positive integers from 1 to
10. This operation is performed more efficiently with the commands
k = 1:10;
x = cos(k);
which utilizes a function of a vector instead of a for loop. An if statement can be used
to define conditional statements. An example is
The allowable comparisons between expressions are >=, <=, <, >, ==, and ~=.
Several of the M-files written for this textbook employ a user-defined variable which
is defined with the command input. For example, suppose that you want to run an M-
file with different values of a variable T. The following command line within the M-
file defines the value:
Whatever comment is between the quotation marks is displayed to the screen when
the M-file is running, and the user must enter an appropriate value.
Definition of Matrices
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1
matrices. Therefore, vector and matrix operations are as simple as common calculator
operations.
Vectors can be defined in two ways. The first method is used for arbitrary elements:
v = [1 3 5 7];
creates a 1x4 vector with elements 1, 3, 5 and 7. Note that commas could have been
used in place of spaces to separate the elements. Additional elements can be added to
the vector:
v(5) = 8;
a = [9 10];
b = [v a];
The second method is used for creating vectors with equally spaced elements:
t = 0:.1:10;
creates a 1x101 vector with the elements 0, .1, .2, .3,...,10. Note that the middle
number defines the increment. If only two numbers are given, then the increment is
set to a default of 1:
k = 0:10;
M = [1 2 4; 3 6 8];
null matrix: M = [ ];
nxm matrix of zeros: M = zeros(n,m);
nxm matrix of ones: M = ones(n,m);
nxn identity matrix: M = eye(n);
M(1,2) = 5;
In this text, matrices are used only in Chapter 12; however, vectors are used
throughout the text. Operations and functions that were defined for scalars in the
previous section can also be used on vectors and matrices. For example,
a = [1 2 3];
b = [4 5 6];
c=a+b
yields:
c = 5 7 9
t = 0:10;
x = cos(2*t);
t = 0:10;
x = t.*cos(t);
A. Definition of Variables
Variables are assigned numerical values by typing the expression directly, for
example, typing
a = 1+2
yields: a = 3
The answer will not be displayed when a semicolon is put at the end of an expression,
for example type a = 1+2;.
MATLAB utilizes the following arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
^ power operator
' transpose
A variable can be assigned using a formula that utilizes these operators and either
numbers or previously defined variables. For example, since a was defined previously,
the following expression is valid
b = 2*a;
To determine the value of a previously defined quantity, type the quantity by itself:
yields: b = 6
If your expression does not fit on one line, use an ellipsis (three or more periods at the
end of the line) and continue on the next line.
c = 1+2+3+...
5+6+7;
There are several predefined variables which can be used at any time, in the same
manner as user-defined variables:
i sqrt(-1)
j sqrt(-1)
pi 3.1416...
For example,
y= 2*(1+4*j)
There are also a number of predefined functions that can be used when defining a
variable. Some common functions that are used in this text are: