3

I have a linear system with three equations:

x1- 2x2 + x3 = 0
2x2 - 8x3 = 8
-4x1 + 5x2 + 9x3 = -9

The solution set is (29, 16, 3), which is a point at the intersection of these planes.

Hoping if anyone can plot these planes in a 3D-space using Matplotlib to visualize the problem clearly.

carlosdc
  • 12,022
  • 4
  • 45
  • 62

1 Answers1

14

Your third equation says:

-4x + 5y + 9z - 9 = 0

or in general an equation of yours is

a x + b y + c z + d = 0

The normal is (a, b, c)

  • If a is not 0 a point on the plane is (-d/a, 0, 0)
  • If b is not 0 a point on the plane is (0, -d/b, 0)
  • If c is not 0 a point on the plane is (0, 0, -d/c)

You plug this into the plotting library that takes a normal vector and a point on the plane, and do this 3 times (one for each plane).

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

point1  = np.array([0,0,0])
normal1 = np.array([1,-2,1])

point2  = np.array([0,-4,0])
normal2 = np.array([0,2,-8])

point3  = np.array([0,0,1])
normal3 = np.array([-4,5,9])

# a plane is a*x+b*y+c*z+d=0
# [a,b,c] is the normal. Thus, we have to calculate
# d and we're set
d1 = -np.sum(point1*normal1)# dot product
d2 = -np.sum(point2*normal2)# dot product
d3 = -np.sum(point3*normal3)# dot product

# create x,y
xx, yy = np.meshgrid(range(30), range(30))

# calculate corresponding z
z1 = (-normal1[0]*xx - normal1[1]*yy - d1)*1./normal1[2]
z2 = (-normal2[0]*xx - normal2[1]*yy - d2)*1./normal2[2]
z3 = (-normal3[0]*xx - normal3[1]*yy - d3)*1./normal3[2]

# plot the surface
plt3d = plt.figure().gca(projection='3d')
plt3d.plot_surface(xx,yy,z1, color='blue')
plt3d.plot_surface(xx,yy,z2, color='yellow')
plt3d.plot_surface(xx,yy,z3, color='cyan')
plt.show()

enter image description here

carlosdc
  • 12,022
  • 4
  • 45
  • 62
  • Thanks for your 'conversion' @carlosdc. But i figured out that display of these planes is not 'quietly' true, because when you rotate the view, there is always a plane that is never intersected or hidden by the other; may be it is an error in the matplotlib or the code isn't sufficient. Any way, i learned good stuff. – AbdulMomen عبدالمؤمن Oct 16 '13 at 19:19
  • 1
    @blueMix http://stackoverflow.com/questions/14824893/how-to-draw-diagrams-like-this/14825951#14825951 <- work around to _some_ of these issues – tacaswell Oct 16 '13 at 19:22
  • @tcaswell ok thanks, i added the alpha when displaying the planes, it kind of 'slightly' solved the problem, because honestly, i found the angles between the three planes and read your code, but don't know how to change it to three planes with different angels between them. – AbdulMomen عبدالمؤمن Oct 16 '13 at 20:05
  • The point is, matplotlib renders each artist in one shot and then layers them into the final image. It will not do the intersections for you. If you _really_ want to do this, you have to split the planes up by hand into 'above' and 'below' sections. – tacaswell Oct 16 '13 at 20:20