MatLab Directions for 18.02Access MatLab by clicking on MatLab on the Athena screen, or by typing:% add matlab [return] % matlab [return]Entering matrices and vectors; Basic operationsIn MatLab the variables r epresent matrices and vectors. The symbol = is used to assignvalues to the variables. To see how this works, type each of these lines in or der; remember :always hit [return] or [enter] to end a line.A = [1 2 3; 4 5 6; 7 8 9] (you can use commas instead of spaces: 1,2,3;)b = [1 0 1]b′eye(3) (eye = I, the identity matrix)Try a mistake: C = [1, 2, 3; 4, 5]; to correct it, press any arrow key to get the line back.Sum, difference A + B, A − B (matrices must b e same size)Product A*B (matrices must be compatibly sized)Powers A ˆn (A times itself n times; A must b e square)Quotient left: A\b (the solution to Ax = b)right: b/A (the solution to xA = b)Transpose A′Inverse inv(A)Try typing (use the values of A and b above): A + eye(3) A*b A*(b′) A*b′3*bSpecial MatLab OperatorsArray Op erators: Use dots to make component-wise operations. Let x = [x1x2. . . xn].x.ˆ m = [xm1. . . xmn] (m can be 0)x. ∗ y = [x1y1. . . xnyn]f(x) = [f(x1) . . . f (xn)], f = sin, cos, log, polynomials, etc.Colon operator This generates a vector with equally spaced entries; for example,[0 : 2 : 12] = [0 2 4 6 8 10 12]; [2 : −.1 : 1.6] = [2.0 1.9 1.8 1.7 1.6]Two-dimensional plots in MatLabLet x = [x1x2. . . xn], and y = [y1. . . yn] ; thenplot (x, y) (plots the n points (xi, yi), joined by solid line segments)plot (x, y,′−−′) (plo ts the n points, joined by dashed line segments)plot (x, y,′∗′) (plo ts the n points as individual stars (or dots or circles, etc))hold (toggles between on and off (at the start it’s off); when off, the newplot replaces the old; when on, the newplot is sup e rimposed on the old)print (gives a print-out of the current screen plot)Try in order (read L to R; commands are separated by spaces; press [return] after each):x = [0 : .1 : 2] plot (x, sin(x)) plot(x, cos(x),′∗′) holdplot (x, sin(x),′− −′) holdplot (x, 4 ∗ x.ˆ 3) (plots y = 4x3; note the need for the array o perator)2Directions for 3D Graphs in MatLabTo plot the 3D graph of z = f (x, y), you specify:the grid (xi, yj) of lattice points: give the vectors x = [x1. . . xn] and y = [y1. . . yn].Example: To make a grid with spacing .1, over the interval [−2, 2] on both a xes, type (inwhat follows, ≫ is the matlab prompt; don’t type it — type the semicolon at the end soMatlab won’t print out all the numbers — remember [return] at the end)≫ x = [− 2 : .1 : 2];≫ y = [−2 : .1 : 2];≫ [x, y] = meshgrid(x, y);the function z = f(x, y) For example, to graph the function f(x, y) = x2− y2, type≫ z = x.ˆ 2 − y.ˆ 2;plot the graph either as a mesh of lines, or as a filled-in surface (the color indicates thevalue of z, i.e., the height of the graph above the xy-plane); type first≫ mesh(x, y, z) then ≫ surf(x, y, z)change the viewpoint To change the viewpoint (i.e., rotate the g raph left or right,up or down), type≫ rotate3dthen place the mouse cursor in the graph region, hold down left button, move mouse,release button. The two numbers on the screen are the azimuth: ang le in deg rees from thenegative y-axis to the line of sight, and the elevation, the angle in degrees from the xy-planeto the line of sight. To turn off rotation, type again: ≫ rotate3dhidden lines Try typing: ≫ hidden (type it again to change back)changing scale To change the x-axis scale to [−4, 4], the y-axis to [−5, 5], and thez-axis to [−20, 20], type≫ axis([−4 4 − 5 5 − 20 20])level curves To get a 2D plot with 20 level curves, type: ≫ c ontour(x, y, z, 20)contour curves To get a 3D plot with 20 contour curves, type: ≫ contour3(x, y, z,
View Full Document