Вы находитесь на странице: 1из 10

ELECTROMAGNETIC FIELDS AND WAVES

LAB # 3

Plotting in MATLAB

AHMED JUNAID OMER ELECTRONICS DEPARTMENT

AIR UNIVERSITY, ISLAMABAD


In this activity you will learn how to plot planes and surfaces in MATLAB. MATLAB excels at plotting graphs of functions. Much of todays lab exercise is dedicated to drawing the graphs of the various functions encountered in EMF course. However, you have to walk before you can run, so we will begin with some basic plotting techniques and move toward more advanced techniques subsequently. Example 1 Use MATLAB to sketch the graph of f ( x ) = x 2 Solution So, how would one accomplish this task in MATLAB? We could begin by creating a list of x -values on the Command prompt. x=[-3 -2 -1 0 1 2 3] x= -3 -2 -1 0 1 2 3

Next we wish to square each value in this list. The natural thing to try is x^2, but that wont work. Try it x^2 ??? Error using ==> ^ Matrix must be square. So where does the problem lie. Remember, in MATLAB, x^2 means x*x. You cannot multiply these lists because their dimensions wont accommodate matrix multiplication. However, we dont want to square the list: We want to square each element in the list. Thats what array exponentiation is for! y=x.^2 y= 9 4 1 0 1 4 9

There are two things to note:

3 1. Array exponentiation squared each element of the list x, and 2. We have stored the result of this operation in the variable y for future use. This allows us to easily plot each of the data points provided in the lists x and y. The command plot(x,y,'o') will lot the image shown in Figure 1. The command plot(x,y,'o')warrants some explanation. The command plot plot(x,y,s)plots list y versus list x. The character string s determines the symbol used for plotting. In our case, s=o, which instructs MATLAB to use a small, unfilled circle to plot each point. Other plotting symbols are available. For example, try plot(x,y,'+'), plot(x,y,'*'), or plot(x,y,'s'). A full list of plotting symbols is given in the help file for the plot command. You can also run the above program through M-file. Let us create a new M-file example1.m. Write the following code in it.
x=-3:.1:3; y=x.^2; plot(x,y,'o')

Figure 1: Plotting the graph of f ( x ) = x 2

Two or More Plots


The Plot command can draw more then one graph at a time. If you have data in x1,y1,x2,y2,.....xn,yn then the command plot(x1,y1,x2,y2,.....xn,yn, sn)will plot the data using style s1 for the data in x1, y1, etc. Example 2 Sketch the graphs of f ( x ) = 1 & g ( x ) = ln ( x 1) on the interval [2,5], using MATLAB.

We work this example on the Command prompt. Use the functions f & g to calculate data for the two plots. x=2:0.1:5; f=1./x; g=log(x-1); Two comments are in order. First, MATLABs1command for the natural logarithm is log. Secondly, it is highly illegal to divide a scalar by a vector quantity as any mathematician will tell you. So the command, f=1./x is confusing, to say the least. Technically, we should enter ones(size(x))./x, which makes a lot more sense, but this is such a common operation that MATLAB makes a special interpretation in this case. That is, MATLAB takes 1./x to mean: form a vector whose first element is computed by dividing 1 by x(1), whose second element is computed by dividing 1 by x(2) etc., exactly what we want. Now the command plot(x,f,x,g) will produce a plot containing, the graphs of both f & g on the desired interval. MATLAB also cycles through a default set of colors, automatically coloring , each graph with a different color. The command plot(x,f,'-',x,g,'--')

Base two and base ten logarithms are also available as log2 and log10

5 plots the graphs of f & g with different lifestyles, f with a solid line, g with a dotted line, useful if you dont have a color printer. Finally, when plotting more than one graph, it is good form to create a legend to help distinguish the plot. The command legend('y=f(x)','y=g(x)') was used to create the image shown in Figure 2.

Figure 2: The graph of f ( x ) = 1 & g ( x ) = ln( x 1)

Comet Plots
MATLAB has animation capability that enables you to examine the plot of a set of functions it is drawn in real time. Lets animate the plot of a Lissajous curve. Save the following sequence of commands in an M-file named lissajous.m.
close all t=linspace(0,2*pi,4000); x=cos(5*t); y=sin(7*t); comet(x,y)

Execute the script file by typing lissajous at the MATLAB command prompt.

