Create Plots

Plot with Symbolic Plotting Functions

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:

  • fplot to create 2-D plots of symbolic expressions, equations, or functions in Cartesian coordinates.

  • fplot3 to create 3-D parametric plots.

  • ezpolar to create plots in polar coordinates.

  • fsurf to create surface plots.

  • fcontour to create contour plots.

  • fmesh to create mesh plots.

Plot the symbolic expression sin(6x) by using fplot. By default, fplot uses the range -5<x<5.

syms x

Plot a symbolic expression or function in polar coordinates r (radius) and θ (polar angle) by using ezpolar. By default, ezpolar plots a symbolic expression or function over the interval 0<θ<2π.

Plot the symbolic expression sin(6t) in polar coordinates.

syms t

Plot Functions Numerically

As an alternative to plotting expressions symbolically, you can substitute symbolic variables with numeric values by using subs. Then, you can use these numeric values with plotting functions in MATLAB™.

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 plot U and V by using standard MATLAB plotting functions.

Create a plot of the vector field defined by the functions U(X,Y) and V(X,Y) by using the MATLAB quiver function.

quiver(X, Y, U, V)

Plot Multiple Symbolic Functions in One Graph

Plot several functions on one graph by adding the functions sequentially. After plotting the first function, add successive functions by using the hold on command. The hold on command keeps the existing plots. Without the hold on command, each new plot replaces any existing plot. After the hold on command, each new plot appears on top of existing plots. Switch back to the default behavior of replacing plots by using the hold off command.

Plot f=exsin(20x) using fplot. Show the bounds of f by superimposing plots of ex and -ex as dashed red lines. Set the title by using the DisplayName property of the object returned by fplot.

syms x y
f = exp(x)*sin(20*x)
f = sin(20x)exsin(20*x)*exp(x)
obj = fplot(f,[0 3]);
hold on
fplot(exp(x), [0 3], '--r')
fplot(-exp(x), [0 3], '--r')
hold off

Plot Multiple Symbolic Functions in One Figure

Display several functions side-by-side in one figure by dividing the figure window into several subplots using subplot. The command subplot(m,n,p) divides the figure into a m by n matrix of subplots and selects the subplot p. Display multiple plots in separate subplots by selecting the subplot and using plotting commands. Plotting into multiple subplots is useful for side-by-side comparisons of plots.

Compare plots of sin((x2+y2)/a) for a=10,20,50,100 by using subplot to create side-by-side subplots.

syms x y a
f = sin((x^2 + y^2)/a);

subplot(2, 2, 1)
fsurf(subs(f, a, 10))
title('a = 10')

subplot(2, 2, 2)
fsurf(subs(f, a, 20))
title('a = 20')

subplot(2, 2, 3)
fsurf(subs(f, a, 50))
title('a = 50')

subplot(2, 2, 4)
fsurf(subs(f, a, 100))
title('a = 100')

Combine Symbolic Function Plots and Numeric Data Plots

Plot numeric and symbolic data on the same graph by using MATLAB and Symbolic Math Toolbox functions together.

For numeric values of x between [-5,5], return a noisy sine curve by finding y=sin(x) and adding random values to y. View the noisy sine curve by using scatter to plot the points (x1,y1),(x2,y2),.

x = linspace(-5,5);
y = sin(x) + (-1).^randi(10, 1, 100).*rand(1, 100)./2;
scatter(x, y)

Show the underlying structure in the points by superimposing a plot of the sine function. First, use hold on to retain the scatter plot. Then, use fplot to plot the sine function.

hold on
syms t
hold off

Combine Numeric and Symbolic Plots in 3-D

Combine symbolic and numeric plots in 3-D by using MATLAB and Symbolic Math Toolbox plotting functions. Symbolic Math Toolbox provides these 3-D plotting functions:

  • fplot3 creates 3-D parameterized line plots.

  • fsurf creates 3-D surface plots.

  • fmesh creates 3-D mesh plots.

Create a spiral plot by using fplot3 to plot the parametric line


syms t
x = (1-t)*sin(100*t);
y = (1-t)*cos(100*t);
z = sqrt(1 - x^2 - y^2);
fplot3(x, y, z, [0 1])
title('Symbolic 3-D Parametric Line')

Superimpose a plot of a sphere with radius 1 and center at (0, 0, 0). Find points on the sphere numerically by using sphere. Plot the sphere by using mesh. The resulting plot shows the symbolic parametric line wrapped around the top hemisphere.

hold on
[X,Y,Z] = sphere;
mesh(X, Y, Z)
title('Symbolic Parametric Plot and a Sphere')
hold off