11.07.2015 Views

MatlabNotes

MatlabNotes

MatlabNotes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

13 Plotting FunctionsIn order to plot the graph of a function, y =sin 3⇡x for 0 apple x apple 1, say, it is sampled atasu ciently large number of points and thepoints (x, y) joined by straight lines. Supposewe take N + 1 sampling points equally spaceda distance h apart:>> N = 10; h = 1/N; x = 0:h:1;defines the set of points x =0,h,2h, . . . , 1 h, 1with h = 0.1. Alternately, we may use thecommand linspace: The general form of thecommand is linspace (a,b,n) which generatesn + 1 equispaced points between a and b,inclusive. So, in this case we would use thecommand>> x = linspace (0,1,11);The corresponding y values are computed by>> y = sin(3*pi*x);and finally, we can plot the points with>> plot(x,y)The result is shown in Fig. 1 below, where it isclear that the value of N is too small.Fig. 2: Graph of y =sin3⇡x for 0 apple x apple 1using h =0.01.13.1 Plotting—Titles & LabelsTo put a title and label the axes, we use>> title(’Graph of y = sin(3pi x)’)>> xlabel(’x axis’)>> ylabel(’y-axis’)The strings enclosed in single quotes, can beanything of our choosing. Some simple L A TEXcommands are available for formatting mathematicalexpressions and Greek characters—seeSection 13.10.See also ezplot the “Easy to use function plotter”.13.2 GridsA dotted grid may be added by>> grid onand is removed with grid off.Fig. 1: Graph of y =sin3⇡x for 0 apple x apple 1using h =0.1.On changing the value of N to 100:>> N = 100; h = 1/N; x = 0:h:1;>> y = sin(3*pi*x); plot(x,y)we get the picture shown in Fig. 2.13.3 Line Styles & ColoursThe default is to plot solid lines. A solid redline is produced by>> plot(x,y,’r--x’)The third argument is a string comprising charactersthat specify the colour (red), the linestyle (dashed) and the symbol (x) tobedrawnat each data point. The order in which theyappear is unimportant and any, or all, may beomitted. The options for colours, styles andsymbols include:12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!