Two-Dimensional Graphics. Statistics Graphics and Curves in Explicit, Parametric and Polar Coordinates
MATLAB allows the representation of any mathematical function, even if it is defined piecewise or jumps to infinity in its field of definition. MATLAB makes graphs of planar (two-dimensional) curves and surfaces (three-dimensional), groups them and can overlap them. You can combine colors, grids, frames, etc., in the graphics. MATLAB allows representations of functions in implicit, explicit and parametric coordinates, and is without a doubt mathematical software with high graphics performance. One of their differences with the rest of the symbolic calculation packages is that “animations” can be generated by combining different graphics with slight variations from each other displayed quickly in succession, to give the impression of movement generated from graphs similarly to moving pictures and cartoons.
In addition, MATLAB also allows for typical bar graphs, lines, star graphs and histograms. It also offers special possibilities of representation of polyhedra with geographical maps. In handling of graphics, it is very important to bear in mind the availability of memory on the computer. The graphics drawings consume lots of memory and require high screen resolution.
2.1 Two-Dimensional Graphics (2-D)
The basic commands that MATLAB uses to draw the graph of a function of a variable are as follows:
Let’s see some examples of 2-dimensional graphics:
We will begin by representing the function f (x) = (sin(x)) ^ 2 + 2xcos(x) (-2,2):
>> x = [-2*pi:0.1:2*pi];
>> y = sin(x) .^ 2 + 2 * x .* cos(x);
>> plot(x,y)
MATLAB returns the graph that is shown in Figure 2-1. It is worth noting that the definition of the function has been made in vector form, using the notation for vector variables. The range of the trigonometric function is defined in pi radian (in this case -2 pi to 2 pi).
The same graph is obtained using the command fplot with the following syntax:
>> fplot ('sin(x) ^ 2 + 2 * x * cos(x)', [- 2 * pi, 2 * pi])
And the same representation can be obtained by using the command ezplot using the following syntax:
>> ezplot ('sin(x) ^ 2 + 2 * x * cos(x)', [- 2 * pi, 2 * pi])
Observe that in the last two cases functions are expressed symbolically, and not as a vector, as in the form of the first case. (Note the quotes surrounding the function).
MATLAB draws not only bounded functions, but it also represents features that have asymptotes and singularities. For example, Figure 2-2 shows the graph of the function y = x ^ 3 /(x^2-4) which has asymptotes for x at -2 and 2 in the range of variation of x given by (- 8,8) by using the command:
>> ezplot ('x ^ 3 / (x^2-4)', [- 8, 8])
EXERCISE 2-1
Represent the graphs of the functions Sine(x), Sine(2x) and Sine(3x), varying in the range (0,2p) for x, all on the same axes.
The graphics, generated by the input is represented in Figure 2-3:
fplot (@(x)[sin(x), sin(2*x), sin(3*x)], [0, 2 * pi])
It may be useful to differentiate between curves by their strokes, for instance if you cannot assume that your ultimate user of the graph can print in color. For variety, in Figure 2-4 we represent the first function, Sine (x), with a black line, the second, Sine(2x), using blue star, and the third, Sine (3x), with red circles. We use the following syntax:
>> x = (0:0.05:2*pi);
>> y1 = sin(x); y2 = sin(2*x); y3 = sin(3*x);
>> plot(x,y1,'k-',x,y2,'b*',x,y3,'ro')
2.2 Titles, Tags, Meshes and Texts
The commands available in MATLAB for these purposes are as follows:
EXERCISE 2-2
On the same axes represent the graphs of the functions y = sin (x2)and y = log (sqrt (x)). The text of each equation is properly positioned within the graph.
We get the graph in Figure 2-5 considering the following MATLAB entry:
>> x = linspace (0,2,30);
>> y = sin(x.^2);
>> plot(x,y)
>> text (1,0.8, ' y = sin(x^2)')
>> hold on
>> z = log(sqrt(x));
>> plot (x, z)
>> text (1, - 0.1, ' y = log (sqrt (x))')
>> xlabel('X Axis'),
>> ylabel ('Y Axis'),
>> title('Graphic sine and logarithmic'),
2.3 Manipulating Graphics
Below, are commands that allow you to manipulate the axes of a graph, placement of it within the screen, their appearance, their presentation from different points of view, etc.
EXERCISE 2-3
Present in the same figure the graphs of the functions Sin(x) and Cos (x), placed horizontally one next to each other with their names, with the x axis values between 0 and 2 * pi, a shaft, and taking y values between - 1 and 1. Also get the vertical representation, so that they one under the other and use slotted shafts.
MATLAB, we propose the following entry:
>> x = (0:0.1:2*pi);
>> y = sin(x);
>> z = cos(x);
>> subplot(1,2,1);
>> plot(x,y), axis([0, 2*pi, -1, 1]), title('sin(x)')
>> subplot(1,2,2);
>> plot(x,z), axis([0, 2*pi, -1, 1]), title('cos(x)')
The result is presented in Figure 2-6:
We now propose to MATLAB the following entry to shift the Sine curve to the top:
>> x = (0:0.1:4*pi);
>> y = sin (x);
>> z = cos (x);
>> subplot(2,1,1);
>> plot(x,y), axis([0 2*pi -1 1]), title('sin(x)'), grid
>> subplot(2,1,2);
>> plot (x, z), axis([0 2*pi -1 1]), title ('cos (x)'), grid
The result is presented in Figure 2-7:
EXERCISE 2-4
Present in the same figure, graphs of the functions Sine (x), Cos (x), Cosec (x) and Sec (x), placed in a matrix of four graphics, but under each function place its inverse for x ranging from [- 2p, 2p].
We use the command subplot to draw the four functions, in the appropriate order under Sine (x) place Cosec (x), and under Cos (x), place Sec (x). The syntax will be as follows:
>> subplot(2,2,1);
>> ezplot('sin (x)', [- 2 * pi, 2 * pi])
>> subplot(2,2,2);
>> ezplot('cos(x)',[-2*pi 2*pi])
>> subplot(2,2,3);
>> ezplot('csc(x)',[-2*pi 2*pi])
>> subplot(2,2,4);
>> ezplot('sec(x)',[-2*pi 2*pi])
MATLAB offers as a result the graph of Figure 2-8.
2.4 Logarithmic Graphics
The commands that enable MATLAB to represent graphs with logarithmic scales are the following:
EXERCISE 2-5
Present on the same graph the function y = abs (e-1/2 x Sine(5x)) represented in normal scale, logarithmic scale and semilogarithmic scales.
The syntax presented here leads us to Figure 2-9, which compares the graph of the same function for the different scales. The graph is represented in the upper part with normal and logarithmic scales and at the bottom, the two semilogarithmic scales.
>> x = 0:0.01:3;
>> y = abs(exp(-0.5*x) .* sin(5*x));
>> subplot(2,2,1)
>> plot(x,y)
>> title('normal')
>> hold on
>> subplot(2,2,2)
>> loglog(x,y)
>> title('logarithmic')
>> subplot(2,2,3)
>> semilogx(x,y)
>> title('semilogarithmic in X axis')
>> subplot(2,2,4)
>> semilogy(x,y)
>> title('semilogarithmic in Y axis')
2.5 Polygons
MATLAB also allows drawing polygons in two dimensions. To do this, use the following commands:
EXERCISE 2-6
Represent a regular octagon (square enclosure), whose vertices are defined by pairs of values (Sine (t), Cos (t)), for values of t varying between 8p and 15p/8 separated by 2p/8. Use only the green color and put the 'Octagon' text at the point (-1/4,0) the inside of the figure.
The syntax for Figure 2-10 is the following:
>> t = [pi/8:2*pi/8:15*pi/8]';
>> x = sin (t);
>> y = cos (t);
>> fill(x,y,'g')
>> axis('square')
>> text(-0.25,0,'OCTOGON)'
2.6 Graphics Functions in Parametric Coordinates 2-D
We are going to see now how the program draws curves in parametric coordinates in the plane. We will discuss how you can get graphs of functions in which the variables x and y depend, in turn, on a parameter t. The command to use is Plot and all its variants, conveniently defining intervals of the parameter variation, and not the independent variable, as it was until now.
EXERCISE 2-7
Represent the curve (Epicycloid) whose parametric coordinates are: x = 4Cos [t] - Cos [4t], y = 4Sine [t] - Sine [4t], for t varying between 0 and 2p.
The syntax will be as follows:
>> t = 0:0.01:2 * pi;
>> x = 4 * cos (t) - cos(4*t);
>> y = 4 * sin (t) - sin(4*t);
>> plot(x,y)
The graph is presented in Figure 2-11, and represents the epicycloid.
EXERCISE 2-8
Represent the graph of the Cycloid whose parametric equations are x = t-2Sine (t), y = 1-2Cos (t), for t varying between - 3π. and 3π.
We will use the following syntax:
>> t = - 3 * pi:0.001:3 * pi;
>> plot(t-2 * sin (t), 1-2 * cos (t))
This gives you the graph in Figure 2-12.
2.7 Graphics Functions in Polar Coordinates
MATLAB enables the specific polar command, representing functions in polar coordinates. Its syntax is as follows:
EXERCISE 2-9
Represent the graph of the curve whose equation in polar coordinates is as follows: r = Sine (2a) Cos (2a) for a between 0 and 2π.
The following syntax leads us to the graph in Figure 2-13:
>> a = 0:0.01:2 * pi;
>> r = sin(2*a) .* cos(2*a);
>> polar(a, r)
EXERCISE 2-10
Represent the graph of the curve whose equation in polar coordinates is as follows: r = 4 (1 + Cos (a))for a between 0 and 2π, (called a cardioid).
To get the graph of Figure 2-14, representing the cardioid, use the following syntax:
>> a = 0:0.01:2 * pi;
>> r = 4 * (1 + cos (a));
>> polar(a, r)
>> title('CARDIOID')
EXERCISE 2-11
Represent the graph of the Lemniscate of Bernoulli whose equation isr = 4 (cos (2a) ^(1/2)) for 0 and 2, and the graph of the spiral of Archimedes whose equation is r = 3a, -4π<a< - 4π.
The first curve is represented in Figure 2-15, and is obtained by the following syntax:
>> a = 0:0.01:2 * pi;
>> r = 4 * (cos(2*a).^(1/2));
>> polar(a, r)
>> title(' Lemniscate of Bernoulli ')
The second curve is represented in Figure 2-16, and is obtained by the following syntax:
>> a = -4 * pi:0.01*pi:4 * pi;
>> r = 3 * a;
>> polar(a, r)
>> title('spiral of ARCHIMEDES')
2.8 Bars and Sectors Graphics. Histograms
MATLAB constructs bar graphs, sectors, Pareto diagrams and histograms of frequencies through the following commands:
Here are some examples:
>> bar([1 - 3, 4, 5, 2, 3])
>> pie([1, 3, 4, 5, 2, 3])
The graphics are in Figures 2-17 and 2-18.
Below, is a bar chart for 20 values of a normal between - 3 and 3:
>> x = -3:0.3:3;
>> bar(x, exp(-x.^2))
This generates the graph in Figure 2-19.
Figure 2-20 represents the step graph corresponding to the previous bar graph whose syntax is:
>> x = -3:0.3:3;
>> stairs(x,exp(-x.^2))
The histogram in Figure 2-21, corresponds to a vector of 10,000 normal random values in 60 bins between the values - 3 and 3 in0.1 increments:
>> x = -3:0.1:3;
>> y = randn(10000,1);
>> hist(y,x)
Figure 2-22 is a pie chart with two of its areas displaced, produced by using the syntax:
>> pie([1, 3, 4, 5, 2, 3], [0,0,0,0,1,1])
2.9 Statistical Errors and Arrow Graphics
There are commands in MATLAB which enable charting errors of a function, as well as certain types of arrow graphics to be discussed now. Some of these commands are described below:
Here are some examples below:
First of all, let's represent in Figure 2-23 a chart of errors for the density of a normal distribution (0,1) function, with the variable defined in 40 points between - 4 and 4, and errors are being defined by 40 uniform random values (0.10):
>> x = -4:.2:4;
>> y = (1/sqrt(2*pi))*exp(-(x.^2)/2);
>> e = rand(size(x))/10;
>> errorbar(x,y,e)
We also represent a graph of clusters corresponding to 50 normal random numbers (0.1) by using the syntax below in Figure 2-24:
>> y = randn (50,1); stem (y)
Figure 2-25 is an angular histogram with 1,000 a multiples of as a reason for normal random multiplicity (0.1), obtained from the following syntax:
>> x = 0:0.1:2 * pi;
>> y = randn (1000,1) * pi;
>> rose(y,x)
Now, Figure 2-26 presents a chart of arrows with center at the origin, corresponding to the eigenvalues of a normal (0,1) random square matrix of size 20 x 20. The syntax is as follows:
>> z = eig(randn (20,20));
>> compass(z)
Figure 2-27 is going to represent the chart of arrows in the previous example, but with the origin of the arrows in a horizontal straight line. The syntax is:
>> z = eig(randn (20,20));
>> feather(z)
Finally, we will draw on the same axes (Figure 2-28) the bessel(1,x),bessel(2,x)y bessel(3,x) functions for values of x between 0 and 12, separated uniformly in two-tenths. The purpose of the chart is to place three legends and three different types of stroke (normal, asterisks and circles, respectively) to the three functions. The syntax will be as follows:
>> x = 0:0.2:12;
>> plot (x, besselj(1,x), x, besselj(2,x),'*g', x, besselj(3,x), 'dr'),
>> legend('Bessel(1,x)','Bessel(2,x)','Bessel(3,x)'),