Problems with the use of streamline for a magnetic field
6 views (last 30 days)
Show older comments
Hi,
I'm trying to plot the streamlines of a magnetic field generated by a magnet and a coil. I have my Field generated in 6 vectors, XYZ for the coordinates et UVW for the components. I managed to reorganize those data in 6 3D-arrays. I ploted the quiver and the plot seems satisfying. Now I wanted to plot the streamlines in order to observe the loopback. Unfortunately, the streamlines are not really good and does not follow the vectors ploted by quiver. Here is the figure :
when i use the following xyz 3D arrays and i have the error sample points must be unique and i don't understand why becuase all the couples are unique.
x=
val(:,:,1) =
-2.000000000000000e-01 -2.000000000000000e-01 -2.000000000000000e-01
-1.387778780781446e-17 -1.387778780781446e-17 -1.387778780781446e-17
2.000000000000000e-01 2.000000000000000e-01 2.000000000000000e-01
val(:,:,2) =
-2.000000000000000e-01 -2.000000000000000e-01 -2.000000000000000e-01
-1.387778780781446e-17 -1.387778780781446e-17 -1.387778780781446e-17
2.000000000000000e-01 2.000000000000000e-01 2.000000000000000e-01
val(:,:,3) =
-2.000000000000000e-01 -2.000000000000000e-01 -2.000000000000000e-01
-1.387778780781446e-17 -1.387778780781446e-17 -1.387778780781446e-17
2.000000000000000e-01 2.000000000000000e-01 2.000000000000000e-01
y=
val(:,:,1) =
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
val(:,:,2) =
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
val(:,:,3) =
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
-2.000000000000000e-01 -1.387778780781446e-17 2.000000000000000e-01
z=
val(:,:,1) =
-5.000000000000000e-02 -5.000000000000000e-02 -5.000000000000000e-02
-5.000000000000000e-02 -5.000000000000000e-02 -5.000000000000000e-02
-5.000000000000000e-02 -5.000000000000000e-02 -5.000000000000000e-02
val(:,:,2) =
1.500000000000000e-01 1.500000000000000e-01 1.500000000000000e-01
1.500000000000000e-01 1.500000000000000e-01 1.500000000000000e-01
1.500000000000000e-01 1.500000000000000e-01 1.500000000000000e-01
val(:,:,3) =
3.500000000000000e-01 3.500000000000000e-01 3.500000000000000e-01
3.500000000000000e-01 3.500000000000000e-01 3.500000000000000e-01
3.500000000000000e-01 3.500000000000000e-01 3.500000000000000e-01
%edit : here is the code used to generate the plot :
[startx,starty,startz]=meshgrid(xzm:0.02:xzp,yzm:0.02:yzp,app.ZSliderMagnet.Value+heig/2); % define the top surface of the cube
streamline(app.CoilCubeAxs,x, y, z, u, v, w, startx, starty, startz);
quiver3(app.CoilCubeAxs,x,y,z,u,v,w,0,Color='g',LineWidth=2)
2 Comments
Sam Chak
on 7 Jun 2024
Hi, please share the code for generating the quivers and the 3D streams, so that forum users can assist you.
Answers (1)
Simar
on 29 Jul 2024
Edited: Simar
on 1 Aug 2024
Hi Mano ,
I understand that you are trying to plot streamlines of a magnetic field but are encountering issues with the streamline function in MATLAB. The error "sample points must be unique" suggests that there may be duplicate points in the grid, or the grid might not be properly defined.
Here is a step-by-step approach to plot the streamlines:
- Verify the Uniqueness of Grid Points: Ensure that the grid points (x, y, z) are unique. It looks like the grid is defined properly, but the way “streamline” function is called might be causing issues.
- Check the Grid Generation: Make sure to generate the grid correctly. The “meshgrid” function should be used to create a 3D grid of points.
- Ensure Consistency in Dimensions: Ensure that the dimensions ofu,v, andwmatch the dimensions of x,y, and z.
- Use Proper Starting Points: Define appropriate starting points for the streamlines. The starting points should be within the bounds of the grid.
Here is a revised version of code with some added checks and corrections:
% Define the grid points
[x, y, z] = meshgrid(-0.2:0.1:0.2, -0.2:0.1:0.2, -0.05:0.2:0.35);
% Define the components of the magnetic field
u = ...; % Define u component
v = ...; % Define v component
w = ...; % Define w component
% Ensure the dimensions of u, v, w match x, y, z
assert(isequal(size(x), size(u)), 'Dimensions of x and u must match');
assert(isequal(size(y), size(v)), 'Dimensions of y and v must match');
assert(isequal(size(z), size(w)), 'Dimensions of z and w must match');
% Define the starting points for the streamlines
[startx, starty, startz] = meshgrid(-0.2:0.02:0.2, -0.2:0.02:0.2, 0.15);
% Plot the streamlines
figure;
streamline(x, y, z, u, v, w, startx, starty, startz);
% Plot the quiver plot
hold on;
quiver3(x, y, z, u, v, w, 'g', 'LineWidth', 2);
hold off;
% Set axis labels and title
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Streamlines and Quiver Plot of Magnetic Field');
Explanation:
- Grid Points: The“meshgrid”function is used to generate a 3D grid of points. Adjust the ranges as per specific requirements.
- Field Components: Define the components of the magnetic field (u,v,w). Ensure these are consistent with the dimensions of x,y, and z.
- Starting Points: The starting points for the streamlines are defined using “meshgrid”. Adjust the ranges to ensure they lie within the bounds of the grid.
- Plotting: The “streamline” function is used to plot the streamlines.The “quiver3” function is used to plot the vector field.
This should help to generate the streamlines correctly. If still encounter issues, double-check the definition of field components (u,v,w) and ensure they are correctly defined over the grid points.
Please refer to the following documentation links-
- streamline- https://www.mathworks.com/help/matlab/ref/streamline.html?searchHighlight=streamline&s_tid=srchtitle_support_results_1_streamline
- meshgrid- https://www.mathworks.com/help/matlab/ref/meshgrid.html?searchHighlight=meshgrid&s_tid=srchtitle_support_results_1_meshgrid
- quiver3- https://www.mathworks.com/help/matlab/ref/quiver3.html?searchHighlight=quiver3&s_tid=srchtitle_support_results_1_quiver3
Hope it helps!
Best Regards,
Simar
0 Comments
See Also
Categories
Find more on Vector Fields in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!