Documentation |
On this page… |
---|
Plot with Symbolic Plotting Functions Plot with MATLAB Plotting Functions Plot Multiple Symbolic Functions in One Graph |
MATLAB^{®} provides many techniques for plotting numerical data. Graphical capabilities of MATLAB include plotting tools, standard plotting functions, graphic manipulation and data exploration tools, and tools for printing and exporting graphics to standard formats. Symbolic Math Toolbox™ expands these graphical capabilities and lets you plot symbolic functions using:
ezplot to create 2-D plots of symbolic expressions, equations, or functions in Cartesian coordinates.
ezplot3 to create 3-D parametric plots. To create animated plots, use the animate option.
ezpolar that creates plots in polar coordinates.
ezsurf to create surface plots. The ezsurfc plotting function creates combined surface and contour plots.
ezcontour to create contour plots. The ezcontourf function creates filled contour plots.
ezmesh to create mesh plots. The ezmeshc function creates combined mesh and contour plots.
For example, plot the symbolic expression sin(6x) in Cartesian coordinates. By default, ezplot uses the range –2π < x < 2π :
syms x
ezplot(sin(6*x))
ezplot also can plot symbolic equations that contain two variables. To define an equation, use ==. For example, plot this trigonometric equation:
syms x y ezplot(sin(x) + sin(y) == sin(x*y))
When plotting a symbolic expression, equation, or function, ezplot uses the default 60-by-60 grid (mesh setting). The plotting function does not adapt the mesh setting around steep parts of a function plot or around singularities. (These parts are typically less smooth than the rest of a function plot.) Also, ezplot does not let you change the mesh setting.
To plot a symbolic expression or function in polar coordinates r (radius) and θ (polar angle), use the ezpolar plotting function. By default, ezpolar plots a symbolic expression or function over the domain 0 < θ < 2π . For example, plot the expression sin(6t) in polar coordinates:
syms t
ezpolar(sin(6*t))
When plotting a symbolic expression, you also can use the plotting functions provided by MATLAB. For example, plot the symbolic expression e^{x/2} sin(10x). First, use matlabFunction to convert the symbolic expression to a MATLAB function. The result is a function handle h that points to the resulting MATLAB function:
syms x h = matlabFunction(exp(x/2)*sin(10*x));
Now, plot the resulting MATLAB function by using one of the standard plotting functions that accept function handles as arguments. For example, use the fplot function:
fplot(h, [0 10]) hold on title('exp(x/2)*sin(10*x)') hold off
An alternative approach is to replace symbolic variables in an expression with numeric values by using the subs function. For example, in the following expressions u and v, substitute the symbolic variables x and y with the numeric values defined by meshgrid:
syms x y u = sin(x^2 + y^2); v = cos(x*y); [X, Y] = meshgrid(-1:.1:1,-1:.1:1); U = subs(u, [x y], {X,Y}); V = subs(v, [x y], {X,Y});
Now, you can use standard MATLAB plotting functions to plot the expressions U and V. For example, create the plot of a vector field defined by the functions U(X, Y) and V(X, Y):
quiver(X, Y, U, V)
To plot several symbolic functions in one graph, add them to the graph sequentially. To be able to add a new function plot to the graph that already contains a function plot, use the hold on command. This command retains the first function plot in the graph. Without this command, the system replaces the existing plot with the new one. Now, add new plots. Each new plot appears on top of the existing plots. While you use the hold on command, you also can change the elements of the graph (such as colors, line styles, line widths, titles) or add new elements. When you finish adding new function plots to a graph and modifying the graph elements, enter the hold off command:
syms x y ezplot(exp(x)*sin(20*x) - y, [0, 3, -20, 20]) hold on p1 = ezplot(exp(x) - y, [0, 3, -20, 20]); p1.Color = 'red'; p1.LineStyle = '--'; p1.LineWidth = 2; p2 = ezplot(-exp(x) - y, [0, 3, -20, 20]); p2.Color = 'red'; p2.LineStyle = '--'; p2.LineWidth = 2; title('exp(x)sin(20x)') hold off
To display several function plots in one figure without overlapping, divide a figure window into several rectangular panes (tiles). Then, you can display each function plot in its own pane. For example, you can assign different values to symbolic parameters of a function, and plot the function for each value of a parameter. Collecting such plots in one figure can help you compare the plots. To display multiple plots in the same window, use the subplot command:
subplot(m,n,p)
This command partitions the figure window into an m-by-n matrix of small subplots and selects the subplot p for the current plot. MATLAB numbers the subplots along the first row of the figure window, then the second row, and so on. For example, plot the expression sin(x^2 + y^2)/a for the following four values of the symbolic parameter a:
syms x y z = x^2 + y^2; subplot(2, 2, 1) ezsurf(sin(z/100)) subplot(2, 2, 2) ezsurf(sin(z/50)) subplot(2, 2, 3) ezsurf(sin(z/20)) subplot(2, 2, 4) ezsurf(sin(z/10))
The combined graphical capabilities of MATLAB and the Symbolic Math Toolbox software let you plot numeric data and symbolic functions in one graph. Suppose, you have two discrete data sets, x and y. Use the scatter plotting function to plot these data sets as a collection of points with coordinates (x1, y1), (x2, y2), ..., (x3, y3):
x = 0:pi/10:4*pi; y = sin(x) + (-1).^randi(10, 1, 41).*rand(1, 41)./2; scatter(x, y)
Now, suppose you want to plot the sine function on top of the scatter plot in the same graph. First, use the hold on command to retain the current plot in the figure. (Without this command, the symbolic plot that you are about to create replaces the numeric data plot.) Then, use ezplot to plot the sine function. To change the color or any other property of the plot, create the handle for the ezplot function call, and then use the set function:
hold on syms t ezplot(sin(t), [0 4*pi]) hold off
MATLAB provides the plotting functions that simplify the process of generating spheres, cylinders, ellipsoids, and so on. The Symbolic Math Toolbox software lets you create a symbolic function plot in the same graph with these volumes. For example, use the following commands to generate the spiral function plot wrapped around the top hemisphere. The animate option switches the ezplot3 function to animation mode. The red dot on the resulting graph moves along the spiral:
syms t x = (1-t)*sin(100*t); y = (1-t)*cos(100*t); z = sqrt(1 - x^2 - y^2); ezplot3(x, y, z, [0 1], 'animate') title('Symbolic Parametric Plot')
Add the sphere with radius 1 and the center at (0, 0, 0) to this graph. The sphere function generates the required sphere, and the mesh function creates a mesh plot for that sphere. Combining the plots clearly shows that the symbolic parametric function plot is wrapped around the top hemisphere:
hold on [X,Y,Z] = sphere; mesh(X, Y, Z) colormap(gray) title('Symbolic Parametric Plot and a Sphere') hold off