Basic Plotting To plot a two dimensional graph

Basic Plotting To plot a two dimensional graph, you need two vectors and the command, plot. Try: >> x = [ 1 2 3 4 5] >> y = [3 9 12 10 6] >>plot(x, y) This is the simplest use of this command. You can add information to your graph such as title and axis labels using other commands but in order to keep the plotted graph in its window you need the hold on command. Try: >> hold on >> title(‘This is my first graph’) >>xlabel(‘x values’) >>ylabel(‘y values’)

Plotting functions Let’s now try and plot a function y=x 2 -4 in the range from x=-3 to 3. First we need the vector x. We could do this several ways: >>x = [-3 -2 -1 0 1 2 3] being the simplest, or >>x=(-3: 1: 3) Now we need the vector y: >> y = x. ^2 -3 Now plot: >>plot(x, y) >>hold on >> title(‘This is an example of a function’) >>xlabel(‘x values’) >>ylabel(‘y values’)

Playing with plot You can change line style to a variety of different types of lines. Try the following as examples: >>plot(x, y, ’: ’) >>plot(x, y, ’--’) >>plot(x, y, ’-. ’) You can also change the color using the letters b for blue, g for green, r for red, c for cyan, m for magenta, y for yellow, k for black and w for white. MATLAB help has a much longer list! Try out a few: >>plot(x, y, ’b’) what do you get? You can plot points with symbols, instead of lines. Try: >>plot(x, y, ’o’) other symbols to try are x, +, *, s, d, . You can then combine these for a specific graph. For example to get a red, dotted line with cross symbols try: >>plot(x, y, ’rx: ’)

Plotting two curves on the same graph Sometimes you may want two curves on the same graph. You can do this by giving more values in the plot command, plot(x, y, x, z). Let’s go through the example below: >>x=0: pi/20: 2*pi; >>y=sin(x); >>z=cos(x); >>plot(x, y, ’-’, x, z, ’--’) >>hold on; >>grid on; >>title(‘Sine and Cosine functions’) >>xlabel(‘x-axis’) >>ylabel(‘sine and cosine’) >>legend(‘sinx’, ‘cosx’) You can also create several axis on one figure using subplot. We won’t do this today, but you can find help on subplot in the book and in the mathworks documentation plot 3 gives you x, y, z axis. Again you can find help in the book and in documentation

Exercises 1. Plot a two-dimensional graph of the two vectors x = [1 2 3 4 5 6 7] and y = [10 15 23 43 30 1012] using the plot command. 2. In Exercise 1 above, add to the graph a suitable title along with labels for the x-axis and the yaxis. 3. Plot the mathematical function y = 2 x 3 + 5 in the range x from -6 to +6. Include a title for the graph as well as labels for the two axes. 4. Repeat the plot of Exercise 3 above but show the curve with a blue dashed line with circle symbols at the plotted points. 5. Plot the two mathematical functions and such that the two curves appear on the same diagram. Make the range for x between 0 and 3π with increments of. Distinguish the two curves by plotting one with a dashed line and the other one with a dotted line. Include the title and axis information on the graph as well as a grid and a legend. 6. Plot the following four mathematical functions each with its own diagram using the subplot command. The functions are Use the vector x = [1 2 3 4 5 6 7 8 9 10] as the range for x. There is no need to show title or axis information.
- Slides: 5