How to plot a function on a 3D sphere?

14 次查看(过去 30 天)
I want to plot the following function on a sphere to obtain these figures:
My coded program is:
clear all;
close all;
clc;
%% ------------------------------Program-------------------------------------
N0=100;
r_med=0.445;
sigma_g=7;
N_ang=91;
th=linspace(0,pi,181);
ph=linspace(0,2*pi,181);
for i=1:length(r_med)
[P11(i,:),P12(i,:),P33(i,:),P34(i,:),~,~,~] = ZK_W_Cloud_PhaseFunc(N0,r_med(i),sigma_g,N_ang)
end
figure
[theta,phi]=meshgrid(th,ph)
I=1;
Q=1;
U=0;
p=P11(1,:).*I+P12(1,:).*(Q.*cos(2*phi)+U.*sin(2*phi));% phase function
x=p.*(sin(phi)).*cos(theta);
y=p.*(sin(phi)).*sin(theta);
z=p.*(cos(phi));
surf(x,y,z)
xlabel('x');
ylabel('y');
zlabel('z');
The output is:
I don't know where is the problem. Please if anybody can solve it. The dimesions of input parameters are:
  2 个评论
Abderrahim. B
Abderrahim. B 2022-7-5
编辑:Abderrahim. B 2022-7-5
You want us to help and you re providng us with the function ZK_W_Cloud_PhaseFunc. Strange!
Are you sure this length(r_med) is not a typo ! Your for loop will only have one itration and maybe that is why you re not getting the spheres .
Wiqas Ahmad
Wiqas Ahmad 2022-7-5
You are right. It's my blunder but let me explain further to make it clear. Actually r_med has nothing to do with my figure. I just imported this data from another program just to show to the readers. There, the r_med has four values and that's why the loop runs. To plot my figure, I just need one value of r_med. Anyway, I plot it directly as you mentioned but got the same results
figure
theta0=linspace(0,pi,181);
phi0=linspace(0,2*pi,361);
I=1;Q=0;U=0;
[theta,phi]=meshgrid(theta0,phi0);
phf = P11.*I+((P12.*Q.*cos(2*phi))+(P12.*U.*sin(2*phi)));
x=phf.*(sin(phi)).*cos(theta);
y=phf.*(sin(phi)).*sin(theta);
z=phf.*(cos(phi));
surf(x,y,z)
xlabel('x');
ylabel('y');
zlabel('z');

请先登录,再进行评论。

回答(1 个)

William Rose
William Rose 2022-9-4
编辑:William Rose 2022-9-4
[edit-correct typo]
I cannot run the simplified code in your comment. That code seems to rely on P11 and P12 computed previously. I do not have ZK_W_Cloud_PhaseFunc, which computes P11and P12.
I recommend you check your theta and phi. Your spherical-to-Cartesian conversion assumes phi is angle measured from the +Z axis, and that theta is the angle measured from +X axis, in the X-Y plane, to the projection of the point onto the X-Y plane. That is standard, but there is anotheer convention which could have beeen used to make the figures you want to reproduce. See the "Conveentions" section here: https://en.wikipedia.org/wiki/Spherical_coordinate_system
If your assumption about the meaning of theta and phi is correct, then your phi0 and theta0 should be
theta0=linspace(0,2*pi,361);
phi0=linspace(0,pi,181);
Note the difference from yours.
Good luck!
  1 个评论
William Rose
William Rose 2022-9-4
In case it was not obvious from my answer, you could implement an alternative convention by leaving theta0 and phi0 as they appear in your code in your comment, but change the role of phi and theta in the conversion to X,Y,Z. In other words;
theta0=linspace(0,pi,181);
phi0=linspace(0,2*pi,361);
% other code here...
x=phf.*(sin(theta)).*cos(phi);
y=phf.*(sin(theta)).*sin(phi);
z=phf.*(cos(theta));
Maybe that will help. I hope so.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by