This is machine translation

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

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


Function plots in 2D

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


plotfunc2d(f1, f2, …, <Colors = [c1, c2, …]>, <attributes>)
plotfunc2d(f1, f2, …, x = xmin .. xmax, <Colors = [c1, c2, …]>, <attributes>)
plotfunc2d(f1, f2, …, x = xmin .. xmax, a = amin .. amax, <Colors = [c1, c2, …]>, <attributes>)


plotfunc2d(f1, f2, ...) generates a 2D plot of the univariate functions f1, f2 etc.

We strongly recommend reading the introduction to plotfunc2d in Section 2.1 ("2D Function Graphs") of the plot document.

The functions to be plotted must not contain any symbolic parameters apart from the variable x and the animation parameter a. Exact numerical values such as PI, sqrt(2) etc. are accepted.

Animations are triggered by specifying a range a = amin .. amax for a parameter a that is different from the indedependent variable x. Thus, in animations, both the x-range x = xmin .. xmax as well as the animation range a = amin .. amax must be specified. See Example 2.

Non-real function values are ignored. See Example 3.

Functions with singularities are handled. See Example 4 and Example 5. If unbounded functions are plotted, the vertical viewing range is clipped, automatically. An explicit vertical viewing range ymin .. ymax may be requested via ViewingBoxYRange = `y_{min}` .. `y_{max}` or YRange = `y_{min}` .. `y_{max}`.

Discontinuities and piecewise defined functions are handled. See  Example 6 and Example 7.

The plot library provides the routine plot::Function2d which allows to create a function graph as a graphical primitive, and to combine it with other graphical objects.

A variety of graphical attributes can be specified for fine tuning the graphical output. Such attributes are passed as equations AttributeName = AttributeValue to the plotfunc2d command.

Section 2.3 ("Attributes for plotfunc2d and plotfunc3d") provides an overview of the available attributes.

In particular, all attributes accepted by the graphical primitive plot::Function2d for function graphs are accepted by plotfunc2d. These attributes allow to specify the mesh for the numerical evaluation, the line width etc. The help page of plot::Function2d provides a concise list.

Further, all attributes accepted by plot::CoordinateSystem2d are accepted by plotfunc2d. These attributes include the specification of a viewing box, of the axes, their tick marks and tick labels, the coordinate type (such as linear versus logarithmic plots), grid lines etc. The help page of plot::CoordinateSystem2d provides a concise list.

Further, all attributes accepted by plot::Scene2d are accepted by plotfunc2d. These attributes include the specification of the layout of the graphical scene, the background color etc. The help page of plot::Scene2d provides a concise list.

Further, all attributes accepted by plot::Canvas are accepted by plotfunc2d. These attributes include the specification of the size of the graphics, of further layout parameters etc. The help page of plot::Canvas provides a concise list.

A graphical attribute such as Mesh = 500 (setting the number of mesh points for the numerical evaluation to 500) is applied to all functions in the call plotfunc2d(f1, f2, …). If separate attributes are appropriate, use the equivalent call

plot(plot::Function2d(f1, attr1), plot::Function2d(f2, attr2), …),

in which the attributes attr1, attr2 etc. can be set separately for each function.

Apart from few exceptions, plotfunc2d uses the standard default values for the graphical attributes (see the help page of plot::Function2d). The exceptions are:

  • If more than one function is plotted, plotfunc2d automatically creates a legend. Use an explicit LegendVisible = FALSE to suppress the legend.

  • AdaptiveMesh is set to 2, i.e., plotfunc2d uses adaptive function evaluation unless AdaptiveMesh = 0 is requested in plotfunc2d.

  • If a parameter range such as x = `x_{min}` .. `x_{max}` is passed to plotfunc2d, the name x is used as the title for the horizontal axis. Pass the attribute XAxisTitle if a different label for the horizontal axis is desired.

Environment Interactions

