Skip to content

MATLAB - Plot Expression or Function

MATLAB - Plotting Expressions and Functions

Section titled “MATLAB - Plotting Expressions and Functions”

MATLAB provides a suite of powerful and flexible tools for visualizing mathematical functions. The modern approach favors using fplot for symbolic expressions and function handles, as it automatically determines the sampling points, resulting in smooth and accurate plots. This is often superior to the older method of manually creating data vectors.

The fplot function is designed to plot a function between specified limits. The most common way to provide the function is by using a function handle, which is an anonymous function starting with @.

fplot(f)
fplot(f, [xmin xmax])
fplot(___, LineSpec)
h = fplot(___)

Let’s plot the function y = sin(x) / x over the default interval [-5, 5].

% Define the function using a function handle
f = @(x) sin(x) ./ x; % Note the element-wise operator ./
figure; % Create a new figure window
fplot(f);
% Add labels and a title for a publication-quality plot
title('Plot of y = sin(x)/x');
xlabel('x');
ylabel('y');
grid on;

Expected Output Description: This code generates a 2D plot in a new figure window. The x-axis ranges from -5 to 5. The y-axis shows the corresponding values of sin(x)/x, which oscillates and dampens, starting from a value of 1 at x=0. The plot has a title, x and y axis labels, and a grid.

Example 2: Specifying Interval and Line Style

Section titled “Example 2: Specifying Interval and Line Style”

Let’s plot y = x^3 - 2x - 5 over the interval [-3, 3] using a red dashed line with circle markers.

% Define the polynomial function
f = @(x) x.^3 - 2*x - 5;
% Define the plot interval
xinterval = [-3, 3];
% Plot with a specified interval and LineSpec ('--or' for dashed orange-red line with circles)
figure;
fplot(f, xinterval, '--or');
title('Plot of a Cubic Polynomial');
xlabel('x-axis');
ylabel('y-axis');
legend('y = x^3 - 2x - 5', 'Location', 'northwest');
grid on;

Expected Output Description: The plot shows a cubic ‘S’ curve. The line is dashed, orange-red, and has circular markers at various points. The x-axis is limited to the range [-3, 3]. A legend appears in the top-left corner identifying the curve.

You can plot parametric curves in both 2D (fplot) and 3D (fplot3) by providing function handles for each coordinate.

% Define parametric functions for x and y
funx = @(t) cos(t);
funy = @(t) sin(t);
% Specify the interval for the parameter 't' to draw a full circle
tinterval = [0, 2*pi];
figure;
fplot(funx, funy, tinterval);
title('Parametric Plot of a Circle');
xlabel('x = cos(t)');
ylabel('y = sin(t)');
axis equal; % Ensures the circle is not distorted into an ellipse
grid on;

Expected Output Description: A perfect circle of radius 1, centered at the origin. The axis equal command ensures the scaling on both axes is the same, preventing a skewed appearance.

fplot3 works similarly, but requires three function handles for x(t), y(t), and z(t).

Let’s plot a helix and customize its appearance by working with the returned plot object.

% Define parametric functions for a helix
xt = @(t) cos(t); % x-coordinate
yt = @(t) sin(t); % y-coordinate
zt = @(t) t; % z-coordinate
% Specify the interval for the parameter 't'
tinterval = [0, 10*pi];
figure;
% Get the plot handle 'h' upon creation
h = fplot3(xt, yt, zt, tinterval);
% --- Customize the plot using the handle 'h' ---
h.LineWidth = 2.5;
h.Color = 'm'; % Magenta
title('3D Plot of a Helix');
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on;
view(30, 25); % Set a nice 3D viewing angle

Expected Output Description: A 3D plot showing a magenta-colored spiral (helix) with a line width of 2.5 points. The helix winds around the z-axis, extending from z=0 to z=10*pi. The view is angled to clearly show its 3D structure.

  • Always Label: Plots without titles and axis labels are ambiguous. Use title(), xlabel(), ylabel(), and legend() to make your plots informative.
  • Function Handles: Use function handles (@) for plotting mathematical expressions. They are efficient and clear.
  • Element-wise Operations: When defining functions, use element-wise operators (.*, ./, .^) to ensure your function works correctly with vector inputs, which fplot uses internally.
  • Accessibility: For black-and-white publications, don’t rely on color alone. Use different line styles (--, :, -.) and markers (o, x, +) to distinguish between different data series.
  • Saving Plots: Use exportgraphics(gca, 'myplot.png', 'Resolution', 300) for high-quality, high-resolution image files.