How to plot streamlines on a sphere?

12 views (last 30 days)
Bob P
Bob P on 9 Jan 2013
Answered: David Taylor on 31 Aug 2018
I would like to plot streamlines wrapped around the surface of a sphere. Here is what I mean:
A few thoughts:
--Matlab makes it easy to plot scalar functions on spheres using surf (as shown here: http://www.mathworks.com/help/matlab/ref/surf.html), but I do not see how to extend this example to vector fields.
--I know how to plot streamlines on a surface provided the domain of the surface is rectangular (following this example: http://www.mathworks.com/help/matlab/ref/streamslice.html). Unfortunately, I don't see how to extend this to a spherical surface.
Thanks!

Answers (2)

Bob P
Bob P on 9 Jan 2013
Nevermind, I've figured out what I need. Something like this seems to work:
npts=100;
x = linspace(-1,1,npts);
y = linspace(-1,1,npts);
[X,Y]=meshgrid(x,y);
Z=X;
for r = 1:npts
for c = 1:npts
if (X(r,c)^2+Y(r,c)^2)>1
Z(r,c) = NaN;
else
Z(r,c)=sqrt(1-X(r,c)^2-Y(r,c)^2);
end
end
end
surf(X,Y,Z);shading interp
alpha(1)
[u v] = gradient(Z);
h = streamslice(X,Y,-u,-v);
set(h,'color','k')
for i=1:length(h);
zi = interp2(X,Y,Z,get(h(i),'xdata'),get(h(i),'ydata'));
set(h(i),'zdata',zi);
end
axis tight
%hold on;
%surf(X,Y,-Z);shading interp
%hold off;
daspect([1,1,1])
axis tight;

David Taylor
David Taylor on 31 Aug 2018
Hero! Thanks for sharing.

Community Treasure Hunt

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

Start Hunting!