Analytical Plotting with Symbolic Math Toolbox

The fplot family accepts symbolic expressions and equations as inputs enabling easy analytical plotting without explicitly generating numerical data.

This example features the following functions

  • fplot

  • fplot3

  • fsurf

  • fcontour

  • fmesh

  • fimplicit

  • fimplicit3

Interactively plot functions of one variable

syms x
fplot(sin(exp(x)))

fplot([sin(x),cos(x),tan(x)])

Generate an implicit plot of a symbolic expression

syms x y 
r = 1:10;
fimplicit(x^2+y^2 == r)

Parametrically explore multiple functions with subs

syms x a
expression = sin(exp(x/a))
expression = sin(ex/a)sin(exp(x/a))
fplot(subs(expression,a,[1,2,4]))
hold off
legend show

Mix Symbolic and Numeric techniques to develop mathematical models

Explore the spline approximation to f(x)=x*exp(-x)*sin(5*x)-2

syms f(x)
f(x) = x*exp(-x)*sin(5*x) -2;
xs = 0:1/3:3;
ys = double(subs(f,xs));
fplot(f,[0,3])
hold on
plot(xs,ys,'*k','DisplayName','Data Points')
fplot(@(x) spline(xs,ys,x),[0 3],'DisplayName','Spline interpolant')
hold off
grid on
legend show

Explore Gibbs Phenomenon

syms x
n = 5;
approx = cumsum(sin((1:2:2*n-1)*x)./(1:2:2*n-1));
fplot(approx,'LineWidth',1)

Plotting the results of computations

With symbolic input, we can perform computations and plot the results.

syms f(a,x)
assume(a>0);
f(a,x) = a*x^2+a^2*x+2*sqrt(a)
f(a, x) = ax2+a2x+2aa*x^2 + a^2*x + 2*sqrt(a)
x_min = solve(diff(f,x), x)
x_min = 

-a2-a/2

fplot(f(a,x_min),[0 5])
xlabel 'a'
title 'Minimum value of f depending on a'

assume(a,'clear')

Visualize Series and Summations

syms x
t6 = taylor(cos(x),x,'Order',6)
t6 = 

x424-x22+1x^4/24 - x^2/2 + 1

t8 = taylor(cos(x),x,'Order',8)
t8 = 

-x6720+x424-x22+1- x^6/720 + x^4/24 - x^2/2 + 1

fplot([cos(x) t6 t8])
xlim([-4 4])
ylim([-1.5, 1.5])
title '6th and 8th Order Taylor Series approximations to cos(x)'
legend show

Explore functions along with their integrals and derivatives

Some symbolic expressions cannot be converted to MATLAB functions.

syms x
f = x^x
f = xxx^x
int(f,x)
ans = 

xxdxint(x^x, x)

diff(f,x)
ans = xxx-1+xxlog(x)x*x^(x - 1) + x^x*log(x)
fplot([f, int(f,x), diff(f,x)],[0 2])
legend show

Generate parametric curves without explicit numerical data

Curves (x(t),y(t))or (x(t),y(t),z(t)) can be drawn by fplot or fplot3 (just like with plot or plot3 for numerical data) :

syms t
fplot3(sin(t)-t/2,cos(t),t^3,'--','LineWidth',2.5)
view([-45 45])

Generate surfaces z=f(x,y) without meshgrid

syms x y
fsurf(sin(x)+sin(y)-(x^2+y^2)/20,'ShowContours','on')
set(camlight,'Color',[0.5 0.5 1]);
set(camlight('left'),'Color', [1 0.6 0.6]);
set(camlight('left'),'Color', [1 0.6 0.6]);
set(camlight('right'),'Color', [0.8 0.8 0.6]);
material shiny
view(-19,56)

Generate numeric streamlines from analytical derivatives using meshgrid

syms x y
u = diff(diff(sin(x^2+y^2)))
u = 2cos(x2+y2)-4x2sin(x2+y2)2*cos(x^2 + y^2) - 4*x^2*sin(x^2 + y^2)
v = diff(diff(cos(x^2+y^2)))
v = -2sin(x2+y2)-4x2cos(x2+y2)- 2*sin(x^2 + y^2) - 4*x^2*cos(x^2 + y^2)
[X, Y] = meshgrid(-3:.1:3,-2:.1:2);
U = subs(u, [x y], {X,Y});
V = subs(v, [x y], {X,Y});

startx = -3:0.1:3;
starty = zeros(size(startx));
h = streamline(X,Y,U,V,X,Y);
for i=1:length(h)-1
    h(i).Color = [rand() rand() rand()];
end

Adaptive visualization

Like fplot, fsurf evaluates your symbolic expression more densely where needed, to more accurately show curved areas and asymptotic regions.

fsurf(log(x) + exp(y), [-2 2])

Create Implicit surfaces

Plot the implicit surface 1/x2-1/y2+1/z2=0. Specify an output to make fimplicit3 return the plot object.

syms x y z
f = 1/x^2 - 1/y^2 + 1/z^2;
fimplicit3(f)

Visualize multivariate surfaces

Unlike pure symbolic functions (like int, diff, solve), fsurf does not allow specifying the order of variables. To set the order, use symbolic functions:

syms f(t) x(u,v) y(u,v) z(u,v)
f(t) = sin(t)*exp(-t^2/3)+1.5;
x(u,v) = u
x(u, v) = uu
y(u,v) = f(u)*sin(v)
y(u, v) = 

sin(v)e-u23sin(u)+32sin(v)*(exp((-u^2/3))*sin(u) + sym(3/2))

z(u,v) = f(u)*cos(v)
z(u, v) = 

cos(v)e-u23sin(u)+32cos(v)*(exp((-u^2/3))*sin(u) + sym(3/2))

fsurf(x,y,z,[-5 5.1 0 2*pi])

Use fmesh for 3D mesh plots

Plot the parameterized mesh

x=r*cos(s)*sin(t)y=r*sin(s)*sin(t)z=r*cos(t)

where r=8+sin(7*s+5*t)

syms s t
r = 8 + sin(7*s + 5*t);
x = r*cos(s)*sin(t);
y = r*sin(s)*sin(t);
z = r*cos(t);
fmesh(x, y, z, [0 2*pi 0 pi], 'Linewidth', 2)
axis equal

Generate a contour plot of a symbolic expression or equation

syms x y g(x,y)
g(x,y) = x^3-4*x-y^2
g(x, y) = x3-4x-y2x^3 - 4*x - y^2
fcontour(g,[-3 3 -4 4],'LevelList',-6:6)
title 'Some Elliptic Curves'