How To Plot Trajectory In Matlab
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 will focus on circles. We will acquire how to create various types of circles in MATLAB. We can create solid or airplane circles in MATLAB, which nosotros will larn as we go alee in the article. We will also learn how to create a circle using the rectangle part.
How to Create a circumvolve using Rectangle Office?
Let u.s.a. first acquire syntax to describe a simple circumvolve in MATLAB:
1. Let usa first declare some points, here nosotros are taking 500 points. The beneath code volition create these points.
- angles = linspace(0, 2*pi, 500);
2. Let united states now declare the radius and eye of the circumvolve. The centre volition be defined past x and y co-ordinates.
- radius = 20;
- CenterX = 50;
- CenterY = xl;
3. Finally, nosotros will plot our circle.
- ten = radius * cos(angles) + CenterX;
- y = radius * sin(angles) + CenterY;
4. We will also write some lawmaking for our output to wait visually better. This is normal formatting and we can adjust it as per our requirement.
- plot(10, y, 'b-', 'LineWidth', two);
- hold on;
- plot(CenterX, CenterY, 'one thousand+', 'LineWidth', iii, 'MarkerSize', 14);
- grid on;
- centrality equal;
- xlabel('Ten', 'FontSize', fourteen);
- ylabel('Y', 'FontSize', 14);
five. This is how our input and output will expect like in MATLAB console:
Code:
angles = linspace(0, 2*pi, 500);
radius = xx;
CenterX = 50;
CenterY = 40;
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
plot(x, y, 'b-', 'LineWidth', 2);
agree on;
plot(CenterX, CenterY, 'k+', 'LineWidth', three, 'MarkerSize', 14);
grid on;
axis equal;
xlabel('X', 'FontSize', 14);
ylabel('Y', 'FontSize', fourteen);
Output:
As we can see in the higher up output, the circumvolve is created with a radius twenty and eye (50, 40) as defined by usa in the lawmaking.
How to Create a Solid 2nd Circumvolve in MATLAB?
Next, let united states learn how to create a solid 2D circle in MATLAB:
1. Starting time, we volition be creating logical epitome of circumvolve. For this, nosotros will define center, diameter and the image size. Permit the states kickoff create image.
- imageSizeOfX = 640;
- imageSizeOfY = 480;
- [colInImage rowsInImage] = meshgrid(one : imageSizeOfX, ane : imageSizeOfY);
two. Next, nosotros will be creating the circle within the paradigm.
- centerOfX = 320;
- centerOfY = 240;
- radius = 80;
- Pixels = (rowsInImage – centerOfY).^2 …
- + (colInImage – centerOfX).^2 <= radius.^2;
3. In the higher up line of code, Pixels is "logical" array and is 2D. Permit us at present display 'Pixels'.
- image(Pixels);
- colormap([0 0 0; 1 1 1]);
- championship('Epitome of circle');
4. This is how our input and output will look similar in MATLAB console:
Lawmaking:
imageSizeOfX = 640;
imageSizeOfY = 480;
[colInImage rowsInImage] = meshgrid(ane : imageSizeOfX, 1 : imageSizeOfY);
centerOfX = 320;
centerOfY = 240;
radius = 80;
Pixels = (rowsInImage - centerOfY).^2 ...
+ (colInImage - centerOfX).^2 <= radius.^2;
prototype(Pixels);
colormap([0 0 0; i i one]);
title('Prototype of circle');
Output:
How to create a Circle in MATLAB Using Rectangle Function?
Let us now learn how to create a circle in MATLAB using rectangle function: Here is a unproblematic lawmaking to accomplish this:
ane. Like nosotros discussed in above examples, nosotros will declare the radius and centre co-ordinates of the required circle.
- radius = 6;
- centerX = 30;
- centerY = xl;
- rectangle('Position',[centerX – radius, centerY – radius, radius*2, radius*2],…
- 'Curvature',[1,1],…
- 'FaceColor','b');
- axis foursquare;
2. We have passed 'FaceColor' as "b" so our output circle will be of Blue colour.
Code:
radius = 6;
centerX = 30;
centerY = 40;
rectangle('Position',[centerX - radius, centerY - radius, radius*two, radius*two],...
'Curvature',[1,1],...
'FaceColor','b');
centrality square;
Output:
How we can Create a Simple arc in MATLAB?
Finally, let us talk over how nosotros can create a simple arc in MATLAB. As we know that arc is nil but a small-scale portion of the circle, code for creating an arc is too very similar to that of creating a circumvolve.
one. First we define the parameters of required arc.
- xCenter = i;
- yCenter = 1;
- radius = four;
2. Next, we define the angle theta as required.
- theta = linspace(20, 100, fifty);
- x = radius * cosd(theta) + xCenter;
- y = radius * sind(theta) + yCenter;
3. Finally, we plot our defined points.
- plot(x, y, 'b-', 'LineWidth', 2);
- centrality equal;
- grid on;
Code:
xCenter = ane;
yCenter = one;
radius = 4;
theta = linspace(20, 100, 50);
10 = radius * cosd(theta) + xCenter;
y = radius * sind(theta) + yCenter;
plot(x, y, 'b-', 'LineWidth', ii);
axis equal;
grid on;
Output:
Conclusion
So, in this article, we learnt how to create circles in MATLAB. Nosotros tin create both plane circles and solid circles in MATLAB. We also learnt how nosotros can leverage the Rectangle role to plot circles in MATLAB. We tin also format our circle as per our requirement.
Recommended Articles
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 Uncomplicated arc. You tin can also go through our other related articles to learn more than –
- Interruption in MATLAB
- Nested Loop in Matlab
- Matlab pcolor() | Examples
- Complete Guide to Optimset Matlab
- Plot Vector Matlab | Functions
- Matlab Figure | Examples
- xlabel Matlab | Examples
Source: https://www.educba.com/matlab-plot-circle/
0 Response to "How To Plot Trajectory In Matlab"
Post a Comment