MATLAB: I want to show intersection of these two spheres. How should I do it

intersectionsphere

theta=linspace(0,2*pi,40);
phi=linspace(0,pi,40);
[theta,phi]=meshgrid(theta,phi);
r=1;
x=r*sin(phi).*cos(theta);
y=r*sin(phi).*sin(theta);
z=r*cos(phi);
mesh(x,y,z)
hold on
theta=linspace(0,2*pi,40);
phi=linspace(0,pi,40);
[theta,phi]=meshgrid(theta,phi);
r=1;
x=r*sin(phi).*cos(theta);
y=r*sin(phi).*sin(theta);
z=r*cos(phi);
x=x+0.25;
y=y-0.2;
z=z+0.1;
surf(x,y,z)

Best Answer

The spheres you describe have equations
1.) x^2 + y^2 + z^2 = 1
2.) (x-0.25)^2 + (y+1/5)^2 + (z-0.1)^2 = 1
Since equations 1 and 2 have same right-hand-side (equal to one), set the left-hand sides equal and you'll end up getting rid of the squared terms to be left with
3.) 5*x+4*y+2*z = 9/8
Equation 3 is the equation for the plane that contains the intersection of the two spheres. To see it add these lines of code to the end of your code above.
[X,Y] = meshgrid(linspace(-1,1,20));
Z = -5/2*X + 2*Y + 9/16;
surf(X,Y,Z)
That gives you the plane that contains the intersection. Realize that the intersection of the spheres is actually a curve that is a circle in this plane. Parametrizing that circle is more complicated.
Related Question