plotting a spherical segment
Show older comments
Hi, I need to plot a spherical segment (blue region of the sphere in the attached picture).

Do you have any suggestions?
Gilles
Accepted Answer
More Answers (1)
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])

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

Categories
Find more on Line 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!