Page 26 -
P. 26

1.7.1  x-y Parametric Plot
                             Now edit another M-file called myline.m as follows and execute it.

                                N=10;
                                for m=1:N
                                   x(m)=m;
                                   y(m)=2*m+3;
                                end
                                plot(x,y)

                              After executing the M-file using myline, you should see a straight line con-
                             necting the points (1, 5) and (10, 23). This demonstration shows the basic con-
                             struct for creating two arrays and plotting the points with their x-coordinate
                             from a particular location in one array and their y-coordinate from the same
                             location in the second array. We say that the plot command here plotted the
                             y-array vs. the x-array.
                              We note that the points are connected by a continuous line making a
                             smooth curve; we say that the program graphically interpolated the discrete
                             points into a continuous curve. If we desire to see additionally the individual
                             points corresponding to the values of the arrays, the last command should be
                             changed to:

                                plot(x,y,x,y,'o')


                             Example 1.10
                             Plot the two curves y  = 2x + 3 and y  = 4x + 3 on the same graph.
                                               1
                                                             2
                             Solution: Edit and execute the following script M-file:

                                for m=1:10                          m=1:10;
                                   x(m)=m;                          x=m;
                                   y1(m)=2*m+3;        or better    y1=2*m+3;
                                   y2(m)=4*m+3;                     y2=4*m+3;
                                end                                 plot(x,y1,x,y2)
                                plot(x,y1,x,y2)

                              Finally, note that you can separate graphs in one figure window. This is
                             done using the subplot function in MATLAB. The arguments of the subplot
                             function are subplot(m,n,p), where m is the number of rows partitioning
                             the graph, n is the number of columns, and p is the particular subgraph cho-
                             sen (enumerated through the left to right, top to bottom convention).



                             © 2001 by CRC Press LLC
   21   22   23   24   25   26   27   28   29   30   31