






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







2.3 Color, Linestyle, Markers, Axes Labels, Titles
1. BASIC OPERATIONS IN MATLAB
Linear Algebra and Arrays Informally, the terms matrix and array are often used interchangeably. More precisely, a matrix is a two-dimensional numeric array that represents a linear transformation. The mathematical operations defined on matrices are the subject of linear algebra. When they are taken away from the world of linear algebra, matrices become two-dimensional numeric arrays. Arithmetic operations on arrays are done element-by-element. This means that addition and subtraction are the same for arrays and matrices, but that multiplicative operations are different. MATLAB uses a dot, or decimal point, as part of the notation for multiplicative array operations. The list of operators includes: + Addition
- Subtraction .* Element-by-element multiplication ./ Element-by-element division .^ Element-by-element power .' Unconjugated array transpose. The transpose operator swaps rows and columns and so all rows become columns and all columns become rows. !!! IMPORTANT !!! The operations listed above are for array or element-by-element operations, which is what we will be using in this course. There are similar operations used for matrix operations, including *, / , ^ , ' which are used in linear algebra. BE CAREFUL NOT TO CONFUSE THE TWO! Some examples are explained below.
There is a difference between A*A and A.*A. The first, A*A , is matrix multiplication which you will learn if you take linear algebra. A.*A is array multiplication and takes each element of A and multiplying it by itself. So, A.* takes each element of A and multiplies it by 2.
This command does matrix multiplication.
MATLAB provides four functions that generate basic matrices: zeros All zeros ones All ones rand Uniformly distributed random elements randn Normally distributed random elements Some examples:
This creates a 2 by 4 matrix of zeros.
This creates a 3 by 3 matrix, each element being a 5.
This creates a 1 by 10 matrix, each element being a random number generated by a uniform distribution from the interval [0,1].
The random number generated in (11) is multiplied by 10 and only the (rounded) integer part is retained in (12).
This creates a 4 by 4 matrix, each element being a random number generated by a normal distribution.
2. GRAPHICS
There are 2 methods for plotting graphs in MATLAB and 2 ways to display your graph output. 2.1.1 Plotting graphs using a function: the ezplot command
One way to plot graphs if you have a function is to use the ezplot command. ezplot(f) plots the expression f = f (x) over the default domain -2pi < x < 2pi. ezplot (f, [a, b]) plots f = f (x) over the domain a < x < b
There are other types of ezplot commands such as ezplot3 that will plot a parametric equation, and ezsurf , which will plot a surface. Look up these commands in the help menu to find out more about them.
x=[1,2,3,4,5,6,7,8,9,10]; u=[1,4,9,16,25,36,49,64,81,100];
Note that the function is enclosed in quotes. This is to indicate that the function x is a variable and doesn't have numerical values. The graph below is the same equation as the one above, but has been modified in the interactive figure window and imported to this document with the insert/picture/from file button in the toolbar above. Unfortunately, with these plots there is no way to do some of the things possible by plotting the other way, such as putting more than one graph on the same plot. Below, we will show you many of the commands that appear in the interactive menus of the figure windows. These are useful when you wish to have a graph display correctly the first time without having to modify it. 2.1.2 Plotting graphs with numerical data: the plot command MATLAB is a computational language and needs data (numbers) to plot graphs of functions or graphs of data. If a function is a built-in function, such as sin(x) , you need to tell MATLAB which values to plot. For example, to plot the value of the sine function from zero to 2p i, use
x contains a sequence of points from 0 to 2*pi in steps of pi/100 (i.e. numerical values for x ). Notice that the semi-colon suppresses the output; otherwise you would get a list of all the numbers in the matrix x (try it!). The steps of pi/100 produce a nice, smooth plot. You might like to try to compare this to plotting values of x , where x = 0:pi/5:2*pi
You will likely want to export your plots as a jpg format so you can then import into them a Word document (using Insert->Picture->From File ) when writing up your lab report.
Next, we plot three related functions of x, with each curve in a separate distinguishing color. MATLAB gives a different color to each curve automatically. You will see how to change colors later. x=0:pi/100:2pi; y2 = sin(x-.25); y3 = sin(x-.5);*
Note : If you want to enter multiple lines against the MATLAB prompt as shown above then you should separate the lines at the prompt by pressing <shift+enter> after each line. First the x values, and then the y values are listed for each graph If you were to type this commands in MATLAB and want them to appear in a new figure window, then you would type the command figure(2 ) before doing the plot command to create a new (second figure window). Otherwise the plot would appear in the previous window.
You can and should enhance your graphs with axes labels, titles and color and legends if appropriate. You can either use the following MATLAB commands to enhance your graphs. These commands work with graphs you are going to embed in your document or graphs in the Figure environment. However, it is usually much easier to label your graphs interactively using the Figure window tool bar by using the Insert and Tools menus. 2.3.1 Commands for color, linestyle, markers It is possible to specify color , linestyle , and markers , such as plus signs or circles, with: plot(x,y,'color_style_marker')
where color_style_marker is a 1-, 2 -, or 3-character string (delineated by single quotation marks) constructed from a color, a linestyle, and a marker type: Color strings are ' c ', ' m ', ' y ', ' r ', ' g ', ' b ', ' w ', and ' k '. These correspond to cyan, magenta, yellow, red, green, blue, white , and black. Linestyle strings are ' - ' for solid , ' - - ' for dashed , ' : ' for dotted , ' -. ' for dash-dot , and ' none ' for no line. The most common marker types include ' + ', ' o ', ' ***** ', and ' x '. For example, the statement:
plots a yellow dotted line and places plus sign markers at each data point. If you specify a marker type but not a linestyle, MATLAB draws only the marker. For example, plot(t,y,'o') would plot only points. 2.3.2 Commands for axis labels, legends, titles MATLAB commands can be used to label your graphs. As described above, axis labels, legends and titles can be placed on a graph using the Insert and Tools menus from the Figure Window tool bar. Usually it is much easier to use the tool bar. x = -pi:pi/100:pi; y = sin(x); plot(x,y) %Restrict X-axis between [-pi,pi] and Y-axis between [-1,1]. axis([-pi pi -1 1]) %These commands label the axes. xlabel('-pi <= x <= pi') ylabel('sin(x)') %This is the command for giving a title to a plot. title('Graph of the sine function') %This command writes inside a plot. Note that you have to specify the %coordinates where the text is to start – in this case (1,-1/3).
Here is another example. x = -pi:pi/100:pi; plot(x,cos(x),'-ro',x,sin(x),'-.b') xlabel('-pi <= x <= \pi')
Having create the matrix containing the samples of the function, the surface can be graphed using either the mesh or the surf commands:
In addition, a contour plot can be created
If you execute these commands within MATLAB to use the Figure environment, you can rotate these graphs interactively by using the circular dashed arrow icon. Try it! Supplementary commands in MATLAB (useful for Bonus Problems)
The subplot function allows you to display multiple plots in the same window or print them on the same piece of paper. Typing subplot(m,n,p) breaks the figure window into an m-by-n matrix of small subplots and selects the pth subplot for the current plot. The plots are numbered along first the top row of the figure window, then the second row, and so on. For example, to plot data in four different subregions of the figure window, figure(4); x = 0:pi/10:2pi; [X,Y,Z] = cylinder(4cos(x)); subplot(2,2,1); mesh(X) subplot(2,2,2); mesh(Y) subplot(2,2,3); mesh(Z)**
The coordinates of 4*cos(x) are stored in X,Y and Z.
Use the figure command to make sure all subsequent plots are placed in a new figure rather than in the subplot.
3. ANIMATION MATLAB provides several ways of generating moving, animated, graphics. Using the EraseMode property is appropriate for long sequences of simple plots where the change from frame to frame is minimal. Here is an example showing simulated Brownian motion. You must execute these commands within MATLAB. First we are going to specify a number of points and then a temperature or velocity. The best values for these two parameters depend upon the speed of your particular computer. Execute the m-file Brownian_motion.m by typing the name of the file "Brownian_motion" at the MATLAB prompt and