Search code examples
matlabplot3dmatlab-figurestreamline

`streamline` not plotting this vector field


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])

Solution

  • 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]