Spherical Patch in 3D

17 views (last 30 days)
hamzah khan
hamzah khan on 29 Mar 2022
Commented: hamzah khan on 29 Mar 2022
Hello,
I am trying to make a 3d spherical segment. I am having problems with the atan2 function. in 2D my code is
clear all; close all;
p = linspace(-1/2,1/2,200);
[X,Y] = meshgrid(p,p); % box mesh
R = p(size(p,2))/2;
r = R/1.5;
alpha = deg2rad(45);
theta = atan2(Y,X);
f = figure('visible','on');
active = (X.^2 + Y.^2 <= R^2) & (X.^2 + Y.^2 >= r^2);
plot(X(active),Y(active),'o','MarkerFaceColor','blue');
active = (X.^2 + Y.^2 <= R^2) & (X.^2 + Y.^2 >= r^2) & (abs(theta)< alpha);
hold on
plot(X(active),Y(active),'o','MarkerFaceColor','red');
here every thing is as I want(the red patch) ,but when I try to make the same patch in 3D, I am getting getting the wrong shape.
here is my code:
clear all; close all;
p = linspace(-1/2,1/2,100);
[X,Y,Z] = meshgrid(p,p,p); % box mesh
R = p(size(p,2))/2;
r = R/1.5;
alpha = deg2rad(45);
f = figure('visible','on');
theta = atan2(Z,X);
active = (X.^2 + Y.^2 + Z.^2 <= R^2) & (X.^2 + Y.^2 +Z.^2 >= r^2);
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','blue');
active = (X.^2 + Y.^2 + Z.^2 <= R^2) & (X.^2 + Y.^2 +Z.^2 >= r^2) & (abs(theta) <= alpha);
hold on
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','red');
saveas(f,'3d_patch','fig')
Can anyone tell me what is wrong? I figured it out to be theta...
Does any one knows how to take correct angle

Accepted Answer

hamzah khan
hamzah khan on 29 Mar 2022
I found out the answer
clear all; close all;
p = linspace(-1/2,1/2,100);
[X,Y,Z] = meshgrid(p,p,p); % box mesh
R = p(size(p,2))/2;
r = R/1.5;
alpha = deg2rad(45);
f = figure('visible','on');
theta = atan2(sqrt(X.^2+Y.^2),Z);
active = (X.^2 + Y.^2 + Z.^2 <= R^2) & (X.^2 + Y.^2 +Z.^2 >= r^2);
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','blue');
active = (X.^2 + Y.^2 + Z.^2 <= R^2) & (X.^2 + Y.^2 +Z.^2 >= r^2) & (abs(theta) <= alpha);
hold on
plot3(X(active),Y(active),Z(active),'o','MarkerFaceColor','red');
saveas(f,'3d_patch','fig')

More Answers (1)

KSSV
KSSV on 29 Mar 2022
The shape is spherical and looks fine. You need to set the axis. Try
axis equal
at the end.
  1 Comment
hamzah khan
hamzah khan on 29 Mar 2022
i found out the answer
theta = atan2(sqrt(X.^2+Y.^2),Z);

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!