使用Matlab画图时,有时三维的球坐标图更直观,需要把直角坐标转换球坐标。
代码如下:
看起来很简单的,下面写个小程序调用一下。
clearclose allclc%画一个平面,然后变换到球坐标系是一个球。theta = -90 : 1 : 90;phi = 0 : 1 : 360;z = ones(length(theta), length(phi));[theta_xx, phi_yy] = meshgrid(phi, theta);figure(1)surf(theta_xx, phi_yy, z)title('直角坐标系')shading flat %去掉网格的边沿颜色[theta_xx, phi_yy, z] = rectangular2spherical(theta_xx, phi_yy, z); %调用上面函数figure(2)surf(theta_xx, phi_yy, z)title('球坐标系')shading flataxis equal %坐标系对称结果如下: