Problems with the use of streamline for a magnetic field

8 次查看(过去 30 天)
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 个评论
Sam Chak
Sam Chak 2024-6-7
Hi, please share the code for generating the quivers and the 3D streams, so that forum users can assist you.
Mano
Mano 2024-6-9
Hi, I just uploaded the question with the lines for ploting the streamlines !

请先登录,再进行评论。

回答(1 个)

Simar
Simar 2024-7-29
编辑:Simar 2024-8-1
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:
  1. 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.
  2. Check the Grid Generation: Make sure to generate the grid correctly. The “meshgrid” function should be used to create a 3D grid of points.
  3. Ensure Consistency in Dimensions: Ensure that the dimensions ofu,v, andwmatch the dimensions of x,y, and z.
  4. 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:
  1. Grid Points: Themeshgridfunction is used to generate a 3D grid of points. Adjust the ranges as per specific requirements.
  2. Field Components: Define the components of the magnetic field (u,v,w). Ensure these are consistent with the dimensions of x,y, and z.
  3. 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.
  4. 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-
Hope it helps!
Best Regards,
Simar

类别

Help CenterFile Exchange 中查找有关 Vector Fields 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by