how to plot a function on a 3D sphere?
Show older comments
N0=100;
r_med=[0.445 0.889 1.445 2];
sigma_g=7;
N_ang=91;
theta = (0:1:360)/180*pi;
phi = (0:1:360)/180*pi;
[x,y]=meshgrid(theta,phi);
Num_r = 50e3;
r = linspace(1,50,Num_r)./2;
I0=1;Q0=0;U0=0;V0=0;
for i = 1:length(r_med)
% [P11(i,:),P12(i,:),P33(i,:),P34(i,:),Qsca_c(i,:),~,~] = ZK_W_Cloud_PhaseFunc(N0,r_med(i),sigma_g,N_ang);
P11_t(i,:) = [P11(i,:) fliplr(P11(i,2:end))];
P12_t(i,:) = [P12(i,:) fliplr(P11(i,2:end))];
P33_t(i,:) = [P33(i,:) fliplr(P11(i,2:end))];
P34_t(i,:) = [P34(i,:) fliplr(P11(i,2:end))];
P1=repmat(P11_t(i,:),length(phi),1);
P2=repmat(P12_t(i,:),length(phi),1);
P3=repmat(P33_t(i,:),length(phi),1);
P4=repmat(P34_t(i,:),length(phi),1);
z = P1.*I0+((P2.*Q0.*cos(2*y))+(P2.*U0.*sin(2*y)));
end
[v,u,w]=sph2cart(x,y,z);
v=z.*cos(y).*cos(x);
u=z.*sin(y).*cos(x);
w=z.*sin(y);
figure,
surf(v,u,w),xlabel('x'),ylabel('y'),zlabel('z')
%set(gca,'zscale','log');
I want to obtain the figure as shown

However, the output of my code is just a circle

Please correct me.
2 Comments
Walter Roberson
on 3 Jan 2022
How does this differ from https://www.mathworks.com/matlabcentral/answers/1620195-how-to-plot-a-3d-function?s_tid=srchtitle ?
Wiqas Ahmad
on 3 Jan 2022
Answers (1)
My recommendation would be to build these surfaces using the cylinder function, e.g.,
R=1; %radius of circular cross-section
d=1.5; %radius of torus
fn=@(z) 2*R*(z-0.5);
x=fn( linspace(0,1) );
r1=d-sqrt(R^2-x.^2); %inner half surface
r2=d+sqrt(R^2-x.^2); %outer half surface
[X1,Y1,Z1]=cylinder(r1,40);
[X2,Y2,Z2]=cylinder(r2,40);
h(1)=surf(X1, Y1, fn(Z1)); hold on
h(2)=surf(X2, Y2, fn(Z2)); hold off
rotate(h,[1,0,0],90)
axis equal
xlabel X; ylabel Y; zlabel Z;
view(35,30)
5 Comments
Wiqas Ahmad
on 3 Jan 2022
Matt J
on 3 Jan 2022
If your scenario is different, you will need to edit your question, so that people can understand how. The cylinder command works for both th surfaces you say you want ot plot.
Wiqas Ahmad
on 4 Jan 2022
Matt J
on 4 Jan 2022
Yes, but that doesn't explain why my posted solution is insufficient.
Wiqas Ahmad
on 4 Jan 2022
Categories
Find more on Surface and Mesh Plots 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!



