I had managed to use MATLAB's spherical harmonics function and use matlab2tikz to convert it into a tikz image. Solution adapted from http://au.mathworks.com/help/matlab/examples/animating-a-surface.html?refresh=true
MATLAB code:
theta = 0:pi/40:pi; % polar angle
phi = 0:pi/20:2*pi; % azimuth angle
[phi,theta] = meshgrid(phi,theta); % define the grid
degree = 0;
order = 0;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,1)
surf(x,y,z, rho);
title('$\ell=0, m=0$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 1;
order = 0;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,6)
surf(x,y,z, rho);
title('$\ell=1, m=0$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 1;
order = 1;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,7)
surf(x,y,z, rho);
title('$\ell=1, m=\pm 1$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 2;
order = 0;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,11)
surf(x,y,z, rho);
title('$\ell=2, m=0$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 2;
order = 1;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,12)
surf(x,y,z, rho);
title('$\ell=2, m=\pm 1$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 2;
order = 2;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,13)
surf(x,y,z, rho);
title('$\ell=2, m=\pm 2$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 3;
order = 0;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,16)
surf(x,y,z, rho);
title('$\ell=3, m=0$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 3;
order = 1;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,17)
surf(x,y,z, rho);
title('$\ell=3, m=\pm 1$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 3;
order = 2;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,18)
surf(x,y,z, rho);
title('$\ell=3, m=\pm 2$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 3;
order = 3;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,19)
surf(x,y,z, rho);
title('$\ell=3, m=\pm 3$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 4;
order = 0;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,21)
surf(x,y,z, rho);
title('$\ell=4, m=0$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 4;
order = 1;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,22)
surf(x,y,z, rho);
title('$\ell=4, m=\pm 1$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 4;
order = 2;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,23)
surf(x,y,z, rho);
title('$\ell=4, m=\pm 2$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 4;
order = 3;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,24)
surf(x,y,z, rho);
title('$\ell=4, m=\pm 3$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
degree = 4;
order = 4;
amplitude = 0.5;
radius = 5;
Ymn = legendre(degree,cos(theta(:,1)));
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1)
yy = [yy Ymn];
end
yy = yy.*cos(order*phi);
order = max(max(abs(yy)));
rho = radius + amplitude*yy/order;
r = radius.*sin(theta); % convert to Cartesian coordinates
x = r.*cos(phi);
y = r.*sin(phi);
z = radius.*cos(theta);
subplot(5,5,25)
surf(x,y,z, rho);
title('$\ell=4, m=\pm 4$')
shading interp
axis equal off % set axis equal and remove axis
view(0,30) % set viewpoint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
map = makeColorMap([0.2 0.2 0.6],[1.0 0.99 0.72],[0.8 0.25 0.33],80);
colormap(map);
cd(Figures)
addpath(genpath([pwd '/../matlab2tikz']))
fig = figure(1);
matlab2tikz('SH1.tikz','height', '\figureheight', 'width', '\figurewidth','parseStrings',false, 'Floatformat', '%.4f');