MATLAB - Polar Plots
MATLAB - Modern Polar Plots
Section titled “MATLAB - Modern Polar Plots”A polar plot visualizes data in a polar coordinate system, defined by a radius (rho) and an angle (theta), instead of the Cartesian (x, y) system. This representation is exceptionally useful for data with inherent directionality or periodicity.
Common Applications:
- Engineering: Visualizing antenna radiation patterns or microphone pickup patterns.
- Physics: Representing vector fields and orbital mechanics.
- Data Science: Analyzing cyclical data, such as daily website traffic or monthly temperature variations.
MATLAB’s modern graphics system provides the polarplot function, a flexible and powerful tool for creating high-quality polar visualizations.
Creating Polar Plots with polarplot
Section titled “Creating Polar Plots with polarplot”The primary function is polarplot(theta, rho), where theta is a vector of angles (in radians) and rho is a vector of corresponding radii.
Example 1: A Simple Polar Plot
Section titled “Example 1: A Simple Polar Plot”Let’s plot a cardioid, given by the equation rho = 1 + cos(theta).
% Create a vector of angles from 0 to 2*pitheta = linspace(0, 2*pi, 200);
% Calculate the corresponding radiirho = 1 + cos(theta);
% Create the polar plotfigure;polarplot(theta, rho);This code generates a polar plot window. The resulting shape is a heart-like curve, known as a cardioid. It is symmetric about the horizontal axis, with its maximum radius of 2 at theta = 0 and its cusp (pinched point) at theta = pi where the radius is 0.
Customizing Plot Appearance
Section titled “Customizing Plot Appearance”You can customize the line style, color, and markers using a LineSpec string, similar to the plot function. For more advanced control, use name-value pairs.
Example 2: Customizing Line and Markers
Section titled “Example 2: Customizing Line and Markers”Let’s plot a four-leaf rose rho = sin(2*theta) with a thick, dashed red line and circle markers.
theta = linspace(0, 2*pi, 200);rho = sin(2 * theta);
% Plot with LineSpec and name-value pairspolarplot(theta, rho, '--ro', 'LineWidth', 2, 'MarkerFaceColor', 'y');LineSpec Explanation:
'--': Dashed line'r': Red color'o': Circle markers
Name-Value Pairs:
'LineWidth', 2: Sets the line thickness.'MarkerFaceColor', 'y': Fills the markers with yellow.
The output is a polar plot showing a flower-like shape with four ‘petals’ or lobes. The line is a thick, dashed red, with yellow-filled circles marking the points used to draw the curve.
Plotting Multiple Lines
Section titled “Plotting Multiple Lines”You can plot multiple datasets on the same polar axes to compare them. This can be done in two main ways.
Method 1: Multiple Arguments in polarplot
Section titled “Method 1: Multiple Arguments in polarplot”Provide multiple theta, rho, LineSpec triplets in a single call.
theta = linspace(0, 2*pi, 200);rho1 = sin(2 * theta); % Four-leaf roserho2 = cos(2 * theta); % Another four-leaf rose, rotated
polarplot(theta, rho1, '-r', theta, rho2, '--b');This generates a single plot with two overlapping shapes: a solid red four-leaf rose and a dashed blue four-leaf rose that is rotated by 45 degrees relative to the first.
Method 2: Using hold on
Section titled “Method 2: Using hold on”The hold on command tells MATLAB to add subsequent plots to the current axes instead of replacing them. This is useful for building up complex plots programmatically.
theta = linspace(0, 2*pi, 200);rho1 = sin(2*theta);rho2 = 0.5 + zeros(size(theta)); % A circle with radius 0.5
polarplot(theta, rho1, 'LineWidth', 2); % Plot the first linehold on;polarplot(theta, rho2, '--k'); % Plot the second line on the same axeshold off; % Best practice to release the holdThe result is a plot containing the blue four-leaf rose from the previous example, with a black dashed circle of radius 0.5 superimposed on it.
Enhancing Polar Plots
Section titled “Enhancing Polar Plots”A good plot is a well-labeled plot. Use title, legend, and functions to adjust the axes limits and ticks.
theta = linspace(0, 2*pi, 100);rho1 = 2*sin(3*theta);rho2 = 2*cos(3*theta);
polarplot(theta, rho1, theta, rho2);
% Enhance the plottitle('Comparison of 3-Leaf Roses');legend({'sin(3\theta)', 'cos(3\theta)'}, 'Location', 'bestoutside');
% Get the polar axes object to customize itpax = gca;pax.ThetaZeroLocation = 'top'; % Set 0 degrees to be at the toppax.ThetaDir = 'clockwise'; % Make angles increase clockwisepax.Rlim = [0 2.5]; % Set radius limitsThis code creates a highly customized plot showing two three-leaf roses. A title and legend clearly explain the content. The axes are modified so that the 0-degree angle is at the top (like a compass), angles increase clockwise, and the radial axis extends to 2.5, providing some space around the curves.
Mini-Project: Visualizing an Antenna Pattern
Section titled “Mini-Project: Visualizing an Antenna Pattern”Goal: Plot the radiation pattern of a simple half-wave dipole antenna. The normalized power intensity is given by rho = (cos(pi/2 * cos(theta)) / sin(theta))^2.
% Use a small epsilon to avoid division by zero at theta=0 and pieps = 0.001;theta = linspace(eps, 2*pi-eps, 200);
% Antenna pattern formularho = (cos(pi/2 * cos(theta)) ./ sin(theta)).^2;
polarplot(theta, rho);title('Half-Wave Dipole Antenna Radiation Pattern');
% Set radial limits to focus on the main lobespax = gca;pax.RLim = [0 1];This project connects the polarplot function to a real-world electrical engineering application, showing how it’s used to understand where an antenna transmits its power most effectively (the two large lobes at theta = pi/2 and theta = 3pi/2).