# MATLAB - Vector Function

## 1. meshgrid function

When using MATLAB to draw 3D graphics, some sample points are often needed, and then the graphics are drawn based on the sample points. The meshgrid function is a function used to generate grid sample points, as shown in the following examples:

```x=-3:1:3;
y=-2:1:2;
[X,Y]=meshgrid(x,y)
```

Run result: The result is shown in the diagram, where meshgrid (x,y) is used to generate two matrices of the same size with vector x as the row and vector y as the column, respectively.

## 2. quiver function

The quiver function is a function that uses arrows to draw two-dimensional vector fields in MATLAB. The basic format of drawing vector functions is: quiver(x,y,u,v,scale). The calling format is to draw small arrows at the position specified by (x,y) to represent vectors starting at that point (u,v). Drawing arrows everywhere gives two-dimensional vector fields, two-dimensional matrix x,y, U.V has a one-to-one correspondence. Scale indicates whether the arrow length is scaled, equal to 0.5 to half shorten; equal to 2 to double the length; equal to 1 to not scale. Examples are as follows

```x = [0 0 0 0];
y = x;
u = [1 -1 0 0];
v = [0 0 1 -1];
quiver(x, y, u, v);
```

The program draws four vectors starting from (0,0) and points to (1,0), (-1,0), (0,1), (0,-1). The result of running: You can see that the corresponding point is not exactly pointed to, so you need to add the scale parameter and set it to 1 and the result is as follows Another call format, quiver(u,v), draws vectors (u,v) on the xoy surface. Since there is no starting point for the specified vector, several grid points are taken evenly on the plane as starting points.

```u = [1 1 1; 1 1 1];
v = u;
quiver(u, v);
```

The results are as follows: ## 3. streamline function

The streamline function is a function that draws streamlines from two-dimensional or three-dimensional vector data in two basic formats:

###### (1),streamline(x,y,u,v,startx,starty)

Draws streamlines based on two-dimensional vector data u,v, and defines the position coordinates x and y of the vectors u and V. y must be monotonic, but not evenly spaced, and X and y have the same number of elements. startx and starty define the starting position of streamlines.

###### (2),streamline(x,y,z,u,v,w,startx,starty,startz)

This function draws streamlines based on three-dimensional data u, v, w, as shown below

```[x,y] = meshgrid(0:0.1:1,0:0.1:1);
u = x;
v = -y;
figure
quiver(x,y,u,v)                  %Draw arrow chart
startx = 0.1:0.1:1;
starty = ones(size(startx));         %Define starting coordinates
streamline(x,y,u,v,startx,starty)     %Streamline
```

Run result: ## 4. Explanation of vector function examples

Based on the related functions described above, these functions can be used to achieve the graphical drawing of vector functions.

##### (1) u = y ∗ c o s ( x ) ， v = y ∗ s i n ( x ) u=y*cos(x)，v=y*sin(x) u=y_cos(x), v=y_sin(x) graphics
```[x,y] = meshgrid(0:0.2:2,0:0.2:2);  	%Generate the required grid sample points, x and y In the range 0 to 2, every
%0.2 Take a point, and this will result in 100 sampling points in 0 to 2
u = cos(x).*y;  	                    %Definition u Weight
v = sin(x).*y;   	                    %Definition v Weight
quiver(x,y,u,v)                      	% Drawing 2-D Vector Field Graph
```

This code gives two components of the vector function directly, then draws them with arrows as follows: ##### (2) u = c o s ( x + 2 ∗ y ) ， v = s i n ( x − 2 ∗ y ) u=cos(x+2*y)，v=sin(x-2*y) u=cos(x+2_y), v=sin(x_2_y) graphics
```syms x y real	                        %Define Symbol Variables x,y
F = [ cos(x+2*y), sin(x-2*y) ];	        %Define Vector Functions
[X,Y]=meshgrid(-2:.25:2);	            %Generate Grid
Fxf = inline(vectorize(F(1)),'x','y');  %Using inline functions inline Constructor Fxf
Fyf = inline(vectorize(F(2)),'x','y');  %Using inline functions inline Constructor Fyf
Fx = Fxf(X,Y);                      	%Compute Grid Data Corresponding Fx Value of
Fy = Fyf(X,Y);                      	%Compute Grid Data Corresponding Fy Value of
quiver(X,Y,Fx,Fy,'k')               	%Draw a two-dimensional vector field
axis tight;	                            %Axis range is consistent with data range, compact mode
set(gca,'position',[0 0 1 1])	        %Remove white edges from images
```

This code uses a symbol variable to define a function and discretizes it to draw a vector graph. The result is as follows: ##### (3) z = y 2 − x 2 z=y^2-x^2 z=y2_x2 3-D graphics

The quiver3 function is similar to the quiver function in that it draws a three-dimensional vector field graph with the following code:

```[x,y]=meshgrid(-3:.5:3,-3:.5:3); 	%Generate the required grid sample points, x and y stay-3 To interval 3
z=y.^2-x.^2;	                    %Define Functions
[u,v,w]=surfnorm(z)  	            %Take the normal of a three-dimensional surface
quiver3(z,u,v,w)     	            %Drawing 3D Vector Field Graph
```

The results are as follows: Tags: MATLAB linear algebra

Posted on Fri, 24 Sep 2021 13:14:11 -0400 by Mistah Roth