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.
Plotting a Function with fplot
Section titled “Plotting a Function with fplot”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 @.
Syntax
Section titled “Syntax”fplot(f)fplot(f, [xmin xmax])fplot(___, LineSpec)h = fplot(___)Example 1: Plotting a Simple Function
Section titled “Example 1: Plotting a Simple Function”Let’s plot the function y = sin(x) / x over the default interval [-5, 5].
% Define the function using a function handlef = @(x) sin(x) ./ x; % Note the element-wise operator ./
figure; % Create a new figure windowfplot(f);
% Add labels and a title for a publication-quality plottitle('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 functionf = @(x) x.^3 - 2*x - 5;
% Define the plot intervalxinterval = [-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.
Plotting Parametric Curves
Section titled “Plotting Parametric Curves”You can plot parametric curves in both 2D (fplot) and 3D (fplot3) by providing function handles for each coordinate.
Example 3: A 2D Parametric Circle
Section titled “Example 3: A 2D Parametric Circle”% Define parametric functions for x and yfunx = @(t) cos(t);funy = @(t) sin(t);
% Specify the interval for the parameter 't' to draw a full circletinterval = [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 ellipsegrid 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.
Plotting a 3D Curve with fplot3
Section titled “Plotting a 3D Curve with fplot3”fplot3 works similarly, but requires three function handles for x(t), y(t), and z(t).
Example 4: A 3D Helix
Section titled “Example 4: A 3D Helix”Let’s plot a helix and customize its appearance by working with the returned plot object.
% Define parametric functions for a helixxt = @(t) cos(t); % x-coordinateyt = @(t) sin(t); % y-coordinatezt = @(t) t; % z-coordinate
% Specify the interval for the parameter 't'tinterval = [0, 10*pi];
figure;% Get the plot handle 'h' upon creationh = 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 angleExpected 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.
Best Practices for Plotting
Section titled “Best Practices for Plotting”- Always Label: Plots without titles and axis labels are ambiguous. Use
title(),xlabel(),ylabel(), andlegend()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, whichfplotuses 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.