How to plot a function on a 3D sphere?
3 views (last 30 days)
Show older comments
I want to plot the following function on a sphere to obtain these figures:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1055005/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1055010/image.png)
I don't know where is the problem. Please if anybody can solve it. The dimesions of input parameters are:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1055015/image.png)
2 Comments
Abderrahim. B
on 5 Jul 2022
Edited: Abderrahim. B
on 5 Jul 2022
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 .
Answers (1)
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
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.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!