How to plot a function on a 3D sphere?

3 views (last 30 days)
Wiqas Ahmad
Wiqas Ahmad on 5 Jul 2022
Commented: William Rose on 4 Sep 2022
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 Comments
Wiqas Ahmad
Wiqas Ahmad on 5 Jul 2022
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');

Sign in to comment.

Answers (1)

William Rose
William Rose on 4 Sep 2022
Edited: William Rose on 4 Sep 2022
[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 Comment
William Rose
William Rose on 4 Sep 2022
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!