How to Draw a Circle of Given Radius in MATLAB?


MATLAB is a tool that can perform various operations and tasks related to mathematics, engineering, and technology. For example, we can use MATLAB to draw different kind of shapes like circle, square, rectangle, triangle, etc. For this we just need to write a piece of codes in MATLAB programming and input the required parameters like radius of the circle, width and height of the rectangle, etc.

This tutorial will explain how you can draw a circle of the specified radius using MATLAB. As we know, in mathematics and geometry, a circle is a basic closed shape formed by joining several equidistant points from a fixed point, called center of the circle.

The distance between any point and the center of the circle is called radius of the circle. It is important to note that a circle is a 2D shape whose all points lie in the same plane.

Let us know the steps involved in drawing a circle in MATLAB.

How to Draw a Circle using MATLAB?

The step−by−step process to draw a circle of a specified radius in MATLAB is given below.

Step (1) − Open MATLAB command window.

Step (2) − Create a variable to store the radius of the circle.

Step (3) − Use a function to draw the circle. This can be done in the following two ways:

  • By using the rectangle function.

  • By creating a row vector.

Step (4) − Set the axis limits to equal, so that the circle will be displayed correctly.

Hence, drawing a circle of a specified radius is a simple four step process.

Now, let us take some examples in MATLAB to draw circles.

(1). Draw a Circle using “rectangle” function with “Curvature” Property in MATLAB

In MATLAB, there is a built−in function “rectangle” which has a “Curvature” property. Using this function and property, we can draw a circle with a specified radius.

Example (1)

The following MATLAB program demonstrates how we can draw a circle with a radius using the “rectangle” function.

% MATLAB code to draw a circle of radius R
% Specify the radius of the circle
R = 10;

% Create a figure to plot the circle
figure;

% Draw the circle using the rectangle function
rectangle('Position', [-R, -R, 2 * R, 2 * R], 'Curvature', [1, 1]);

% Set axis limit to equal to display the circle correctly
axis equal;

% Add a title to figure
title('Circle with Radius R');

Output

Code Explanation

In this MATLAB, we start by defining the radius of the circle “R”. Then, we create a figure to plot and display the circle. After that we use the “rectangle” function with “Curvature” property set to [1, 1] to draw the circle of radius “R”. To ensure the circle is displayed properly, we set the axis limits to equal.

When we execute this code, a circle of radius R = 10 is drawn and displayed in a figure.

(2). Draw a Circle by Creating a Row Vector in MATLAB

This is another method of drawing a circle in MATLAB. The following example program demonstrates how we can draw a circle with a specified radius by creating a row vector of evenly spaced points between two fixed points.

Example (2)

% MATLAB program to draw a circle
% Define the radius of the circle
R = 7;

% Create a row vector
v = linspace(0,2*pi,200);
 
% Generate x-coordinates
x = R * cos(v);
 
% Generate y-coordinate
y = R * sin(v);
 
% plot the circle.
plot(x, y);
 
% Set axis limit to equal to display circle correctly
axis equal;

% Add a title to the plot
title('Circle with Radius R');

Output

Code Explanation

In this MATLAB code, we start by defining the radius of the circle “R”. Then, we create a row vector using the “linespace” function which creates 200 evenly spaced points between two points.

After that we use this vector to generate the x−axis and y−axis coordinates to plot the circle. Then, we use these x and y coordinates to draw the circle using the “plot” function.

Finally, we set the axis limits to equal to display the circle correctly.

This is how we can draw a circle with a specified radius using a row vector.

(3). Draw a Circle with a Radius R Provided by the User

In MATLAB, we can also allow users to input the radius of the circle.

The following MATLAB program demonstrates how to create a circle by taking radius as the user input.

Example (3)

% MATLAB code to draw a circle of radius R
% Take input for the radius of the circle
R = input('Enter the Radius of the Circle = ');

% Create a figure to plot the circle
figure;

% Draw the circle using the rectangle function
rectangle('Position', [-R, -R, 2 * R, 2 * R], 'Curvature', [1, 1]);

% Set axis limit to equal to display the circle correctly
axis equal;

% Add a title to figure
title('Circle with Radius R');

Output

Enter the Radius of the Circle = 15

Code Explanation

The implementation of this MATLAB is same as the previous one. The only difference is that here we allow users to input the value of radius of the circle by using the “input” function.

Conclusion

In conclusion, MATLAB provides various methods to plot or draw a circle with a specified radius. In this tutorial, I have explained the two most common and simple methods of drawing a circle in MATLAB. You can try all these codes with different radius values to draw circles.

Updated on: 10-Oct-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements