Main Content

Create Plots of Symbolic Expressions

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.

  • fpolarplot to create plots in polar coordinates. (since R2024a)

  • 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
fplot(sin(6*x))

Figure contains an axes object. The axes object contains an object of type functionline.

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

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

syms t
fpolarplot(sin(6*t))

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type functionline.

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)

Figure contains an axes object. The axes object contains an object of type quiver.

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)ex
obj = fplot(f,[0 3]);
hold on
fplot(exp(x),[0 3],"--r")
fplot(-exp(x),[0 3],"--r")
title(obj.DisplayName)
hold off

Figure contains an axes object. The axes object with title sin ( 20 blank x ) blank exp ( x ) contains 3 objects of type functionline.

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

Figure contains 4 axes objects. Axes object 1 with title a = 10 contains an object of type functionsurface. Axes object 2 with title a = 20 contains an object of type functionsurface. Axes object 3 with title a = 50 contains an object of type functionsurface. Axes object 4 with title a = 100 contains an object of type functionsurface.

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), and so on.

figure
rng("default")
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
fplot(sin(t))
hold off

Figure contains an axes object. The axes object contains 2 objects of type scatter, functionline.

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

x=(1-t)sin(100t)y=(1-t)cos(100t)z=1-x2-y2.

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

Figure contains an axes object. The axes object with title Symbolic 3-D Parametric Line contains an object of type parameterizedfunctionline.

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)
colormap(gray)
title("Symbolic Parametric Plot and a Sphere")
hold off

Figure contains an axes object. The axes object with title Symbolic Parametric Plot and a Sphere contains 2 objects of type parameterizedfunctionline, surface.