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




% MATLAB code for draw circle
 % taking input from user
r = input('Enter the radius of the circle: ')
 
% Create a vectortheta.
theta = linspace(0,2*pi,100);
 
% Generate x-coordinates.
x = r * cos(theta);
 
% Generate y-coordinate.
y = r * sin(theta);
 
% plot the circle.
plot(x,y);
 
% Set equal scale on axes.
axis('equal');
 
% put a title
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.



How to Draw a Circle of Given Radius R in MATLAB?

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




% MATLAB code for draw circle
 
r=2;
% Create a vectortheta.
theta=linspace(0,2*pi,200);
 
% Generate x-coordinates.
x=r*cos(theta);
 
% Generate y-coordinate.
y=r*sin(theta);
 
% plot the circle.
plot(x,y);
 
 % Set equal scale on axes.
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.

Similar Reads

Draw Circle by Taking User Input:

...