YData: [27 8 1]ZData:[1x0 double]The colour is described by a rgb triple in which[0 0 0] denotes black and [1 1 1] denoteswhite. Properties can be changed with the setcommand, for example>> set(plt,’markersize’,12)will change the size of the marker symbol ’o’while>> set(plt,’linestyle’,’:’,’ydata’,[1 8 27])will change the lifestyle from dashed to dottedwhile also changing the y–coordinates of thedata points. The commands>> x = 0:.01:1; y=sin(3*pi*x);>> plot(x,y,’k-’,x,cos(3*pi*x),’g--’)>> legend(’Sin curve’,’Cos curve’)>> title(’Multi-plot ’)>> xlabel(’x axis’), ylabel(’y axis’)>> set(gca,’fontsize’,16,...’ytick’,-1:.5:1);redraw Fig. 3 and the last line sets the fontsize to 16points and changes the tick-marks onthe y-axis to 1, 0.5, 0, 0.5, 1—see Fig. 4. The... in the penultimate line tell Matlab that theline is split and continues on the next line.Example 13.1 Plot the first 100 terms in thesequence {y n } given by y n = 1+ 1 nn and illustratehow the sequence converges to the limite = exp(1) = 2.7183.... as n !1.Exercises such as this that require a certainamount of experimentation are best carried outby saving the commands in a script file. Thecontents of the file (which we call latexplot.m)are:close allfigure(1);set(0,’defaultaxesfontsize’,12)set(0,’defaulttextfontsize’,16)set(0,’defaulttextinterpreter’,’latex’)N = 100; n = 1:N;y = (1+1./n).^n;subplot(2,1,1)plot(n,y,’.’,’markersize’,8)hold onaxis([0 N,2 3])plot([0 N],[1, 1]*exp(1),’--’)text(40,2.4,’$y_n = (1+1/n)^n$’)text(10,2.8,’y = e’)xlabel(’$n$’), ylabel(’$y_n$’)The results are shown in the upper part of Fig. 5.Fig. 4: Repeat of Fig. 3 with a font size of16points and amended tick marks on the y-axis.13.10 Formatted text on PlotsIt is possible to typeset simple mathematicalexpressions (using L A TEX commands) in labels,legends, axes and text. We shall give two illustrations.Fig. 5: The output from Example 13.1 (top)and Example 13.2 (bottom).The salient features of these commands are1. The set commands in lines 3–4 increasethe size of the default font size used for15
the axis labels, legends, titles and text.Line 4 tells Matlab to interpret any stringscontained within $ symbols as L A TEX commands.2. Defining a variable N = 100 makes it easierto experiment with a di↵erent numberof sampling points.3. The size of the plot symbol “.” is changedfrom the default (6) to size 8 by the additionalstring followed in the plot command.4. The axis command changes the dimensionsof the plotting area to be 0 apple x apple Nand 2 apple y apple 3.The axis command has four parameters,the first two are the minimum and maximumvalues of x to use on the axis andthe last two are the minimum and maximumvalues of y.5. The command text(40,2.4,’string’ )prints string at the location with coordinates(40 2.4).6. The string y_n gives subscripts: y n ,whilex^3 gives superscripts: x 3 .Example 13.2 Draw a graph the function y =e 3x2 sin 3 (3⇡x) on the interval 2 apple x apple 2.2. Greek characters ↵, ,...,!, ⌦ are producedby the strings ’\alpha’, ’\beta’,...,’\omega’, ’\Omega’. the integral symbol:R is produced by ’\int’.3. The thickness of the line used in the plotcommand is changed from its default value(0.5) to 2.4. The graphics are saved in jpeg format tothe file eplot1.13.11 Controlling AxesThe look of a graph can be changed by usingthe axis command. We have already seen inExample 13.1 how the plotting area can bechanged.axis equal is required in order that a circledoes not appear as an ellipse>> clf, N = 100; t = (0:N)*2*pi/N;>> x = cos(t); y = sin(t);>> plot(x,y,’-’,0,0,’.’);>> set(gca,’ytick’,-1:.5:1)>> axis equalSee Fig. 6. We recommend looking at helpaxis and experimenting with the commandsaxis equal, axis off, axis square,axis normal, axis tight in any order.The appropriate commands are included in thescript file for the previous example (so the defaultvalues continue to operate):subplot(2,1,2)x = -2:.01:2;y = exp(-3*x.^2).*sin(8*pi*x).^3;plot(x,y,’r-’,’linewidth’,1)xlabel(’$x$’), ylabel(’$y$’)text(-1.95,.75,’$ \exp(-40x^2)\sin^3(8\pi x)$’)print -djpeg90 eplot1The results are shown in the lower part of Fig. 5.1. sin 3 8⇡x is typeset by the L A TEX string$\sin^3 8\pi x$ and translates into theMatlab command sin(8*pi*x).^3—theposition of the exponent is di↵erent.Fig. 6: Use of axis equal to get a circle toappear correctly.16
- Page 1 and 2: An Introduction to MatlabVersion 3.
- Page 3 and 4: 1 MATLAB• Matlab is an interactiv
- Page 5: These are not allowable:Net-Cost, 2
- Page 8 and 9: Note that the components of x were
- Page 10 and 11: For example, if u = [10, 11, 12], v
- Page 12 and 13: a.*b -24, ans./cans =-18 -10 0 12 2
- Page 14 and 15: Colours Line Styles/symbolsy yellow
- Page 18 and 19: 14 Elementwise ExamplesExample 14.1
- Page 20 and 21: 0 00 0The second command illustrate
- Page 22 and 23: 9 10 11 1220 0 5 4>> J(1,1)ans =1>>
- Page 24 and 25: 15.12 Sparse MatricesMatlab has pow
- Page 26 and 27: It is well-known that the solution
- Page 28 and 29: the corresponding number, and two f
- Page 30 and 31: -2.0000 3.1416 5.0000-5.0000 -3.000
- Page 32 and 33: if logical test 1Commands to be exe
- Page 34 and 35: 6 8 0 1>> k = find(A==0)k =29Thus,
- Page 36 and 37: function [A] = area2(a,b,c)% Comput
- Page 38 and 39: In order to plot this we have to de
- Page 40 and 41: 100 2256200 4564300 3653400 6798500
- Page 42 and 43: ’string’,’lin-log’,...’po
- Page 44 and 45: Matrix analysis.cond Matrix conditi
- Page 46 and 47: imag, 42inner product, 8, 21, 22int