| Contents | Index |
| On this page… |
|---|
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 ex/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]);
set(p1,'Color','red', 'LineStyle', '--', 'LineWidth', 2)
p2 = ezplot(-exp(x) - y, [0, 3, -20, 20]);
set(p2,'Color','red', 'LineStyle', '--', '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. By default, MATLAB does not use a different color for a new function; the sine function appears in blue. 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 p = ezplot(sin(t), [0 4*pi]); set(p,'Color','red')

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')

Plotting a symbolic function can help you visualize and explore the features of the function. Graphical representation of a symbolic function can also help you communicate your ideas or results. MATLAB displays a graph in a special window called a figure window. This window provides interactive tools for further exploration of a function or data plot.

Interactive data exploration tools are available in the figure toolbar and also from the Tools menu. By default, a figure window displays one toolbar that provides shortcuts to the most common operations. You can enable two other toolbars from the View menu. When exploring symbolic function plots, use the same operations as you would for the numeric data plots. For example:
Zoom in and out on particular parts of a graph (
). Zooming allows you
to see small features of a function plot. Zooming behaves differently
for 2-D or 3-D views. For more information, see Enlarging the View.
Shift the view of the graph with the pan tool (
). Panning is useful when
you have zoomed in on a graph and want to move around the plot to
view different portions. For more information, see Panning — Shifting Your View of the Graph.
Rotate 3-D graphs (
). Rotating 3-D graphs
allows you to see more features of the surface and mesh function plots.
For more information, see Rotate 3D — Interactive Rotation of 3-D Views.
Display particular data values on a graph and export
them to MATLAB workspace variables (
). For more information,
see Data Cursor — Displaying Data Values Interactively.
For more information about data exploration tools available in MATLAB, see Ways to Explore Graphical Data.
MATLAB supports the following two approaches for editing graphs:
Interactive editing lets you use the mouse to select and edit objects on a graph.
Command-line editing lets you use MATLAB commands to edit graphs.
These approaches work for graphs that display numeric data plots, symbolic function plots, or combined plots.
To enable the interactive plot editing mode in the MATLAB figure
window, click the Edit Plot button (
) or select Tools > Edit Plot from the main menu. If you enable plot editing mode
in the MATLAB figure window, you can perform point-and-click
editing of your graph. In this mode, you can modify the appearance
of a graphics object by double-clicking the object and changing the
values of its properties. For more information about interactive editing,
see Working in Plot Edit Mode.
The complete collection of properties is accessible through a graphical user interface called the Property Editor. To open a graph in the Property Editor window:
Enable plot editing mode in the MATLAB figure window.
Double-click any element on the graph.
For information about editing object properties in plot editing mode, see The Property Editor.
If you prefer to work from the MATLAB command line or if you want to create a code file, you can edit graphs by using MATLAB commands. For information about command-line graph editing, see Handle Graphics Objects.
Also, you can combine the interactive and command-line editing approaches to achieve the look you want for the graphs you create.
After you create, edit, and explore a function plot, you might want to save the result. MATLAB provides three different ways to save graphs:
Save a graph as a MATLAB FIG-file (a binary format). The FIG-file stores all information about a graph, including function plots, graph data, annotations, data tips, menus and other controls. You can open the FIG-file only with MATLAB. For more information, see Saving a Graph in FIG-File Format.
Export a graph to a different file format. When saving a graph, you can choose a file format other than FIG. For example, you can export your graphs to EPS, JPEG, PNG, BMP, TIFF, PDF, and other file formats. You can open the exported file in an appropriate application. For more information, see Saving to a Different Format — Exporting Figures.
Print a graph on paper or print it to file. To ensure the correct plot size, position, alignment, paper size and orientation, use Print Preview. For more information, see Printing Figures.
Generate a MATLAB file from a graph. You can use the generated code to reproduce the same graph or create a similar graph using different data. This approach is useful for generating MATLAB code for work that you have performed interactively with the plotting tools. For more information, see Generating a MATLAB File to Recreate a Graph.
![]() | Special Functions of Applied Mathematics | Generating Code from Symbolic Expressions | ![]() |

See how symbolic computations can help you find analytical solutions to math and engineering problems.
Get free kit| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |