Draw a Moving Circle in Matlab

Matlab Plot Circle

Introduction to Matlab Plot Circle

MATLAB tin can be used to perform operations involving geometric figures similar circles, rectangles, squares etc. In this article, nosotros volition focus on circles. We will learn how to create various types of circles in MATLAB. We can create solid or plane circles in MATLAB, which we will larn every bit we go ahead in the article. Nosotros volition also acquire how to create a circle using the rectangle role.

How to Create a circle using Rectangle Part?

Let us showtime learn syntax to draw a unproblematic circle in MATLAB:

1. Allow united states showtime declare some points, here we are taking 500 points. The below code will create these points.

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

2. Allow us now declare the radius and centre of the circle. The centre volition be defined by x and y co-ordinates.

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

3. Finally, we will plot our circle.

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

4. We volition also write some code for our output to look visually meliorate. This is normal formatting and nosotros can adjust information technology as per our requirement.

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

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

Code:

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

Output:

Matlab Plot Circle - 1

Equally we can meet in the above output, the circumvolve is created with a radius 20 and middle (50, 40) as defined by us in the code.

How to Create a Solid second Circle in MATLAB?

Adjacent, let united states acquire how to create a solid 2D circumvolve in MATLAB:

one. Get-go, nosotros volition be creating logical image of circle. For this, we will define center, diameter and the image size. Let the states start create epitome.

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

2. Next, we will be creating the circumvolve inside the paradigm.

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

3. In the above line of code, Pixels is "logical" array and is 2D. Let us now display 'Pixels'.

  • prototype(Pixels);
  • colormap([0 0 0; ane 1 one]);
  • title('Epitome of circle');

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

Lawmaking:

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

Output:

Matlab Plot Circle - 2

How to create a Circle in MATLAB Using Rectangle Function?

Let usa at present learn how to create a circle in MATLAB using rectangle function: Hither is a uncomplicated code to reach this:

i. Like we discussed in above examples, we volition declare the radius and eye co-ordinates of the required circle.

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

2. We accept passed 'FaceColor' as "b" and so our output circumvolve will exist of Blue colour.

Lawmaking:

radius = half-dozen;
centerX = 30;
centerY = forty;
rectangle('Position',[centerX - radius, centerY - radius, radius*two, radius*2],...
'Curvature',[1,1],...
'FaceColor','b');
centrality square;

Output:

Rectangle Function

How we can Create a Simple arc in MATLAB?

Finally, let us talk over how we tin create a simple arc in MATLAB. Equally nosotros know that arc is cypher but a small portion of the circle, code for creating an arc is likewise very similar to that of creating a circle.

i. Beginning we define the parameters of required arc.

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

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

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

3. Finally, we plot our defined points.

  • plot(x, y, 'b-', 'LineWidth', ii);
  • axis equal;
  • filigree on;

Code:

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

Output:

Simple arc

Determination

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

Recommended Articles

This is a guide to Matlab Plot Circumvolve. Hither we discuss an introduction, how to Create a circumvolve using rectangle function, a Solid 2D Circumvolve, a circle in MATLAB and Simple arc. You can likewise 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

goodallorge1964.blogspot.com

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

0 Response to "Draw a Moving Circle in Matlab"

แสดงความคิดเห็น

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel