Plot function between specified limits


[X,Y] = fplot(fun,limits,...)


fplot plots a function between specified limits. The function must be of the form y = f(x), where x is a vector whose range specifies the limits, and y is a vector the same size as x and contains the function's value at the points in x (see the first example). If the function returns more than one value for a given x, then y is a matrix whose columns contain each component of f(x) (see the second example).

fplot(fun,limits) plots fun between the limits specified by limits. limits is a vector specifying the x-axis limits ([xmin xmax]), or the x- and y-axes limits, ([xmin xmax ymin ymax]).

fun must be

  • The name of a function

  • A string with variable x that may be passed to eval, such as 'sin(x)', 'diric(x,10)', or '[sin(x),cos(x)]'

  • A function handle

The function f(x) must return a row vector for each element of vector x. For example, if f(x) returns [f1(x),f2(x),f3(x)] then for input [x1;x2] the function should return the matrix

f1(x1) f2(x1) f3(x1)
f1(x2) f2(x2) f3(x2)

fplot(fun,limits,LineSpec) plots fun using the line specification LineSpec.

fplot(fun,limits,tol) plots fun using the relative error tolerance tol (the default is 2e-3, i.e., 0.2 percent accuracy).

fplot(fun,limits,tol,LineSpec) plots fun using the relative error tolerance tol and a line specification that determines line type, marker symbol, and color. See LineSpec for more information.

fplot(fun,limits,n) with n >= 1 plots the function with a minimum of n+1 points. The default n is 1. The maximum step size is restricted to be (1/n)*(xmax-xmin).

fplot(fun,lims,...) accepts combinations of the optional arguments tol, n, and LineSpec, in any order.

fplot(axes_handle,...) plots into the axes with handle axes_handle instead of the current axes (gca).

[X,Y] = fplot(fun,limits,...) returns the abscissas and ordinates for fun in X and Y. No plot is drawn on the screen; however, you can plot the function using plot(X,Y).


MATLAB® Function Handle

Plot the hyperbolic tangent function from -2 to 2 using the MATLAB® function tanh.

fh = @tanh;

Function Handle Created From Anonymous Function

Create a function handle from an anonymous function. Plot the function from 0.01 to 0.1.

sn = @(x) sin(1./x);

Function Handle Created From Custom Function File

Create a file named myfun.m that contains the following code.

function Y = myfun(x)
Y(:,1) = 200*sin(x(:))./x(:);
Y(:,2) = x(:).^2;

Then, create a function handle pointing to myfun.

fh = @myfun;

Plot the function from -20 to 20.

fplot(fh,[-20 20])

More About

collapse all


fplot uses adaptive step control to produce a representative graph, concentrating its evaluation in regions where the function's rate of change is the greatest.

See Also

| | | |

Introduced before R2006a

Was this topic helpful?