MATLAB: Either quiver3 or streamline work for me, but not both, for the same test data

MATLABquiver3streamline

I want to do a quiver3 vector plot and streamline. I am doing a test with simple data as defined in the attached file testx.m. If I define my grid with meshgrid, quiver3 gives an incorrect vector field, but streamline works expectedly on the incorrect field. If I define my grid as X(i,j,k)=x(i), etc, quiver3 works correctly but streamline returns the error below. Would appreciate some help. This may have something to do with the way grids are defined but, as it stands, it seems internally inconsistent between quiver3 and streamline.
>> h=streamline(X,Y,Z,Bx,By,Bz,0.8,0,2) ??? Error using ==> interp1 at 261 The values of X should be distinct.
Error in ==> stream3 at 67 sxi=interp1(xx(:),1:szu(2),sx(k));
Error in ==> streamline at 69 verts = stream3(x,y,z,u,v,w,sx,sy,sz,options);

Best Answer

Does this look like what you're expecting?
Edited code:
qpl=4;
qpd=4;
ky=2*pi/qpl;
ep=0.3;
nx=31;
ny=31;
nz=9;
dx=1/(nx-5);
dy=qpl/(ny-5);
dz=qpd/(nz-5);
% Taken out of the for loop, indices have been shifted accordingly
x=dx*(-2:nx-3);
y=dy*(-2:ny-3);
z=dz*(-2:nz-3);
[X,Y,Z]=meshgrid(x,y,z);
% Taken out of the for loop, Bx calculated using the meshgrid version of Y
Bx=ep*sin(ky*Y);
By=ones(nx,ny,nz);
Bz=zeros(nx,ny,nz);
figure
quiver3(X,Y,Z,Bx,By,Bz)
streamline(X,Y,Z,Bx,By,Bz,0.8,0,2)
Related Question