Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

2.1 The Basic Features: Matlab Notes Matlab 2 2. Matrix

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

MATLAB NOTES MATLAB 2 2. Matrix 2.

1 The Basic Features Let us start with something simple, like defining a row vector with components the numbers 1, 2, 3, 4, 5 and assigning it a variable name, say x. x=[1 2 3 4 5] x= 1 2 3 4 5 To create a column vector (MATLAB distinguishes between row and column vectors, as it should) we can either use semicolons ( ; ) to separate the entries, or first define a row vector and take its transpose to obtain a column vector. Let us demonstrate this by defining a column vector y with entries 6, 7, 8, 9, 10 using both techniques.

Try this out and see what happen: a. Define y= 6 7 8 9 10 in a row vector. b. Observe, when you put single quote ( ), for example: y Observation:

2.3 Vectors and Matrices


1. We have already seen how to define a vector and assign a variable name to it. Often it is useful to define vectors (and matrices) that contain equally spaced entries. This can be done 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, 1, 2, 3, ..., 7, 8, you can type >>u=[0:8] Observation:

2. To obtain a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line: >>u=[0:2:8] Observation:

Obtain the vector whose entries are 0, 3, 6, 9, 12. Answer:

3. Defining a matrix is similar to defining a vector. To define a matrix A, you can treat it like a column of row vectors. That is, you enter each row of the matrix as a row vector (remember to separate the entries either by commas or spaces) and you separate the rows by semicolons ( ; ). >> A = [1 2 3; 4 5 6; 7 8 9] Observation:

Exercise 2:
Create a diary session called sec2_2 in which you should complete the following exercises. Define

1. Calculate the following (when defined) (a) Ab (b) a+4 (c) ba 2. Explain any differences between the answers that MATLAB gives when you type in A*A , A^2 and A.^2.

2.4 Plotting
1. By typing help plot you can see the various capabilities of this main command for two-dimensional plotting, some of which will be illustrated below. 2. If x and y are two vectors of the same length then plot(x,y) plots x versus y. For example, to obtain the graph of y = cos(x) from and , with increment 0.01. x=-pi:0.01:pi; 3. We placed a semicolon at the end of the input line to avoid seeing the (long) output. Note that the smallest the increment, the smoother the curve will be. 4. Next, we define the vector y y=cos(x); plot(x,y) to , we can first

define the vector x with components equally spaced numbers between -

5. It is good practice to label the axis on a graph and if applicable indicate what each axis represents. This can be done with the x- label and y- label commands. xlabel('x') ylabel('y=cos(x)')

Inside parentheses, and enclosed within single quotes, we type the text that we wish to be displayed along the x and y axis, respectively. We could even put a title on top using title('Graph of cosine from -pi to pi') Note: Remember to enclose the text in parentheses within single quotes. 6. Various line types, plot symbols and colors can be used. If these are not specified (as in the case above) MATLAB will assign (and cycle through) the default ones as given in the table below.

So, to obtain the same graph but in green, we type plot(x,y,g) where the third argument indicating the color, appears within single quotes. We could get a dashed line instead of a solid one by typing plot(x,y,--)

7. Multiple curves can appear on the same graph. If for example we define another vector z=sin(x); We can get both graphs on the same axis, distinguished by their line type, using plot(x,y,'--',x,z,':')

When multiple curves appear on the same axis, it is a good idea to create a legend to label and distinguish them. The command legend does exactly this. legend('cos(x)','sin(x)') 8. Other commands for data visualization that exist in MATLAB include:

Exercise 3:
1. Plot of the functions f ( x ) = x 2 , g( x ) = x 3 for x = -1 1on the same axis. Label the x and y axes and create a legend indicating which graph is which. 2. Here are some laboratory results to plot on a graph and plot voltage against current: current voltage 1 2.1 2 4.3 3 5.7 4 8.6 5 9.4 6 13.1 7 15.2

You might also like