0

I am trying to test streamline with a very simple 3D vector field. I fill in a mesh using 3 for loops (not the best, but this is reminescent of a different expression for "v" which I couldn't easily vectorise). Then I define the vector field v as r. Simple radial field. My full code is below. quiver3 is fine, but unfortunately streamline gives me the following error:

Error using griddedInterpolant Interpolation requires at least two sample points in each dimension.

N = 5;
L = 2;
dl = 2*L/N;
for i = 1:N+1
    for j = 1:N+1
        for k = 1:N+1
     
        x = -L + (i-1)*dl;
        y = -L + (j-1)*dl;
        z = -L + (k-1)*dl;
        
        X(i,j,k) = x;
        Y(i,j,k) = y;
        Z(i,j,k) = z;
        
        r = [x,y,z];
        
        v = r-r0;
        
        Vx(i,j,k) = v(1);
        Vy(i,j,k) = v(2);
        Vz(i,j,k) = v(3);
        
        end
    end
end

quiver3(X,Y,Z,Vx,Vy,Vz);
[sx,sy,sz] = meshgrid(0:0.1:1,0:1:5,0:1:5);
streamline(X,Y,Z,Vx,Vy,Vz,sx,sy,sz);
hold on;
streamslice(X,Y,Z,Vx,Vy,Vz,[],[],5);
pbaspect([1,1,1])
usumdelphini
  • 213
  • 1
  • 11
  • I suspect that the issue is in the line , `[sx,sy,sz] = meshgrid(1,0:1:5,0:1:5);` where you use effectively only 2D and not 3D (the scalar `1`) . If you use streamline(X,Y,Z,Vx,Vy,Vz) instead you wont get a bug. – bla Jan 15 '21 at 18:52

1 Answers1

1

It returns back to gridded X, Y variables, if you use transposed version of X and Y, you will not get this interpolation error in the streamline function. To transpose a N-D array in MATLAB, use permute function, like:

X = permute(X, [2,1,3]);  % to rearrange X and Y dimension

or just define correct form of X and Y at the first place [in the loop]

Mansour Torabi
  • 411
  • 2
  • 6
  • permuting X,Y,Z does not seem to fix things. How would I "define them correctly in the first place?". Thanks – usumdelphini Jan 15 '21 at 20:49
  • 1
    if you use the command: streamline(permute(X,[2 1 3]),permute(Y,[2 1 3]),Z,Vx,Vy,Vz,sx,sy,sz); you will not encounter the interpolation error. OR in the loop, use: X(j,i,k) = x; Y(j,i,k) = y; – Mansour Torabi Jan 15 '21 at 21:06