贡献者: addis
我们可以用 Matlab 内建的 surf(X,Y,Z,Val)
函数在空间中画出任意曲面。其中 X,Y,Z,Val
是尺寸相同的 2 维矩阵,分别代表曲面上每个网格的各点。
% surf() in spherical coordinate
% usage 1. R,Th,Ph,Val are 2D matrices in same shape
% usage 2. R,Th,Ph are vector,vector,scalar
% usage 3. R,Th,Ph are vector,scalar,vector
function h = surfSph(R,Th,Ph,Val,varargin)
% resize
if isvector(R) && isvector(Th)
[R,Th] = ndgrid(R, Th);
end
if isvector(R) && isvector(Ph)
[R,Ph] = ndgrid(R,Ph);
end
if isscalar(Th)
Th = Th*ones(size(R));
end
if isscalar(Ph)
Ph = Ph*ones(size(R));
end
if ~isequal(size(R),size(Th)) || ...
~isequal(size(Th),size(Ph)) || ...
~isequal(size(Ph),size(Val))
error('wrong shape for Th, Ph, Val');
end
% plot
[X,Y,Z] = Sph2Cart(R, Th, Ph);
h = surf(X,Y,Z,Val,varargin{:});
view(0,0);
shading flat; axis equal;
xlabel x; ylabel y; zlabel z;
Rmax = max(R(:));
axis([-1, 1, -1, 1, -1, 1]*Rmax);
set(datacursormode(gcf), 'UpdateFcn', @datatip);
cameratoolbar;
end
Nr = 200; Nth = 150;
R = linspace(0, 10, Nr);
Th = linspace(0, 2*pi, Nth);
[R, Th] = ndgrid(R, Th);
Ph = 0;
Val = cos(R) .* cos(Th).^2;
figure; surfSph(R,Th,Ph,Val);
title('cos(r)cos^2(\theta)');
view(0, 0);
运行结果如图 1 。
Nr = 200; Nth = 150;
R = linspace(0, 10, Nr);
Ph = linspace(0, 2*pi, Nth);
[R, Ph] = ndgrid(R, Ph);
Val = cos(R) .* cos(Ph).^2;
figure; surfSph(R,pi/6,Ph,Val);
hold on; surfSph(R,pi/3,Ph,Val);
surfSph(R,pi/2,Ph,Val);
xlabel x; ylabel y; zlabel z;
title('cos(r)cos^2(\phi)');
view(30, 16);
运行结果如图 2 。