Draw a Circle on 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, we will focus on circles. We will larn how to create various types of circles in MATLAB. Nosotros can create solid or plane circles in MATLAB, which we will larn as we go alee in the article. We volition also learn how to create a circumvolve using the rectangle role.

How to Create a circle using Rectangle Function?

Let united states first learn syntax to draw a simple circumvolve in MATLAB:

one. Permit usa first declare some points, hither we are taking 500 points. The beneath code will create these points.

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

ii. Let the states at present declare the radius and centre of the circle. The middle will exist defined past 10 and y co-ordinates.

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

3. Finally, we will plot our circle.

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

4. Nosotros will also write some lawmaking for our output to look visually better. This is normal formatting and we tin can adjust it every bit per our requirement.

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

5. This is how our input and output will await like in MATLAB panel:

Code:

angles = linspace(0, 2*pi, 500);
radius = 20;
CenterX = 50;
CenterY = xl;
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
plot(x, y, 'b-', 'LineWidth', 2);
hold on;
plot(CenterX, CenterY, 'm+', 'LineWidth', 3, 'MarkerSize', 14);
grid on;
axis equal;
xlabel('Ten', 'FontSize', fourteen);
ylabel('Y', 'FontSize', fourteen);

Output:

Matlab Plot Circle - 1

Every bit nosotros can run across in the above output, the circumvolve is created with a radius 20 and middle (l, 40) every bit defined by us in the code.

How to Create a Solid 2nd Circumvolve in MATLAB?

Next, let us learn how to create a solid 2d circle in MATLAB:

1. Get-go, we will exist creating logical image of circle. For this, we will define center, diameter and the image size. Let us outset create image.

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

ii. Side by side, we will be creating the circle inside the image.

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

iii. In the above line of code, Pixels is "logical" array and is second. Let usa at present display 'Pixels'.

  • image(Pixels);
  • colormap([0 0 0; one 1 1]);
  • championship('Image of circle');

4. This is how our input and output will await like in MATLAB console:

Code:

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

Output:

Matlab Plot Circle - 2

How to create a Circle in MATLAB Using Rectangle Function?

Let us now learn how to create a circumvolve in MATLAB using rectangle office: Hither is a simple code to accomplish this:

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

  • radius = half dozen;
  • centerX = 30;
  • centerY = xl;
  • rectangle('Position',[centerX – radius, centerY – radius, radius*2, radius*ii],…
  • 'Curvature',[1,1],…
  • 'FaceColor','b');
  • axis square;

two. Nosotros have passed 'FaceColor' as "b" then our output circle will be of Blueish colour.

Code:

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

Output:

Rectangle Function

How we tin can Create a Simple arc in MATLAB?

Finally, let us hash out how we can create a simple arc in MATLAB. Every bit we know that arc is nothing simply a pocket-sized portion of the circle, code for creating an arc is also very similar to that of creating a circle.

1. First we define the parameters of required arc.

  • xCenter = 1;
  • yCenter = 1;
  • radius = iv;

ii. Side by side, we ascertain the angle theta as required.

  • theta = linspace(xx, 100, l);
  • x = 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;

Code:

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

Output:

Simple arc

Conclusion

So, in this article, we learnt how to create circles in MATLAB. We can create both plane circles and solid circles in MATLAB. We also learnt how we tin leverage the Rectangle function to plot circles in MATLAB. We can likewise format our circle every bit 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. You can also go through our other related articles to larn more –

  1. Interruption 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

rodrigueburs1967.blogspot.com

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

0 Response to "Draw a Circle on Matlab"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel