How to Draw a Circle of Given Radius R in MATLAB?
Last Updated :
01 Dec, 2022
A closed plane figure, which is formed by the set of all those points which are equidistant from a fixed point in the same plane, is called a Circle. Now we will draw a circle of a given radius R. To draw a circle below are the points or procedures to remember.
Steps:
- declare a variable that stores radius value. We can also take input from the user using the ‘input()’ function.
- use linspace() which returns a row vector of n evenly spaced points between two points and we will store these values in the vector.
- x = r * cos(theta) , which generates x-coordinates.
- y = r * sin(theta) , which generates y-coordinates.
- plot (x,y) , plot all the points (x,y).
- you can see by joining these points our circle will be drawn.
Example 1:
Matlab
r=2;
theta=linspace(0,2*pi,200);
x=r*cos(theta);
y=r*sin(theta);
plot(x,y);
axis( 'equal' );
title( 'Circle of r radius' )
|
Output:
Explanation:
In the above, we are drawing the circle of radius 2, and after that, we are creating a vector theta and we are dividing 200 parts between 0 and 2*pi we store these parts into vectors after that generate x-coordinates and y-coordinates and simply plot the points through joining these points a circle will be drawn.
Draw Circle by Taking User Input:
For taking input from the user we use the input() command. In the Below example, we will take input from the user and then we will draw a circle.
Example 2:
Matlab
r = input( 'Enter the radius of the circle: ' )
theta = linspace(0,2*pi,100);
x = r * cos(theta);
y = r * sin(theta);
plot(x,y);
axis( 'equal' );
title( 'Circle of the given radius r' )
|
Output:
Enter the radius of the circle: 4.3
Above we are drawing a circle of a given radius. We are creating a vector and divide 100 parts between 0 and 2*pi and we store these parts in the vector theta, after that generate x-coordinates and y-coordinates and simply plot the points. By joining these points a circle will be drawn.
Share your thoughts in the comments
Please Login to comment...