Clearly, the command comet(x,y)is similar to the plot command, but it animates the plot in real time. One command in the script file lissajous warrant further comment. The close all command close all open figure windows, clearing the desktop for fresh start before opening a new figure window with the comet command.

Plots in other coordinate systems


The command plot uses Cartesian coordinates. However, it is also possible to use other coordinate system like polar and spherical etc. In the following section we will learn how to plot in other coordinate systems and in the complex plane. Example 3
cos t Plot the function r ( t ) = e 2 cos 4t + sin

t both in the Cartesian and polar 12

coordinates. Solution. Let us make a new M-file polar.m and write the following code in it.
t=linspace(0,22*pi,1100); r=exp(cos(t))-2*cos(4*t)+(sin(t./12)).^5; subplot(2,1,1) P=polar(t,r); %plot in polar coordinates. subplot(2,1,2) [x,y]=pol2cart(t,r); %Find the Cartesian coordinates plot(x,y); %Plot in Cartesian coordinates

save the file and run it from command prompt by writing polar. You will see two graphs as shown in Figure 3. The command subplot allows several small graphs to be drawn in the same graphics window. You can write help subplot on the command prompt to find more about it. The commands polar(theta,r) plots in polar coordinates. The elements of vector theta are the arguments in radians, and the elements of the vector r are the distances from the origin.

Surface plots in MATLAB


So far we have discussed two-dimensional plots only i.e. plots of functions of onevariable z = f ( x ) . However, in electromagnetics we often have situations in which functions have two variables i.e. z = f ( x , y ) . The plots of such function fall in the category of surface plots. In this section we will learn how to draw surface plots.

Figure 3: Plots in Polar (upper) and Cartesian (lower) coordinates Example 4 Plot the function f ( x , y ) = x 2 + y 2 Solution For each ordered pair ( x , y ) our function computes an output z = f ( x , y ) . It is the ordered triple ( x , y, z ) that must be plotted. We must come up with a new strategy for crating a table of points that satisfy the equation f ( x , y ) = x 2 + y 2 . MATLAB accomplishes this with the meshgrid command. [X,Y]=meshgrid([1,2,3,4,5]) X= 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5

Y= 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

This rather cryptic output warrants extensive explanation. Actually, the output is easily understood if one superimposes the matrix Y onto the matrix X to obtain a grid of ordered pairs. Therefore Table 2 contains a set of points in the plane that we will substitute into the function f ( x , y ) = x 2 + y 2 (1,1) (1,1) (1,1) (1,1) (1,1) (2,1) (2,1) (2,1) (2,1) (2,1) (3,1) (3,1) (3,1) (3,1) (3,1) (4,1) (4,1) (4,1) (4,1) (4,1) (5,1) (5,1) (5,1) (5,1) (5,1)

Table 2 Interpreting the output of mesh grid MATLABs array smart operators make this an easy proposition. Z=X.^2+Y.^2 Z= 2 5 10 17 26 5 10 17 26 8 13 20 29 13 18 25 34 20 25 32 41 29 34 41 50

The alert student will want use their calculators to check that these points actually satisfy the equation f ( x, y ) = x 2 + y 2 . It is now an easy task to plot the surface to which these points belong. The following command was used to produce the image in Figure 4. mesh(X,Y,Z) Recall how MATLABs plot command was used to draw graphs of functions x=linspace(0,2*pi);

9 y=sin(2*x); plot(x,y)

Figure 4: Plotting the surface f ( x, y ) = x 2 + y 2 This set of commands would plot the points in the vectors x and y, then line segments were used to connect consecutive points. If you plotted enough points, then the plot took on the shape of a smooth curve. Too few points and your plot had a jagged look. When MATLAB plots a surface, a similar thing occurs. MATLAB plots the points, then neighboring points are connected with segments. The surface takes on the appearance of a mesh, where each set of four neighboring points seemed to be joined with small quadrilaterals. Again, if you plot too few points, the surface takes on a jagged look and feel. To draw a smoother surface, plot more points. The following commands were used to draw the image in Figure 5. [X,Y]=meshgrid(1:.2:5); Z=X.^2+Y.^2; mesh(X,Y,Z)

10

Figure 5: A smoother plot of the surface f ( x, y ) = x 2 + y 2 Good Luck! & Have a nice day NB: The assignment questions will be given in the lab

Вам также может понравиться