plotting a spherical segment
27 views (last 30 days)
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
0 Comments
Accepted Answer
KSSV
on 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 Comments
More Answers (1)
isku
on 24 Jul 2020
Edited: isku
on 24 Jul 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])
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
0 Comments
See Also
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!