USE OF BUILT-IN FUNCTIONS

definitions also. MATLAB will evaluate the matrix version when the letter m is appended to the function name. Try sqrtmA ans = 0.4498 + 0.7623i 0.5526 + 0.2068i 0.6555 - 0.3487i 1.0185 + 0.0842i 1.2515 + 0.0228i 1.4844 - 0.0385i 1.5873 - 0.5940i 1.9503 - 0.1611i 2.3134 + 0.2717i There are several functions for rounding. For example, suppose that we enter a vector: E = [-1.6 -1.5 -1.4 1.4 1.5 1.6]; The round function rounds the elements of E to the nearest integers: roundE ans = -2 -2 -1 1 2 2 The ceil short for ceiling function rounds to the nearest integers toward infinity: ceilE ans = -1 -1 -1 2 2 2 The floor function rounds down to the nearest integers toward minus infinity: floorE ans = -2 -2 -2 1 1 1 There are also functions that perform special actions on the elements of matrices and arrays. For example, the sum function returns the sum of the elements: F = [3 5 4 6 1]; sumF ans = 19 In a similar way, it should be pretty obvious what’s happening with the following commands: minF,maxF,meanF,prodF,sortF ans = 1 ans = 6 ans = 3.8000 ans = 360 ans = 1 3 4 5 6 2.4 USE OF BUILT-IN FUNCTIONS 37 A common use of functions is to evaluate a formula for a series of arguments. Recall that the velocity of a free-falling bungee jumper can be computed with [Eq. 1.9]: v = gm c d tanh gc d m t where v is velocity ms, g is the acceleration due to gravity 9.81 ms 2 , m is mass kg, c d is the drag coefficient kgm, and t is time s. Create a column vector t that contains values from 0 to 20 in steps of 2: t = [0:2:20] t = 2 4 6 8 10 12 14 16 18 20 Check the number of items in the t array with the length function: lengtht ans = 11 Assign values to the parameters: g = 9.81; m = 68.1; cd = 0.25; MATLAB allows you to evaluate a formula such as v = f t, where the formula is computed for each value of the t array, and the result is assigned to a corresponding posi- tion in the v array. For our case, v = sqrtgmcdtanhsqrtgcdmt v = 18.7292 33.1118 42.0762 46.9575 49.4214 50.6175 51.1871 51.4560 51.5823 51.6416

2.5 GRAPHICS

MATLAB allows graphs to be created quickly and conveniently. For example, to create a graph of the t and v arrays from the data above, enter plott, v The graph appears in the graphics window and can be printed or transferred via the clip- board to other programs. 60 50 40 30 20 10 2 4 6 8 10 12 16 18 14 20 You can customize the graph a bit with commands such as the following: titlePlot of v versus t xlabelValues of t ylabelValues of v grid 60 50 40 30 20 10 2 4 6 8 10 Values of t Plot of v versus t Values of v 16 18 14 12 20 The plot command displays a solid thin blue line by default. If you want to plot each point with a symbol, you can include a specifier enclosed in single quotes in the plot func- tion. Table 2.2 lists the available specifiers. For example, if you want to use open circles enter plott, v, o You can also combine several specifiers. For example, if you want to use square green markers connected by green dashed lines, you could enter plott, v, s--g You can also control the line width as well as the marker’s size and its edge and face i.e., interior colors. For example, the following command uses a heavier 2-point, dashed, cyan line to connect larger 10-point diamond-shaped markers with black edges and magenta faces: plotx,y,--dc,LineWidth,2,... MarkerSize,10,... MarkerEdgeColor,k,... MarkerFaceColor,m Note that the default line width is 1 point. For the markers, the default size is 6 point with blue edge color and no face color. MATLAB allows you to display more than one data set on the same plot. For example, an alternative way to connect each data marker with a straight line would be to type plott, v, t, v, o It should be mentioned that, by default, previous plots are erased every time the plot command is implemented. The hold on command holds the current plot and all axis prop- erties so that additional graphing commands can be added to the existing plot. The hold off command returns to the default mode. For example, if we had typed the following commands, the final plot would only display symbols: plott, v plott, v, o 2.5 GRAPHICS 39