fplot
Plot symbolic expression or function
Syntax
Description
fplot(
plots f
,[xmin xmax]
)f
over
the interval [xmin xmax]
.
fplot(
plots xt = x(t) and yt = y(t) over
the specified range xt
,yt
,[tmin
tmax]
)[tmin tmax]
.
fplot(___,
specifies
line properties using one or more Name,Value
)Name,Value
pair
arguments. Use this option with any of the input argument combinations
in the previous syntaxes. Name,Value
pair settings
apply to all the lines plotted. To set options for individual lines,
use the objects returned by fplot
.
fplot(
plots
into the axes specified by ax
,___)ax
instead of the
current axes gca
.
returns
a function line object or parameterized line object, depending on
the type of plot. Use the object to query and modify properties of
a specific line. For details, see FunctionLine Properties and ParameterizedFunctionLine Properties.fp
= fplot(___)
Examples
Plot Symbolic Expression
Plot tan(x)
over the default range of [-5 5]
. fplot
shows poles by default. For details, see the ShowPoles
argument in Name-Value Pair Arguments.
syms x
fplot(tan(x))
Plot Symbolic Function
Plot the symbolic function over the default range [-5 5]
.
syms f(x)
f(x) = cos(x);
fplot(f)
Plot Parametric Curve
Plot the parametric curve and .
syms t
x = cos(3*t);
y = sin(2*t);
fplot(x,y)
Specify Plotting Interval
Plot over by specifying the plotting interval as the second input to fplot
.
syms x
fplot(sin(x),[-pi/2 pi/2])
Plot Multiple Lines on Same Figure
You can plot multiple lines either by passing the inputs as a vector or by using hold on
to successively plot on the same figure. If you specify LineSpec
and Name-Value arguments, they apply to all lines. To set options for individual plots, use the function handles returned by fplot
.
Divide a figure into two subplots using subplot
. On the first subplot, plot and using vector input. On the second subplot, plot and using hold on
.
syms x subplot(2,1,1) fplot([sin(x) cos(x)]) title('Multiple Lines Using Vector Inputs') subplot(2,1,2) fplot(sin(x)) hold on fplot(cos(x)) title('Multiple Lines Using hold on Command') hold off
Change Line Properties and Display Markers
Plot three sine curves with a phase shift between each line. For the first line, use a linewidth of 2
. For the second, specify a dashed red line style with circle markers. For the third, specify a cyan, dash-dot line style with asterisk markers. Display the legend.
syms x fplot(sin(x+pi/5),'Linewidth',2) hold on fplot(sin(x-pi/5),'--or') fplot(sin(x),'-.*c') legend('show','Location','best') hold off
Control Resolution of Plot
Control the resolution of a plot by using the MeshDensity
option. Increasing MeshDensity
can make smoother, more accurate plots, while decreasing it can increase plotting speed.
Divide a figure into two by using subplot
. In the first subplot, plot a step function from x = 2.1
to x = 2.15
. The plot's resolution is too low to detect the step function. Fix this issue by increasing MeshDensity
to 39
in the second subplot. The plot now detects the step function and shows that by increasing MeshDensity
you increased the plot's resolution.
syms x stepFn = rectangularPulse(2.1, 2.15, x); subplot(2,1,1) fplot(stepFn); title('Default MeshDensity = 23') subplot(2,1,2) fplot(stepFn,'MeshDensity',39); title('Increased MeshDensity = 39')
Modify Plot After Creation
Plot sin(x)
. Specify an output to make fplot
return the plot object.
syms x
h = fplot(sin(x))
h = FunctionLine with properties: Function: sin(x) Color: [0 0.4470 0.7410] LineStyle: '-' LineWidth: 0.5000 Show all properties
Change the default blue line to a dashed red line by using dot notation to set properties. Similarly, add 'x'
markers and set the marker color to blue.
h.LineStyle = '--'; h.Color = 'r'; h.Marker = 'x'; h.MarkerEdgeColor = 'b';
Add Title and Axis Labels and Format Ticks
For from to , plot . Add a title and axis labels. Create the x-axis ticks by spanning the x-axis limits at intervals of pi/2
. Display these ticks by using the XTick
property. Create x-axis labels by using arrayfun
to apply texlabel
to S
. Display these labels by using the XTickLabel
property.
To use LaTeX in plots, see latex
.
syms x fplot(sin(x),[-2*pi 2*pi]) grid on title('sin(x) from -2\pi to 2\pi') xlabel('x') ylabel('y') ax = gca; S = sym(ax.XLim(1):pi/2:ax.XLim(2)); ax.XTick = double(S); ax.XTickLabel = arrayfun(@texlabel,S,'UniformOutput',false);
Re-evaluation on Zoom
When you zoom into a plot, fplot
re-evaluates
the plot automatically. This re-evaluation on zoom reveals hidden
detail at smaller scales.
Plot x^3*sin(1/x)
for -2 < x
< 2
and -0.02 < y < 0.02
. Zoom
in on the plot using zoom
and redraw the plot
using drawnow
. Because of re-evaluation on zoom, fplot
reveals
smaller-scale detail. Repeat the zoom 6 times to view smaller-scale
details. To play the animation, click the image.
syms x fplot(x^3*sin(1/x)); axis([-2 2 -0.02 0.02]); for i=1:6 zoom(1.7) pause(0.5) end
Create Animations
Create animations by changing the displayed expression using
the Function
, XFunction
, and YFunction
properties
and then by using drawnow
to update the plot.
To export to GIF, see imwrite
.
By varying the variable i from 0.1 to 3, animate the parametric curve
To play the animation, click the image.
syms t fp = fplot(t, t); axis([-15 15 -15 15]) for i=0.1:0.05:3 fp.XFunction = i.*t.*sin(i*t); fp.YFunction = i.*t.*cos(i*t); drawnow end
Input Arguments
Output Arguments
Tips
If
fplot
detects a finite number of discontinuities inf
, thenfplot
expands the range to show them.
Version History
Introduced in R2016a