The function is sensitive to the environment variable DIGITS which determines the numerical working precision. Make sure that DIGITS is set to a sufficiently small value (such as the default value 10) to avoid the costs of computing unnecessarily precise plot data.


Example 1

The following command draws the sine function and the cosine function on the interval [- π, π]:

plotfunc2d(sin(x), cos(x), x = -PI .. PI):

With the attribute Scaling = Constrained, the y-axis has the same scale as the x-axis:

plotfunc2d(sin(x), cos(x), x = -PI .. PI, Scaling = Constrained):

Example 2

When creating an animation, a range for the independent variable x must be specified. An additional second range triggers the animation:

plotfunc2d(sin(x - a), cos(x + 2*a),
           x = -PI .. PI, a = -PI .. PI)

Example 3

Only real function values are plotted:

plotfunc2d(sqrt(1 - x), sqrt(x), x = -2 .. 2):

Example 4

The following functions have singularities in the specified interval:

plotfunc2d(x/(x^3 - 4*x), x = -5 .. 5):

plotfunc2d(1/sin(x), tan(x), x = 0 .. 2*PI):

Note that the automatic clipping may in some cases lead to an incorrect impression, such as the following image where the function appears to converge to about - 4.6 (but actually goes to - ∞ for small absolute values of x:


In this case, the asymptote which points to the pole is not seen because of the axis:

plotfunc2d(ln(abs(x)), Axes=Boxed)

Example 5

We specify a vertical range to which the function graph is restricted:

plotfunc2d(tan(x), x = -3 .. 3, YRange = -10 .. 10):

Example 6

The following function has a jump discontinuity:

plotfunc2d((x^2 - x)/(2*abs(x - 1)), x = -3 .. 3, 
           YRange = -3 .. 3)

Example 7

Piecewise defined functions are handled:

f := piecewise([x < 1, -x^2 + 1], [x >= 1, x]):
plotfunc2d(f(x), x = -3 .. 3, YRange = -3 .. 3,
           GridVisible = TRUE, TicksDistance = 1)

f := piecewise([x <= 0, x], [x > 0, 1/x]):
plotfunc2d(f(x), x = -3 .. 3, YRange = -3 .. 3,
           GridVisible = TRUE, TicksDistance = 1)

delete f:

Example 8

We use the attribute CoordinateType to create a logarithmic plot:

plotfunc2d(exp(x/10) + exp(-x), x = -1 .. 10,
           CoordinateType = LinLog)

We demonstrate various further graphical attributes in a doubly logarithmic plot:

plotfunc2d(x^2, x^3/(1 + x^(1/2)), x^3, 
           x = 1/10 .. 10^3,
           CoordinateType = LogLog,
           Axes = Boxed,
           DiscontinuitySearch = FALSE,
           GridVisible = TRUE,
           TicksNumber = None,
           TicksAt = [[10^i $ i = -1 .. 3], 
                      [10^i $ i in {-3, 0, 3, 6, 9}]


f1, f2, …

The functions: arithmetical expressions or piecewise objects in the indeterminate x and the animation parameter a. Alternatively, procedures that accept 1 input parameter x or 2 input parameters x, a and return a real numerical value when the input parameters are numerical.


The independent variable: an identifier or an indexed identifier.

xmin .. xmax

The plot range: xmin, xmax must be numerical real values or expressions of the animation parameter a. If not specified, the default range x = -5 .. 5 is used.


The animation parameter: an identifier or an indexed identifier.

amin .. amax

The animation range: amin, amax must be numerical real values.

c1, c2, …

The colors for f1, f2 etc.: RGB or RGBa values. The length of the color list needs not coincide with the number of functions in the plot. The colors are used cyclically; surplus colors are ignored.


An arbitrary number of graphical attributes. Each attribute is given by an equation of the form AttributeName = AttributeValue.

Return Values

MuPAD® graphics tool is called to render the graphical scene. The null() object is returned to the MuPAD session.

See Also

MuPAD Functions

MuPAD Graphical Primitives

More About

Was this topic helpful?