plotting a spherical segment
46 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I need to plot a spherical segment (blue region of the sphere in the attached picture).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158338/image.gif)
Do you have any suggestions?
Gilles
0 commentaires
Réponse acceptée
KSSV
le 24 Nov 2016
clc; clear all ;
[X,Y,Z] = sphere(200) ;
surf(X,Y,Z) ;
r = 1; b = 0.9 ; h = 1 ;
%%Get radius
R = sqrt(X.^2+Y.^2) ;
X1 = X ; Y1 = Y ; Z1 = Z ;
X1(Z<r/2) = NaN ; Y1(Z<r/2) = NaN ; Z1(Z<r/2) = NaN ;
X1(Z>b) = NaN ; Y1(Z>b) = NaN ; Z1(Z>b) = NaN ;
surf(X1,Y1,Z1) ;
axis equal
% shading interp ;
0 commentaires
Plus de réponses (1)
isku
le 24 Juil 2020
Modifié(e) : isku
le 24 Juil 2020
My code:
>> clear; syms t x y z real;
r = 1
a = .4*r % 40 pct. of r
b = .6*r % 60 pct. of r
eq = x^2+y^2+z^2 == r^2 % equation of a sphere with center at the origin
% define high by projecting on xz-plane by setting y=0. And then x=0 e,g. the high on z-axe
za = solve( subs( eq, [x^2, y^2,r^2], [0,0, (r^2-a^2)] ), z ) % by Pythagoras, see figure under
za = za(2) % only positive value, upper half of the sphere.
% Similarly
zb = solve( subs( eq, [x^2, y^2,r^2], [0,0, (r^2-b^2)] ), z ) % by Pythagoras
zb = zb(2) % only positive value
h = za - zb
% plotting
>> clear x;
x(t) = sin(t) *r ; % -1<x<1
[X,Y,Z] = sphere;
s = mesh(X,Y,Z, 'edgecolor', 'k', 'FaceAlpha',0.3) ;
hold on
yApos = sqrt( r^2 -x^2-za^2)
yAneg = -yApos
zA(t) = 0*t + za;
fplot3( x, yApos, zA, [-pi, pi], 'red', 'LineWidth', 2 ),
fplot3( x, yAneg, zA, [-pi, pi], 'red', 'LineWidth', 2 )
yBpos = sqrt( r^2 -x^2-zb^2)
yBneg = -yBpos
zB(t) = 0*t+ zb
fplot3( x, yBpos, zB, [-pi, pi], 'red', 'LineWidth', 2 )
fplot3( x, yBneg, zB, [-pi, pi], 'red', 'LineWidth', 2 )
xlabel('x'), ylabel('y'), zlabel('z'), axis equal , grid on
axis( [-1,1, -1,1, -1,1] *r )
campos( [5,5,5])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/336625/image.gif)
Using Pythagoras to define the hight at given a and b.
The high on z-axe at b:
zb^2 = r^2 - b^2 % b= 0.6*r
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/336646/image.png)
0 commentaires
Voir également
Catégories
En savoir plus sur Vector Fields dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!