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:
Plot the symbolic expression by using fplot
. By default, fplot
uses the range .
syms x
fplot(sin(6*x))
Plot a symbolic expression or function in polar coordinates (radius) and (polar angle) by using fpolarplot
. By default, fpolarplot
plots a symbolic expression or function over the interval .
Plot the symbolic expression in polar coordinates.
syms t
fpolarplot(sin(6*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 using fplot
. Show the bounds of by superimposing plots of and 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 =
obj = fplot(f,[0 3]); hold on fplot(exp(x),[0 3],"--r") fplot(-exp(x),[0 3],"--r") title(obj.DisplayName) 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 for 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 between , return a noisy sine curve by finding and adding random values to . View the noisy sine curve by using scatter
to plot the points , 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
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) colormap(gray) title("Symbolic Parametric Plot and a Sphere") hold off