How to Declare a 3D Field in MATLAB
MATLAB is a software environment used frequently in technical data collection, modeling and analysis. A 3D vector field is a collection of vectors, each localized to a point in three-dimensional space. Vector fields, especially in three dimensions, are common in physics and engineering. Though MATLAB does not contain a specialized function for vector fields, it is simple to declare them using basic MATLAB functions.
Instructions
-
-
1
Declare a set of points in three dimensions at which the values for your vector field are to be represented using the "meshgrid()" function. The meshgrid() function replicates a series of vectors to create a matrix. For example, to create a grid from -10 to 10 in steps of 1 in three dimensions, type:
[X,Y,Z] = meshgrid(-10:10, -10:10, -10:10);
Each of the matrices X, Y and Z are of dimension 21 x 21 x 21, each corresponding to the values of the x, y and z dimensions at a particular index.
-
2
Assign value to the x, y and z magnitude of the vector located at each of the corresponding points. While this will commonly be based upon a formula that can be calculated using the results of meshgrid() or from measured observations, you can use random values for this example.
vec_x = rand(size(X));
vec_y = rand(size(Y));
vec_z = rand(size(Z)); -
-
3
Convert the existing separate matrices to a cell array for ease of programmatic access:
my_temp_array = zeros([size(X) 6]);
my_temp_array(:,:,:,1) = X;
my_temp_array(:,:,:,2) = Y;
my_temp_array(:,:,:,3) = Z;
my_temp_array(:,:,:,4) = vec_x;
my_temp_array(:,:,:,5) = vec_y;
my_temp_array(:,:,:,6) = vec_z;
my_vector_field = num2cell(my_temp_array,4);
clear my_temp_array
-
1
References
- Photo Credit Comstock/Comstock/Getty Images