The default type for any variable or quantity in MATLAB is a matrix---a two-dimensional array. Scalars and vectors are regarded as special cases of matrices. A scalar is a 1 by 1matrix, while a vector is an n by 1 or 1 by n matrix. A matrix is entered by rows, with entries in a row separated by spaces or commas, and the rows separated by semicolons. The entire matrix is enclosed in square brackets. For example, I can enter a 3 by 2 matrix as follows:
A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
Here is how I would enter a 2 by 1 (column) vector:
x=[1;-1]
x =
1
-1
A scalar, as we have seen above, requires no brackets:
a=4
a =
4
A variation of the who command, called whos, gives more information about the defined variables:
whos
Name Size Bytes Class Attributes
A 3x2 48 double
a 1x1 8 double
ans 1x1 8 double
x 2x1 16 double
The column labeled "size" gives the size of each array; you should notice that, as I mentioned above, a scalar is regarded as a 1 by 1 matrix (see the entry for a, for example).
MATLAB can perform the usual matrix arithmetic. Here is a matrix-vector product:
A*x
ans =
-1
-1
-1
Here is a matrix-matrix product:
B=[-1 3 4 6;2 0 1 -2]
B =
-1 3 4 6
2 0 1 -2
A*B
ans =
3 3 6 2
5 9 16 10
7 15 26 18
MATLAB signals an error if you attempt an operation that is undefined:
B*A
??? Error using ==> mtimes
Inner matrix dimensions must agree.
A+B
??? Error using ==> plus
Matrix dimensions must agree.
More about M-Books
If you are reading this document using the MATLAB notebook facility, then you may wish to execute the commands as you read them. Otherwise, the variables shown in the examples are not actually created in the MATLAB workspace. To execute a command, click on it (or select it) and press control-enter (that is, press the enter key while holding down the control key). While reading the tutorial, you should execute each of my commands as you come to it. Otherwise, the state of MATLAB is not what it appears to be, and if you try to experiment by entering your own commands, you might get unexpected results if your calculations depend on the ones you see in this document.
Notice that the command lines in this document appear in green, and are enclosed in gray square brackets. Output appears in blue text, also enclosed in gray square brackets. These comments may not apply if you are reading a version of this document that has been printed or converted to another format (such as or PDF).
If you are reading this using MATLAB's notebook command, then, as I mentioned above, you can try your own MATLAB commands at any time. Just move the cursor to a new line, type the command, and then type control-enter. You should take advantage of this facility, as it will make learning MATLAB much easier.
Simple graphics in MATLAB
Two-dimensional graphics are particularly easy to understand: If you define vectors x and y of equal length (each with n components, say), then MATLAB's plot command will graph the points (x1,y1), (x2,y2), …, (xn,yn) in the plane and connect them with line segments. Here is an example:
format short
x=[0,0.25,0.5,0.75,1]
x =
0 0.2500 0.5000 0.7500 1.0000
y=[1,0,1,0,1]
y =
1 0 1 0 1
plot(x,y)
Two features of MATLAB make it easy to generate graphs. First of all, the command linspace creates a vector with linearly spaced components---essentially, a regular grid on an interval. (Mathematically, linspace creates a finite arithmetic sequence.) To be specific, linspace(a,b,n) creates the (row) vector whose components are a,a+h,a+ 2h,…,a+(n-1)h, where h=1/(n-1).
x=linspace(0,1,6)
x =
0 0.2000 0.4000 0.6000 0.8000 1.0000
The second feature that makes it easy to create graphs is the fact that all standard functions in MATLAB, such as sine, cosine, exp, and so forth, are vectorized. A vectorized function f, when applied to a vector x, returns a vector y (of the same size as x) with ith component equal to f(xi). Here is an example:
y=sin(pi*x)
y =
0 0.5878 0.9511 0.9511 0.5878 0.0000
I can now plot the function:
plot(x,y)
Of course, this is not a very good plot of sin(x), since the grid is too coarse. Below I create a finer grid and thereby obtain a better graph of the function. Often when I create a new vector or matrix, I do not want MATLAB to display it on the screen. (The following example is a case in point: I do not need to see the 41 components of vector x or vector y.) When a MATLAB command is followed by a semicolon, MATLAB will not display the output.
x=linspace(0,1,41);
y=sin(pi*x);
plot(x,y)
The basic arithmetic operations have vectorized versions in MATLAB. For example, I can multiply two vectors component-by-component using the ".*" operator. That is, z=x.*y sets zi equal to xiyi. Here is an example:
x=[1;2;3]
x =
1
2
3
y=[2;4;6]
y =
2
4
6
z=x.*y
z =
2
8
18
The "./" operator works in an analogous fashion. There are no ".+" or ".-" operators, since addition and subtraction of vectors are defined componentwise already. However, there is a ".^" operator that applies an exponent to each component of a vector.
x
x =
1
2
3
x.^2
ans =
1
4
9
Finally, scalar addition is automatically vectorized in the sense that a+x, where a is a scalar and x is a vector, adds a to every component of x.
The vectorized operators make it easy to graph a function such as f(x)=x/(1+x2). Here is how it is done:
x=linspace(-5,5,41);
y=x./(1+x.^2);
plot(x,y)
If I prefer, I can just graph the points themselves, or connect them with dashed line segments. Here are examples:
plot(x,y,'.')
plot(x,y,'o')
Share with your friends: |