Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

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

clear all
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

clear all 
syms x a
eqn = sin(exp(x/a))
eqn = 

fplot(subs(eqn,a,[1,2,4]))
hold off
legend show

Mix Symbolic and Numeric techniques to develop mathematical models

Explore the spline approximation to

clear
syms x 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) = 

x_min = solve(diff(f,x), x)
x_min = 

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

Visualize Series and Summations

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

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

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.

clear 
syms x
f = x^x
f = 

int(f,x)
ans = 

diff(f,x)
ans = 

fplot([f, int(f,x), diff(f,x)],[0 2])
legend show

Generate parametric curves without explicit numerical data

Curves or can be drawn by fplot or fplot3 (just like with plot or plot3 for numerical data) :

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

Generate surfaces 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

clear
figure
syms x y
u = diff(diff(sin(x^2+y^2)))
u = 

v = diff(diff(cos(x^2+y^2)))
v = 

[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 . 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) = 

y(u,v) = f(u)*sin(v)
y(u, v) = 

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

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

Use fmesh for 3D mesh plots

Plot the parameterized mesh

where

clear
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

clear
syms x y g(x,y)
g(x,y) = x^3-4*x-y^2
g(x, y) = 

fcontour(g,[-3 3 -4 4],'LevelList',-6:6)
title 'Some Elliptic Curves'

Was this topic helpful?