Matlab --- how to use matlab to draw a three-dimensional vector in the three-dimensional coordinate system

Drawing 3D Vectors with MATLAB

        Two days ago, when I was writing an article on Gram-Schmidt orthogonalization, I thought it would be best to use matlab to combine the original vectors that have not been orthogonalized and the orthogonal basis that has been orthogonalized in three-dimensional coordinates In the system, it is represented by an image. In this way, you can see the "perpendicularity" between vectors and vectors more intuitively, instead of calculating the inner product between vectors to prove that they are "orthogonal".

        In fact, I have had the need to draw in a three-dimensional coordinate system many times before, but I have never found a very suitable one. For example, drawing a point in a 3D coordinate system, etc. This time, when I was drawing a three-dimensional vector, I accidentally found a function quiver3(), which can realize the problem of drawing one or more three-dimensional vectors in a three-dimensional coordinate system.

1. First, we use the quiver3 function to draw a vector v=[1, 2, 8]

First define the coordinate origin of the vector as [0,0,0]

X=[0];
Y=[0];
Z=[0];

define a three-dimensional vector

U=[1];
V=[2];
W=[8];

drawing

figure;
quiver3(X,Y,Z,U,V,W,0,'LineWidth',1)

Among them, the parameter "0" after "W" indicates the scaling of the length of the vector in the image (it can only be a positive integer, such as 2, which means that the length of the vector in the image is twice the length of the actual vector) , which is the Scale in the above syntax. In general, if you do not enter the scale parameter, for example, use quiver3(X,Y,Z,U,V,W) directly, matlab will automatically scale the length of the vector appropriately in order to beautify the image. Therefore, if we want to draw our input vector in the image "in a ratio of one to one", we should turn off the scale parameter. The way to turn it off is to set scale=0, that is, off. (default is auto)

"LineWidth" indicates the width of the line in the picture. Generally, it is not necessary to set it. I wrote 1 here to make it easier to see. The default value may be 0.5 or 0.7. Anyway, it is not as thick as 1.

Finally, the following image is obtained. In order to show the effect of scale=0, I also show the image with the default value of scale=auto.

Projection of the vector [1, 2, 8] on the xy plane

 Projection of the vector [1, 2, 8] on the xz plane

        As can be seen from the above comparison diagram, when scale=0, that is, when auto scale is off (the right side of the above diagram). The length of the vector in each direction of x, y, and z is consistent with our actual input, but if it is auto scale (the left side of the picture above), you can see that the length of the vector will be reduced by a certain percentage compared to the picture on the right.

This is the description and partial illustration of the "Scale" parameter in the official matlab documentation:

Finally enter:

axis equal

        He makes each coordinate axis use a unified standard scale to draw, that is to say, when matlab uses quiver3 to draw vector graphics, it also scales the coordinate axes to different degrees by default, so that, in this example In the vector [1, 2, 8], it seems in the figure that the lengths of the arrows in the three directions of x, y, and z are almost the same. The scales in the z-direction are 0, 2, 4, 6, 8 (red), the scales in the x-axis are 0, 0.2, 0.4, 0.6, 0.8, 1 (yellow), and the scales in the y-axis are 0, 0.5, 1, 1.5, 2 (blue).

 After inputting axis equal, because the vector uses the standard scale of the same size on each axis of x, y, and z, because the direction of the vector [1, 2, 8] will also be very accurate, as shown in the figure below.

2. We use quiver3 function to draw 2 vectors v1=[1,1,1], v2=[1,3,5]

        Similarly, first define the coordinate origin [0,0,0] of the two vectors respectively, where the first element in X, Y, and Z represents the starting coordinate of the first vector v1, and the second element is the first The starting coordinates of the two vector v2.

X=[0,0];
Y=[0,0];
Z=[0,0];

        Secondly, input two vectors v1, v2, similarly, the three values ​​in v1 are stored in the first element of U, V, W respectively, and the three values ​​in v2 are placed in the U, V, W in the second element.

U=[1,1];
V=[1,3];
W=[1,5];

drawing 

figure;
quiver3(X,Y,Z,U,V,W,0,'LineWidth',1)
legend('v1,v2','Location','northwest')

        Among them, the function legend() is used to add explanatory text to the image. If you want to distinguish two vectors in the graph and use different colors to represent them, you need to draw them one by one like the method of drawing a single vector before.

X=[0];
Y=[0];
Z=[0];
U=[1];
V=[1];
W=[1];

quiver3(X,Y,Z,U,V,W,0,'LineWidth',1)
axis equal
hold on

U=[1];
V=[3];
W=[5];
quiver3(X,Y,Z,U,V,W,0,'LineWidth',1)
axis equal
legend('v1','v2','Location','northwest')

The color of the two vectors can also be specified separately through the parameter color, 'r' is red and 'k' is black:

X=[0];
Y=[0];
Z=[0];
U=[1];
V=[1];
W=[1];

quiver3(X,Y,Z,U,V,W,0,'LineWidth',1,'Color','r')
axis equal
hold on

U=[1];
V=[3];
W=[5];
quiver3(X,Y,Z,U,V,W,0,'LineWidth',1,'Color','k')
axis equal
legend('v1','v2','Location','northwest')

If you want to use other custom colors, please refer to the following instructions:

3. Use quiver3 to draw multiple sets of vectors, and use different colors to represent them

%plot b
X=[0];
Y=[0];
Z=[0];
U=[1];
V=[3];
W=[5];
quiver3(X,Y,Z,U,V,W,0,'LineWidth',1)
axis equal

%plot x,y,z
X=[0,0,0];
Y=[0,0,0];
Z=[0,0,0];
U=[1,0,0];
V=[0,1,0];
W=[0,0,1];
quiver3(X,Y,Z,U,V,W,0,'LineWidth',1)
axis equal

%plot projection of b
X=[0];
Y=[0];
Z=[0];
U=[1];
V=[3];
W=[0];
quiver3(X,Y,Z,U,V,W,0,'LineWidth',1)
axis equal

legend('vector b','x,y,z','projection','Location','northwest')

As shown in the figure, I have drawn three sets of vectors in the same picture:

1, the vector b,

2, the unit vector on the xyz axis

3. The projection of vector b on the xy plane. 


(full text)

Author --- Panasonic J27

References (thanks):

1. Three-dimensional arrow diagram or vector diagram - MATLAB quiver3 - MathWorks India

(The accompanying picture has nothing to do with this article)

Copyright statement: Some pictures, texts or other materials in this article may come from many different websites and descriptions, so I cannot list them here. If there is any infringement, please let me know and delete it immediately. Everyone is welcome to reprint, but if someone quotes or copies my article, you must indicate in your article that the pictures or text you use come from my article, otherwise, the infringement will be investigated. ----Panasonic J27

Guess you like

Origin blog.csdn.net/daduzimama/article/details/130812421