Javascript required
Skip to content Skip to sidebar Skip to footer

Drawing a Circle on Plot Matlab

Matlab Plot Circle

Introduction to Matlab Plot Circle

MATLAB tin be used to perform operations involving geometric figures like circles, rectangles, squares etc. In this article, nosotros will focus on circles. We will learn how to create various types of circles in MATLAB. We tin can create solid or plane circles in MATLAB, which nosotros will learn equally we go ahead in the article. We will also larn how to create a circumvolve using the rectangle function.

How to Create a circle using Rectangle Function?

Permit us first larn syntax to draw a simple circle in MATLAB:

1. Let us first declare some points, here we are taking 500 points. The below lawmaking will create these points.

  • angles = linspace(0, 2*pi, 500);

2. Let us now declare the radius and middle of the circle. The eye will be defined by ten and y co-ordinates.

  • radius = 20;
  • CenterX = fifty;
  • CenterY = 40;

3. Finally, nosotros volition plot our circle.

  • x = radius * cos(angles) + CenterX;
  • y = radius * sin(angles) + CenterY;

iv. We will also write some lawmaking for our output to look visually meliorate. This is normal formatting and we can adjust it as per our requirement.

  • plot(ten, y, 'b-', 'LineWidth', two);
  • hold on;
  • plot(CenterX, CenterY, 'k+', 'LineWidth', iii, 'MarkerSize', xiv);
  • filigree on;
  • axis equal;
  • xlabel('Ten', 'FontSize', 14);
  • ylabel('Y', 'FontSize', xiv);

5. This is how our input and output will expect similar in MATLAB console:

Code:

angles = linspace(0, 2*pi, 500);
radius = 20;
CenterX = 50;
CenterY = xl;
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
plot(10, y, 'b-', 'LineWidth', 2);
concur on;
plot(CenterX, CenterY, 'k+', 'LineWidth', 3, 'MarkerSize', xiv);
grid on;
centrality equal;
xlabel('X', 'FontSize', 14);
ylabel('Y', 'FontSize', xiv);

Output:

Matlab Plot Circle - 1

As we can run across in the higher up output, the circumvolve is created with a radius twenty and centre (l, forty) equally divers by us in the code.

How to Create a Solid 2d Circumvolve in MATLAB?

Next, permit u.s. larn how to create a solid 2D circle in MATLAB:

ane. Get-go, nosotros volition be creating logical image of circumvolve. For this, we will define heart, bore and the epitome size. Let us first create image.

  • imageSizeOfX = 640;
  • imageSizeOfY = 480;
  • [colInImage rowsInImage] = meshgrid(1 : imageSizeOfX, 1 : imageSizeOfY);

2. Adjacent, we will be creating the circle inside the paradigm.

  • centerOfX = 320;
  • centerOfY = 240;
  • radius = eighty;
  • Pixels = (rowsInImage – centerOfY).^two …
  • + (colInImage – centerOfX).^2 <= radius.^2;

3. In the above line of code, Pixels is "logical" array and is 2d. Permit usa now display 'Pixels'.

  • image(Pixels);
  • colormap([0 0 0; 1 1 1]);
  • title('Epitome of circle');

iv. This is how our input and output will wait like in MATLAB console:

Code:

imageSizeOfX = 640;
imageSizeOfY = 480;
[colInImage rowsInImage] = meshgrid(1 : imageSizeOfX, i : imageSizeOfY);
centerOfX = 320;
centerOfY = 240;
radius = lxxx;
Pixels = (rowsInImage - centerOfY).^2 ...
+ (colInImage - centerOfX).^2 <= radius.^two;
image(Pixels);
colormap([0 0 0; one 1 one]);
title('Image of circle');

Output:

Matlab Plot Circle - 2

How to create a Circumvolve in MATLAB Using Rectangle Function?

Allow us now learn how to create a circle in MATLAB using rectangle role: Here is a simple code to achieve this:

i. Like nosotros discussed in above examples, we will declare the radius and centre co-ordinates of the required circle.

  • radius = 6;
  • centerX = 30;
  • centerY = twoscore;
  • rectangle('Position',[centerX – radius, centerY – radius, radius*2, radius*two],…
  • 'Curvature',[ane,1],…
  • 'FaceColor','b');
  • axis square;

ii. We have passed 'FaceColor' every bit "b" so our output circle will be of Blue colour.

Code:

radius = 6;
centerX = 30;
centerY = 40;
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*ii],...
'Curvature',[1,ane],...
'FaceColor','b');
centrality square;

Output:

Rectangle Function

How we can Create a Unproblematic arc in MATLAB?

Finally, let us discuss how we tin create a simple arc in MATLAB. As we know that arc is naught but a pocket-sized portion of the circle, code for creating an arc is besides very like to that of creating a circle.

ane. Showtime we ascertain the parameters of required arc.

  • xCenter = ane;
  • yCenter = 1;
  • radius = 4;

2. Adjacent, nosotros define the bending theta every bit required.

  • theta = linspace(xx, 100, 50);
  • ten = radius * cosd(theta) + xCenter;
  • y = radius * sind(theta) + yCenter;

3. Finally, we plot our defined points.

  • plot(x, y, 'b-', 'LineWidth', two);
  • axis equal;
  • grid on;

Code:

xCenter = one;
yCenter = 1;
radius = 4;
theta = linspace(20, 100, 50);
ten = radius * cosd(theta) + xCenter;
y = radius * sind(theta) + yCenter;
plot(ten, y, 'b-', 'LineWidth', two);
axis equal;
filigree on;

Output:

Simple arc

Conclusion

So, in this article, we learnt how to create circles in MATLAB. Nosotros can create both plane circles and solid circles in MATLAB. We also learnt how nosotros can leverage the Rectangle function to plot circles in MATLAB. We tin as well format our circle equally per our requirement.

Recommended Manufactures

This is a guide to Matlab Plot Circle. Hither nosotros discuss an introduction, how to Create a circumvolve using rectangle function, a Solid 2nd Circle, a circle in MATLAB and Unproblematic arc. You can also become through our other related articles to larn more than –

  1. Break in MATLAB
  2. Nested Loop in Matlab
  3. Matlab pcolor() | Examples
  4. Consummate Guide to Optimset Matlab
  5. Plot Vector Matlab | Functions
  6. Matlab Figure | Examples
  7. xlabel Matlab | Examples

hagenauersorpathey1961.blogspot.com

Source: https://www.educba.com/matlab-plot-circle/