0% found this document useful (0 votes)
198 views

Introduction To Matlab PDF

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views

Introduction To Matlab PDF

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PLOT INSTRUCTION

introductio
MATLAB has an excellent set of graphic tools. Plotting a given data set or the results
of computation is possible with very few commands. You are highly encouraged to plot
mathematical functions and results of analysis as often as possible. Trying to understand
mathematical equations with graphics is an enjoyable and very efficient way of learning math-
ematics. Being able to plot mathematical functions and data freely is the most important
step, and this section is written to assist you to do just that.

Creating simple plots


The basic MATLAB graphing procedure, for example in 2D, is to take a vector of x -

coordinates, x = ( x1 , . . . , xN ), and a vector of y-coordinates, y = (y1 , . . . , yN ), locate the


points (x i, y i ), with i = 1, 2, . . . , n and then join them by straight lines. You need to prepare
x and y in an identical array form; namely, x and y are both row arrays or column arrays of
the same length.
The MATLAB command to plot a graph is plot(x,y) . The vectors x = (1 , 2, 3 , 4, 5, 6)
and y = (3, 1 , 2, 4, 5, 1) produce the picture shown in Figure 2.1.

>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)

Note: The plot functions has different forms depending on the input arguments. If y is a
vector plot(y)produces a piecewise linear graph of the elements of y versus the index of the
elements of y. If we specify two vectors, as mentioned above, plot(x,y) produces a graph
of y versus x.
For example, to plot the function sin (x) on the interval [0 , 2π], we first create a vector of
x values ranging from 0 to 2 π, then compute the sine of these values, and finally plot the
result:

1
5

−1
1 2 3 4 5 6

Figure 2.1: Plot for the vectors x and y

>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)

Notes:

• 0:pi/100:2*pi yields a vector that

– starts at 0,
– takes steps (or increments) of π/100,
– stops when 2π is reached.

• If you omit the increment, MATLAB automatically increments by 1.

2
REFERENCES

[1] The MathWorks Inc. MATLAB 7.0 (R14SP2). The MathWorks Inc., 2005.

[2] S. J. Chapman. MATLAB Programming for Engineers. Thomson, 2004.

[3] C. B. Moler. Numerical Computing with MATLAB. Siam, 2004.

You might also like