CU Mechanical Engineeringcourse specific Matlab tutorials |
Working with Vectors
Almost all of Matlab's basic commands revolve around the use of vectors. A vector is defined by placing a sequence of numbers within square braces:
>> v = [3 1] v = 3 1
This creates a row vector which has the label "v". The first entry in the vector is a 3 and the second entry is a 1. Note that matlab printed out a copy of the vector after you hit the enter key. If you do not want to print out the result put a semi-colon at the end of the line:
>> v = [3 1]; >>
If you want to view the vector just type its label:
>> v v = 3 1
You can define a vector of any size in this manner:
>> v = [3 1 7 -21 5 6] v = 3 1 7 -21 5 6
Notice, though, that this always creates a row vector. If you want to create a column vector you need to take the transpose of a row vector. The transpose is defined using an apostrophe ( ' ):
>> v = [3 1 7 -21 5 6]' v = 3 1 7 -21 5 6
A common task is to create a large vector with numbers that fit a repetitive pattern. Matlab can define a set of numbers with a common increment using colons. For example, to define a vector whose first entry is 1, the second entry is 2, the third is three, up to 8 you enter the following:
>> v = = [1:8] v = 1 2 3 4 5 6 7 8
If you wish to use an increment other than one that you have to define the start number, the value of the increment, and the last number. For example, to define a vector that starts with 2 and ends in 4 with steps of .25 you enter the following:
>> v = [2:.25:4] v = Columns 1 through 7 2.0000 2.2500 2.5000 2.7500 3.0000 3.2500 3.5000 Columns 8 through 9 3.7500 4.0000
Accessing elements within a vector
You can view individual entries in this vector. For example to view the first entry just type in the following:
>> v(1) ans = 2
This command prints out entry 1 in the vector. Also notice that a new variable called ans has been created. Any time you perform an action that does not include an assignment matlab will put the label ans on the result.
To simplify the creation of large vectors, you can define a vector by specifying the first entry, an increment, and the last entry. Matlab will automatically figure out how many entries you need and their values. For example, to create a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line:
>> 0:2:8 ans = 0 2 4 6 8Matlab also keeps track of the last result. In the previous example, a variable "ans" is created. To look at the transpose of the previous result, enter the following:
>> ans' ans = 0 2 4 6 8To be able to keep track of the vectors you create, you can give them names. For example, a row vector v can be created:
>> v = [0:2:8] v = 0 2 4 6 8 >> v v = 0 2 4 6 8 >> v; >> v' ans = 0 2 4 6 8
Note that in the previous example, if you end the line with a semi-colon, the result is not displayed. This will come in handy later when you want to use Matlab to work with very large systems of equations.
Matlab will allow you to look at specific parts of the vector. If you want to only look at the first three entries in a vector you can use the same notation you used to create the vector:
>> v(1:3) ans = 0 2 4 >> v(1:2:4) ans = 0 4 >> v(1:2:4)' ans = 0 4
Basic operations on vectors
Once you master the notation you are free to perform other operations:
>> v(1:3)-v(2:4) ans = -2 -2 -2
For the most part Matlab follows the standard notation used in linear algebra. We will see later that there are some extensions to make some operations easier. For now, though, both addition subtraction are defined in the standard way. For example, to define a new vector with the numbers from 0 to -4 in steps of -1 we do the following:
>> u = [0:-1:4] u = [0:-1:-4] u = 0 -1 -2 -3 -4
We can now add u and v together in the standard way:
>> u+v ans = 0 1 2 3 4
Additionally, scalar multiplication is defined in the standard way. Also note that scalar division is defined in a way that is consistent with scalar multiplication:
>> -2*u ans = 0 2 4 6 8 >> v/3 ans = 0 0.6667 1.3333 2.0000 2.6667
With these definitions linear combinations of vectors can be easily defined and the basic operations combined:
>> -2*u+v/3 ans = 0 2.6667 5.3333 8.0000 10.6667
You will need to be careful. These operations can only be carried out when the dimensions of the vectors allow it. You will likely get used to seeing the following error message which follows from adding two vectors whose dimensions are different:
>> u+v' ??? Error using ==> plus Matrix dimensions must agree.
Now that you know how to manipulate vectors, let's learn about matrices!