Unformatted text preview: 1 = sin(x) and y2 = cos(x) with x in 0 2 ] on the same graph. Use a solid line for sin(x) and the symbol + for cos(x). The rst step is to de ne a set of values for x at which the functions will be de ned.
x = 0 : 0.1 : 2*pi y1 = sin(x) y2 = cos(x) plot(x, y1, '-', x, y2, '+') the end of each line directs Matlab to not print the values of each vector.
1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 Note: Ordinarily Matlab prints the results of each calculation right away. Placing at 1 2 Example 1 3 4 5 6 7 Another way to get multiple plots on the same graph is to use the hold command to keep the current graph, while adding new plots. Another hold command releases the previous one. For example, the following statements generate the same graph as in Example 1. Matlab remembers that the vector x is already de ned. 3 90 250 120 200 150 150 100 50 180 0 30 60 210 330 240 300 Example 2
plot(x, sin(x), '-') hold plot(x, cos(x), '+') 270 The next example shows how Matlab generates a spiral using the polar coordinate system. Example 2: Plot = 2 with 0 5 in polar coordinates.
theta = 0: 0.2: 5*pi rho = theta.^2 polar(theta, rho, '*') The following example illustrates how to generate a mesh surface in Matlab . p Example 3: Plot z = sin(r)=r with r = x2 + y2, ;8 x 8, ;8 y 8. The rst step in displaying a function of two variables, z = f (x y ), is to use the meshgrid function to generate X and Y matrices consisting of repeated rows and columns, respectively, over the domain of the function. The function can then be evaluated and graphed.
x = -8: .5: 8 y=x X,Y] = meshgrid(x,y) R = sqrt(X.^2 + Y.^2) + eps Z = sin(R)./R mesh(x, y, Z) % add eps to prevent R=0 % or mesh(X,Y,Z) Anything following % on a line is treated as a comment. We added eps (the machine ) to R to pre...
View Full Document
- Summer '10
- matlab, The Matrix Reloaded, Polar coordinate system