Analytical Plotting with Symbolic Math Toolbox
Symbolic Math Toolbox™ provides analytical plotting of mathematical expressions without explicitly generating numerical data. These plots can be in 2-D or 3-D as lines, curves, contours, surfaces, or meshes.
These examples feature the following graphics functions that accept symbolic functions, expressions, and equations as inputs:
fplot
fimplicit
fcontour
fplot3
fsurf
fmesh
fimplicit3
Plot Explicit Functions Using fplot
Plot the function .
syms x
fplot(sin(exp(x)))
Plot the trigonometric functions , , and simultaneously.
fplot([sin(x),cos(x),tan(x)])
Plot a Function Defined by for Various Values of
Plot the function for , and .
syms x a expr = sin(exp(x/a)); fplot(subs(expr,a,[1,2,4])) legend show
Plot the Derivative and Integral of a Function
Plot a function , its derivative , and its integral .
syms f(x)
f(x) = x*(1 + x) + 2
f(x) =
f_diff = diff(f(x),x)
f_diff =
f_int = int(f(x),x)
f_int =
fplot([f,f_diff,f_int]) legend({'$f(x)$','$df(x)/dx$','$\int f(x)dx$'},'Interpreter','latex','FontSize',12)
Plot a Function with as the Horizontal Axis
Find the that minimizes a function by solving the differential equation .
syms g(x,a);
assume(a>0);
g(x,a) = a*x*(a + x) + 2*sqrt(a)
g(x, a) =
x0 = solve(diff(g,x),x)
x0 =
Plot the minimum value of for from 0 to 5.
fplot(g(x0,a),[0 5]) xlabel('a') title('Minimum Value of $g(x_0,a)$ Depending on $a$','interpreter','latex')
Plot an Implicit Function Using fimplicit
Plot circles defined by with radius as the integers from 1 to 10.
syms x y r = 1:10; fimplicit(x^2 + y^2 == r.^2,[-10 10]) axis square;
Plot Contours of a Function Using fcontour
Plot contours of the function for contour levels from –6 to 6.
syms x y f(x,y) f(x,y) = x^3 - 4*x - y^2; fcontour(f,[-3 3 -4 4],'LevelList',-6:6); colorbar title 'Contour of Some Elliptic Curves'
Plot an Analytic Function and Its Approximation Using Spline Interpolant
Plot the analytic function .
syms f(x)
f(x) = x*exp(-x)*sin(5*x) -2;
fplot(f,[0,3])
Create a few data points from the analytic function.
xs = 0:1/3:3; ys = double(subs(f,xs));
Plot the data points and the spline interpolant that approximates the analytic function.
hold on plot(xs,ys,'*k','DisplayName','Data Points') fplot(@(x) spline(xs,ys,x),[0 3],'DisplayName','Spline interpolant') grid on legend show hold off
Plot Taylor Approximations of a Function
Find the Taylor expansion of near up to 5th and 7th orders.
syms x t5 = taylor(cos(x),x,'Order',5)
t5 =
t7 = taylor(cos(x),x,'Order',7)
t7 =
Plot and its Taylor approximations.
fplot(cos(x)) hold on; fplot([t5 t7],'--') axis([-4 4 -1.5 1.5]) title('Taylor Series Approximations of cos(x) up to 5th and 7th Order') legend show hold off;
Plot the Fourier Series Approximation of a Square Wave
A square wave of period and amplitude can be approximated by the Fourier series expansion
Plot a square wave with period and amplitude .
syms t y(t) y(t) = piecewise(0 < mod(t,2*pi) <= pi, pi/4, pi < mod(t,2*pi) <= 2*pi, -pi/4); fplot(y)
Plot the Fourier series approximation of the square wave.
hold on; n = 6; yFourier = cumsum(sin((1:2:2*n-1)*t)./(1:2:2*n-1)); fplot(yFourier,'LineWidth',1) hold off
The Fourier series approximation overshoots at a jump discontinuity and the "ringing" does not die out as more terms are added to the approximation. This behavior is also known as the Gibbs phenomenon.
Plot a Parametric Curve Using fplot3
Plot a helix that is defined by for from –10 to 10.
syms t fplot3(sin(t),cos(t),t/4,[-10 10],'LineWidth',2) view([-45 45])
Plot a Surface Defined by Using fsurf
Plot a surface defined by . Analytical plotting using fsurf
(without generating numerical data) shows the curved areas and asymptotic regions near .
syms x y fsurf(log(x) + exp(y),[0 2 -1 3]) xlabel('x')
Plot a Multivariate Surface Using fsurf
Plot a multivariate surface defined by
where .
Set the plot interval of from –5 to 5 and from 0 to 2.
syms f(u) x(u,v) y(u,v) z(u,v) f(u) = sin(u)*exp(-u^2/3)+1.5; x(u,v) = u; y(u,v) = f(u)*sin(v); z(u,v) = f(u)*cos(v); fsurf(x,y,z,[-5 5 0 2*pi])
Plot a Multivariate Surface Using fmesh
Plot a multivariate surface defined by
where . Show the plotted surface as meshes by using fmesh
. Set the plot interval of from 0 to 2 and from 0 to .
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
Plot an Implicit Surface Using fimplicit3
Plot the implicit surface .
syms x y z f = 1/x^2 - 1/y^2 + 1/z^2; fimplicit3(f)
Plot the Contours and Gradient of a Surface
Plot the surface using fsurf
. You can show the contours on the same graph by setting 'ShowContours'
to 'on'
.
syms x y f = sin(x)+sin(y)-(x^2+y^2)/20
f =
fsurf(f,'ShowContours','on') view(-19,56)
Next, plot the contours on a separate graph with finer contour lines.
fcontour(f,[-5 5 -5 5],'LevelStep',0.1,'Fill','on') colorbar
Find the gradient of the surface. Create 2-D grids using meshgrid
and substitute the grid coordinates to evaluate the gradient numerically. Show the gradient using quiver
.
hold on
Fgrad = gradient(f,[x,y])
Fgrad =
[xgrid,ygrid] = meshgrid(-5:5,-5:5); Fx = subs(Fgrad(1),{x,y},{xgrid,ygrid}); Fy = subs(Fgrad(2),{x,y},{xgrid,ygrid}); quiver(xgrid,ygrid,Fx,Fy,'k') hold off