The drawing of grid graph and surface graph is very similar, and the calling format is almost the same. Before drawing, a lattice point matrix of word variables must be formed. The following summarizes several functions for creating grids in matlab for your reference~~~~
Like it first, then watch it, otherwise it's all rascals
table of Contents
- linspace: Generate linear spacing vector
- logspace: Generate logarithmic spacing vector
- freqspace: frequency spacing of frequency response
- meshgrid: 2D and 3D grid
- ndgrid: rectangular grid in N-dimensional space
- Resource portal
- 「❤️ Thank you everyone」
linspace: Generate linear spacing vector
grammar
y = linspace(x1,x2)
y = linspace(x1,x2,n)
Description
y = linspace(x1,x2)
Returns a row vector containing 100 equally spaced points between x1 and x2.
y = linspace(x1,x2,n)
Generate n points. The distance between these points is (x2-x1)/(n-1).
linspace
Similar冒号运算符“:”
, but you can directly control the number of points and always include the endpoints.The "lin" in the "linspace" name indicates to generate linear spacing values instead of the sibling function logspace, which generates logarithmic spacing values.
Example
Equally spaced digital vector
Create a vector of 100 equidistant points in the interval [-5,5].
y = linspace(-5,5);
A vector containing the specified number of values
Create a vector of 7 equidistant points in the interval [-5,5].
>> y1 = linspace(-5,5,7)
>
y1 = 1×7
-5.0000 -3.3333 -1.6667 0 1.6667 3.3333 5.0000
A vector of evenly distributed complex numbers
Create a complex vector containing 8 equidistant points between 1+2i and 10+10i.
>> y = linspace(1+2i,10+10i,8)
>
y = 1×8 complex
1.0000 + 2.0000i 2.2857 + 3.1429i 3.5714 + 4.2857i 4.8571 + 5.4286i 6.1429 + 6.5714i 7.4286 + 7.7143i 8.7143 + 8.8571i 10.0000 +10.0000i
Input parameters
x1, x2-point interval
Point interval, specified as a numeric scalar pair . x1 and x2 define the interval of points generated by linspace. x1 and x2 can be real or complex numbers, and x2 can be greater or less than x1. If x2 is smaller than x1, the vector contains decreasing values.
Data type: single | double | datetime | duration
Complex number support: yes
n-the number of points
100 (default) | Real scalar
The number of points, specified as a real scalar.
If n is 1, linspace returns x2.
If n is zero or negative, linspace returns a 1×0 empty matrix.
If n is not an integer, linspace rounds down and returns floor(n) points.
logspace: Generate logarithmic spacing vector
grammar
y = logspace(a,b)
y = logspace(a,b,n)
y = logspace(a,pi)
y = logspace(a,pi,n)
Description
y = logspace(a,b)
Generate a row vector y consisting of 50 logarithmically spaced points between 10 a and 10 b (10 to the power of N). The logspace function is particularly useful for creating frequency vectors. This function is the logarithmic equivalent function of linspace and the ":" operator.
y = logspace(a,b,n)
Generate n points between 10 a and 10 b (10 to the Nth power).
y = logspace(a,pi)
Generate 50 points between 10^a and pi, which is useful for digital signal processing that creates a logarithmic pitch frequency in the interval [10^a,pi].
y = logspace(a,pi,n)
Generate n points between 10^a and pi.
Example
Vector of logarithmically spaced values
Create a vector consisting of 50 logarithmically spaced points in the interval [10 1 ,10 5 ].
y = logspace(1,5);
A vector containing the specified number of values
Create a vector consisting of 7 logarithmically spaced points in the interval [10 1 ,10 5 ].
>> y1 = logspace(1,5,7)
>
y1 = 1×7
105 ×
0.0001 0.0005 0.0022 0.0100 0.0464 0.2154 1.0000
Vector of logarithmically spaced complex numbers
Create a complex vector consisting of 8 logarithmically spaced points between 10 (1+2i) and 10 (5+5i) .
>> y = logspace(1+2i,5+5i,8)
y = 1×8 complex
104 ×
-0.0001 - 0.0010i 0.0029 - 0.0024i 0.0133 + 0.0040i 0.0147 + 0.0497i -0.1242 + 0.1479i -0.7150 - 0.0822i -1.2137 - 2.3924i 4.9458 - 8.6913i
Input parameters
a-first boundary
The first boundary, specified as a numeric scalar . The a parameter defines a boundary of the interval where the point generated by logspace is located. a can be a real or complex number, and the other boundary b can be greater or less than a. If b is smaller than a, the vector contains decreasing values.
Data type: single | double
complex number support: yes
b-second boundary
The second boundary, specified as a numeric scalar . The b parameter defines a boundary of the interval where the points generated by logspace are located. b can be a real or complex number, and b can be greater or less than another boundary a. If b is smaller than a, the vector contains decreasing values.
Data type: single | double
complex number support: yes
n-the number of points
50 (default) | Real scalar
The number of points, specified as a real scalar .
If n is 1, logspace returns 10 b .
If n is zero or negative, logspace returns an empty row vector.
If n is not an integer, logspace rounds n down and returns floor(n) points.
freqspace: frequency spacing of frequency response
grammar
[f1,f2] = freqspace(n)
[f1,f2] = freqspace([m n])
[x1,y1] = freqspace(...,'meshgrid')
f = freqspace(N)
f = freqspace(N,'whole')
Description
freqspace
Returns the implied frequency range of the equidistant frequency response. Freqspace is especially useful when creating the desired frequency response for various 1D and 2D applications.
[f1,f2] = freqspace(n)
Returns the two-dimensional frequency vectors f1 and f2 for an n×n matrix.
对奇数 n,f1 和 f2 为 [-n+1:2:n-1]/n。
对偶数 n,f1 和 f2 为 [-n:2:n-2]/n。
[f1,f2] = freqspace([m n])
Return the two-dimensional frequency vectors f1 and f2 for the m×n matrix.
[x1,y1] = freqspace(...,'meshgrid')
Equivalent to
[f1,f2] = freqspace(...);
[x1,y1] = meshgrid(f1,f2);
f = freqspace(N)
Return a one-dimensional frequency vector f and assume that there are N equidistant points around the unit circle. For odd or even N, f is (0:2/N:1). For even N, freqspace will return (N+2)/2 points. For an odd number N, it will return (N+1)/2 points.
f = freqspace(N,'whole')
Returns N equidistant points around the entire unit circle. In this example, f is 0:2/N:2*(N-1)/N.
meshgrid: 2D and 3D grid
grammar
[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)
[X,Y,Z] = meshgrid(x)
Description
[X,Y] = meshgrid(x,y)
Returns the two-dimensional grid coordinates based on the coordinates contained in the vectors x and y. X is a matrix, and each row is a copy of x; Y is also a matrix, and each column is a copy of y. The grid represented by coordinates X and Y has length(y) rows and length(x) columns.
[X,Y] = meshgrid(x) 与 [X,Y] = meshgrid(x,x) 相同
, And return the square grid coordinates with the grid size length(x)×length(x).
[X,Y,Z] = meshgrid(x,y,z)
Returns the three-dimensional grid coordinates defined by the vectors x, y, and z. The size of the grid represented by X, Y, and Z is length(y)×length(x)×length(z).
[X,Y,Z] = meshgrid(x) 与 [X,Y,Z] = meshgrid(x,x,x) 相同
, And return the three-dimensional grid coordinates with a grid size of length(x)×length(x)×length(x).
Example
Two-dimensional grid
Create a two-dimensional grid coordinate using the x coordinate defined by the vector x and the y coordinate defined by the vector y.
>> x = 1:3;
>> y = 1:5;
>> [X,Y] = meshgrid(x,y)
>
X = 5×3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Y = 5×3
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
Calculate the expression x 2 +y 2 on a two-dimensional grid .
>> X.^2 + Y.^2
>
ans = 5×3
2 5 10
5 8 13
10 13 18
17 20 25
26 29 34
Draw a surface map
Use uniformly distributed x and y coordinates to create a two-dimensional grid in the interval [-2,2].
x = -2:0.25:2;
y = x;
[X,Y] = meshgrid(x);
Calculate and plot the function f(x,y)=xe^ (−x 2 −y 2 ) on a two-dimensional grid .
F = X.*exp(-X.^2-Y.^2);
surf(X,Y,F)
Starting from R2016b, it is not always necessary to create a grid before operating it. For example, the calculation expression xe^ (−x 2 −y 2 ) will implicitly expand the vectors x and y.
surf(x,y,x.*exp(-x.^2-(y').^2))
Three-dimensional grid
Create a three-dimensional grid coordinate using the defined x, y, and z coordinates in the interval [0,6], and calculate the expression x 2 +y 2 +z 2 .
x = 0:2:6;
y = 0:1:6;
z = 0:3:6;
[X,Y,Z] = meshgrid(x,y,z);
F = X.^2 + Y.^2 + Z.^2;
Determine the size of the grid. The three coordinate vectors have different lengths, forming a rectangular box of grid points.
>> gridsize = size(F)
>
gridsize = 1×3
7 4 3
Using single-input syntax, a uniformly distributed three-dimensional grid is generated based on the coordinates defined in x. The new grid forms a grid point cube.
>> [X,Y,Z] = meshgrid(x);
>> G = X.^2 + Y.^2 + Z.^2;
>> gridsize = size(G)
>
gridsize = 1×3
4 4 4
Input parameters
x-the x coordinate of the point
The x coordinate of the point, specified as a vector.
type of data: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
y-the y coordinate of the point
The y coordinate of the point, specified as a vector.
type of data: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
z-the z coordinate of the point
The z coordinate of the point, specified as a vector.
type of data: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Output parameters
X-the x coordinate on the grid
Two-dimensional or three-dimensional array
The x coordinate on the grid, returned as a two-dimensional array (two inputs) or a three-dimensional array (three inputs).
Y-the y coordinate on the grid
Two-dimensional or three-dimensional array
The y coordinate on the grid, returned as a two-dimensional array (two inputs) or a three-dimensional array (three inputs).
Z-the z coordinate on the grid
Three-dimensional array
The z coordinate on the grid, returned as a three-dimensional array.
ndgrid: rectangular grid in N-dimensional space
grammar
[X1,X2,...,Xn] = ndgrid(x1,x2,...,xn)
[X1,X2,...,Xn] = ndgrid(xg)
Description
[X1,X2,...,Xn] = ndgrid(x1,x2,...,xn)
Copy the grid vector x1,x2,...,xn to generate an n-dimensional full grid.
[X1,X2,...,Xn] = ndgrid(xg)
Specifies to use a single grid vector xg for all dimensions. The number of output parameters you specify determines the dimension n of the output.
Example
Create a two-dimensional grid
Create a two-dimensional grid based on the vectors [1 3 5 7 9 11 13 15 17 19] and [2 4 6 8 10 12].
>> [X,Y] = ndgrid(1:2:19,2:2:12)
>
X = 10×6
1 1 1 1 1 1
3 3 3 3 3 3
5 5 5 5 5 5
7 7 7 7 7 7
9 9 9 9 9 9
11 11 11 11 11 11
13 13 13 13 13 13
15 15 15 15 15 15
17 17 17 17 17 17
19 19 19 19 19 19
Y = 10×6
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
2 4 6 8 10 12
Calculate functions on the grid domain
Calculate and plot functions
In the grid domain
−2<x1<2 和 −2<x2<2。
Create a grid of domain values.
[X1,X2] = ndgrid(-2:.2:2);
Calculate the function on the domain.
Z = X1 .* exp(-X1.^2 - X2.^2);
Generate a grid graph of the function.
mesh(X1,X2,Z)
In R2016b and later, this task does not require ndgrid. You can use implicit extensions for these commands to build a grid:
x = -2:.2:2;
Z1 = x.' .* exp(-(x.').^2 - x.^2);
Insert data
Create a two-dimensional grid and calculate some function values on the grid. Interpolate between the specified values to refine the mesh.
Create a (x,y) coarse grid in the range [−5,5].
[X,Y] = ndgrid(-5:0.5:5);
Calculate some function values on the grid and draw function graphs.
f = sin(X.^2) * cos(Y.^2);
surf(X,Y,f)
Use a finer grid to interpolate between points and plot the result.
[X1,Y1] = ndgrid(-5:0.125:5);
F = interpn(X,Y,f,X1,Y1,'spline');
surf(X1,Y1,F)
Input parameters
x1,x2,…,xn-grid vector (specified with separate parameters)
Grid vector , specified as a vector containing grid coordinates for each dimension. The grid vector implicitly defines the grid. For example, in a two-dimensional space:
data type : single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
complex number support : Yes
xg-grid vector of all dimensions
Grid vectors of all dimensions , specified as a vector containing grid coordinates. ndgrid uses xg as the grid vector for each dimension.
Data type : single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
complex number support : yes
Output parameters
X1,X2,…,Xn-complete grid representation
Complete grid representation, returned as a separate array. For each output array Xi, the i-th dimension contains a copy of the grid vector xi.
Resource portal
- Pay attention to [ Be a tender program ape ] public account
- Reply to the [ python information ] [ 2020 Autumn Recruitment ] in the background of the [ Be a tender program ape ] public account to get the corresponding surprise!
「❤️ Thank you everyone」
- Like to support it, so that more people can see this content (If you don’t like it, it’s all hooligans-_-)
- Welcome to share your thoughts with me in the message area, and you are also welcome to record your thought process in the message area.