Skip to content Skip to sidebar Skip to footer

Draw a 2d Circle in Matlab

Matlab Plot Circle

Introduction to Matlab Plot Circle

MATLAB can exist used to perform operations involving geometric figures like circles, rectangles, squares etc. In this commodity, we will focus on circles. Nosotros will larn how to create various types of circles in MATLAB. We can create solid or plane circles in MATLAB, which we will learn as we go alee in the article. We volition also learn how to create a circle using the rectangle function.

How to Create a circle using Rectangle Function?

Let u.s.a. first learn syntax to draw a simple circumvolve in MATLAB:

1. Allow us commencement declare some points, hither we are taking 500 points. The below code will create these points.

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

2. Permit us now declare the radius and eye of the circle. The centre volition exist divers past x and y co-ordinates.

  • radius = twenty;
  • CenterX = fifty;
  • CenterY = twoscore;

3. Finally, we will plot our circle.

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

four. Nosotros will also write some code for our output to look visually better. This is normal formatting and we tin can adjust it equally per our requirement.

  • plot(x, y, 'b-', 'LineWidth', two);
  • concur on;
  • plot(CenterX, CenterY, 'k+', 'LineWidth', 3, 'MarkerSize', 14);
  • grid on;
  • axis equal;
  • xlabel('X', 'FontSize', 14);
  • ylabel('Y', 'FontSize', 14);

five. This is how our input and output will look like in MATLAB console:

Lawmaking:

angles = linspace(0, 2*pi, 500);
radius = 20;
CenterX = l;
CenterY = twoscore;
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
plot(x, y, 'b-', 'LineWidth', 2);
concord on;
plot(CenterX, CenterY, 'k+', 'LineWidth', 3, 'MarkerSize', fourteen);
grid on;
axis equal;
xlabel('10', 'FontSize', fourteen);
ylabel('Y', 'FontSize', 14);

Output:

Matlab Plot Circle - 1

As nosotros tin can run across in the above output, the circumvolve is created with a radius 20 and centre (l, 40) as defined past us in the code.

How to Create a Solid 2d Circle in MATLAB?

Next, let us larn how to create a solid 2D circle in MATLAB:

1. First, we will exist creating logical image of circle. For this, we will define center, diameter and the image size. Let us first create image.

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

two. Next, we will be creating the circle inside the image.

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

three. In the higher up line of lawmaking, Pixels is "logical" assortment and is 2D. Permit us now display 'Pixels'.

  • image(Pixels);
  • colormap([0 0 0; i i ane]);
  • championship('Image of circumvolve');

4. This is how our input and output volition look like in MATLAB console:

Lawmaking:

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

Output:

Matlab Plot Circle - 2

How to create a Circle in MATLAB Using Rectangle Office?

Let u.s.a. now learn how to create a circle in MATLAB using rectangle function: Here is a simple code to achieve this:

1. Like we discussed in to a higher place examples, nosotros will declare the radius and centre co-ordinates of the required circumvolve.

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

two. We take passed 'FaceColor' as "b" so our output circle will exist of Blue colour.

Code:

radius = 6;
centerX = thirty;
centerY = xl;
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],...
'Curvature',[1,i],...
'FaceColor','b');
axis square;

Output:

Rectangle Function

How we can Create a Uncomplicated arc in MATLAB?

Finally, let united states discuss how we can create a simple arc in MATLAB. As nosotros know that arc is nothing merely a minor portion of the circle, lawmaking for creating an arc is as well very like to that of creating a circumvolve.

1. First nosotros define the parameters of required arc.

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

2. Next, we define the angle theta equally required.

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

3. Finally, we plot our defined points.

  • plot(10, y, 'b-', 'LineWidth', 2);
  • axis equal;
  • grid on;

Lawmaking:

xCenter = 1;
yCenter = 1;
radius = four;
theta = linspace(twenty, 100, 50);
ten = radius * cosd(theta) + xCenter;
y = radius * sind(theta) + yCenter;
plot(x, y, 'b-', 'LineWidth', 2);
axis equal;
grid on;

Output:

Simple arc

Conclusion

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

Recommended Manufactures

This is a guide to Matlab Plot Circle. Here we discuss an introduction, how to Create a circle using rectangle function, a Solid 2D Circumvolve, a circle in MATLAB and Simple arc. Y'all can also go through our other related articles to learn more –

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

pletcheruppon1939.blogspot.com

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

Post a Comment for "Draw a 2d Circle in Matlab"