# Plot intersections of two relations

How can I plot an intersection of two relations? For instance, if I have two spheres $x^2 + (y-1)^2 + (z-1)^2 = 1$ and $x^2 + y^2 + z^2 = 1$, how can I plot their intersection on a graph?

edit retag close merge delete

Sort by » oldest newest most voted

Since you have two equations and three variables, first solve the system in terms of one variable:

var('x y z')
f=solve( [ x^2+(y-1)^2+(z-1)^2==1, x^2+y^2+z^2==1 ], y, z )


This gives solutions as a list:

[[y == -1/2*sqrt(-2*x^2 + 1) + 1/2, z == 1/2*sqrt(-2*x^2 + 1) + 1/2], [y == 1/2*sqrt(-2*x^2 + 1) + 1/2, z == -1/2*sqrt(-2*x^2 + 1) + 1/2]]


You can then either copy the solutions into a 3d parametric plot,

p = parametric_plot3d( [ x, -1/2*sqrt(-2*x^2 + 1) + 1/2, 1/2*sqrt(-2*x^2 + 1) + 1/2 ], (x,-1,1) )
p += parametric_plot3d( [ x, 1/2*sqrt(-2*x^2 + 1) + 1/2, -1/2*sqrt(-2*x^2 + 1) + 1/2 ], (x,-1,1) )


or substitute the solutions through the list indices:

p = parametric_plot3d( [ x, y.subs(f), z.subs(f) ], (x,-1,1) )
p += parametric_plot3d( [ x, y.subs(f), z.subs(f) ], (x,-1,1) )


and then show the combined plot. Here's an example including a unit sphere in cyan and the parametric solution in blue, with a slightly different output method.

more