Open In App

Create Polar Axes in MATLAB

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Polar Axes/Coordinate system is a type of coordinate system which rather than using the traditional cartesian axes with X and Y axes uses polar coordinates, which consists of a magnitude vector (r) and its corresponding angle (\theta    ). The conversion from polar to cartesian coordinate is simple and the same is given below:

x = r \times \cos{\theta}\\ y = r \times \sin{\theta}

In this article, we shall see how to create Polar Axes in MATLAB and shall explore various options available with the same.

Creating Polar Axes

MATLAB provides a built-in function to create polar axes, the polaraxes function. By default, this creates a new figure component and creates an empty set of polar axes in it. Its syntax is:

pax = polaraxes(<optional arguments>)

The polaraxes function has no default parameters but, only a few optional parameters which we shall explore in later sections. Now, let us see the usage of the same with the help of some examples.

Example 1

In this example, we shall create an empty set of polar axes using the polaraxes function.

Matlab

% Code
polaraxes

                    

This will only create a set of polaraxes without any plots in it. Its output will be:

Output:

 

Example 2

In this example, we shall create a polar plot in the polar axes made in the previous example. To create a polar we use the polarplot function of MATLAB. 

Syntax

polarplot(axes, theta, r)

Here, 

axes -> Name of polar axes

theta -> Range of angle coordinate

r -> Magnitude of the vector  

We shall create a pattern of 3 petals using the mathematical relation

r = a\ \cos{(n\times\theta)}

Where a defined the radius of the nodes and n defines the number of nodes/petals created.

Matlab

% creating a uifigure component
fig = uifigure;
 
% creating a set of polar axes in figure fig
ax1 = polaraxes(fig);
 
% defining the range of angle coordinate
theta=0:.01:2*pi;
 
% using the relation to plot 3 petals
r= cos(3*theta);
 
% plotting the graph
polarplot(ax1,theta,r)

                    

This will create 3 petals with each of radius 1 unit. 

Output:

 

Example 3 

Making the Polar Axes the default axes

In the previous example, we saw that we had to specify the polar axes in which we have to plot the polar plot. This is syntactical in MATLAB as it needs a value for the polar axes object. However, there is an option provided by MATLAB which allows you to set particular polar axes as default for following plots, which saves the programmer from the recursive mentioning of the polar axes. 

Matlab

ax1 = polaraxes;
 
% making ax1 the default polaraxes
polaraxes(ax1)
 
%creating data for 2 different polar plots
theta=0:.01:2*pi;
r1= cos(3*theta);
r2 = 1.23*sin(3*theta);
 
%using hold command to plt multiple plots in same plot
hold on   
polarplot(theta,r1)
polarplot(theta,r2)

                    

In this example, we use polaraxes(ax1) form of the polar axes function which makes it the default axes for further polar plots. Next, we plot two petal functions to explain that once the default plot is set, there is no requirement to mention the polar axes name every time you plot some function in polar form.

The output of the above code is given below:

 

Conclusion

This article explained how to create Polar Axes in MATLAB and some options available with the same. For more modifications and an understanding of Polar Plot behavior in MATLAB, see changing the appearance and behaviors of Polar Axes.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads