Question

MECH 1240 Lab 4 1. Generating Row Vectors Manually generating row vectors can be tedious especially if they are very long but fortunately MATLAB has better ways. a) In this

method the Start, Step Size and End values are entered. row_vector ( start value: step size : end value) For example, t = (0.01.99 ) generates a row vector from 0 to .99 in steps of .01 i= ( 105 1000 ) generates a row vector from 10 to 1000 in steps of 5 If only 2 values are entered the step size is automatically set to 1. j= (01000) generates a row vector from 0 to 1000 in steps of 1. b) With the linspace command the Start, End and Number of Points (elements) are entered. row_vector = linspace( start value, end value, number of points) For example, t = linspace( 0, 99, 100) generates a row vector from 0 to .99 with 100 points n = linspace( -10, 50, 20) generates a row vector from -10 to 50 with 20 points If only 2 values are entered the number of points is automatically set to 100. j=linspace(0, 1000) generates a row vector from 0 to 1000 with 100 points 1 2. Plotting with MATLAB a) The plot command The plot(x, y) command plots the y vector (on the y axis) with respect to the x vector (on the x axis). Notice the x axis vector is written first in the command. Examples i. 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 F -0.6 -0.8 - -1 t = linspace( 0, 4*pi) y = sin(t) plot(t, y) NE MECH 1240 Lab 4 2 A plots 2 cycles of a sine wave , 6 generates 100 points from 0 to 4π 2 00 10 12 14 ii. 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 y = cos(t) MECH 1240 Lab 4 plot(t,y) plots 2 cycles of a cosine wave. It is not necessary to repeat the t row vector once it has been defined. 6 8 10 3 12 14 b) Plotting multiple waveforms on the same graph The plot command may be used to plot more than one waveform on the same graph. Enter the x axis vector followed by the y axis vector for each plot. There must be a pair of x and y axis vectors for each waveform even if they share the same variable. MATLAB automatically chooses different colors for each waveform to distinguish between them. iii. 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 - -0.6 Example -0.8 -1 MECH 1240 Lab 4 t = linspace(0, 4*pi) y = sin(t) z = cos(t) plot(t, y, t, z) 4 6 plots 2 cycles of the sine (green) and cosine (blue) waveform 8 10 12 T 14 c) Legend Command The legend command may be used to identify the individual waveforms by colors. 1 0.8 0.6 0.4 0.2 0 -0.2 Colors 'C' ‘m’ -0.4 -0.6 -0.8 -1 XXXX cyan magenta yellow 'y' 'r' red MECH 1240 Lab 4 green ‘'g' 'W' white 'k' black 'b' blue d) Specifying Colors, Line Styles and Markers plot(x, y, 'color-style-marker') 'color-style-marker' is a string containing one to four characters that define the color, line style and marker type. The string is enclosed by single quotation marks. Line Style solid dashed dotted dash-dot (none) no line 10 5 'X' 's' ‘ď’ 6/9 12 'V' sin(t) Marker Type '+' '0' 6*9 دے، cos(t) 14 square diamond up triangle down triangle right triangle left triangle 'p' pentagram 'h' hexagram (none) no marker