4

I'm trying to learn how to use matplotlib and numpy to plot systems of equations for one of my classes. We haven't been given much guidance, and I'm not too great with programming and I can't really understand how to do this, although i don't think its too complicated. Basically, I have a system of equations like this:

4x -2y + z =11
-2x +4y -2z = -16
x -2y + 4z = 17

How can I use matplotlib to plot the graphs for each of these equations?

Thanks!

Edit: The instructions also say to use mplot3d as well. EDIT: SOLUTION: How to draw planes from a set of linear equations in Python?

Community
  • 1
  • 1
Vandexel
  • 609
  • 2
  • 7
  • 17
  • It's not clear to me, what you mean 'by plotting the solution'. What you wrote down is a linear system of equations, which can have zero, one or infinitely many solutions. This has nothing to do with matplotlib per se, but is a linear algebra problem. – David Zwicker Jan 30 '16 at 21:37
  • Sorry, I mean to plot the equations, not the solution - I will edit. – Vandexel Jan 30 '16 at 21:43
  • If you want to do things graphically, then you have lines in three dimensions, so you want to use mplot3d to plot the three lines in three dimensional-space. Then you can see what the solution set looks like. Check out http://stackoverflow.com/questions/4622057/plotting-3d-polygons-in-python-matplotlib for some of the necessary framework. – MathBio Jan 30 '16 at 21:43
  • 2
    Well, actually each of the equations defines a plane in space, not a line. – Christoph Jan 30 '16 at 21:48
  • @DimiMak edit the question please. That helps future askers. Your answer is here: http://stackoverflow.com/questions/19410733/how-to-draw-planes-from-a-set-of-linear-equations-in-python – abalter Jan 30 '16 at 21:57
  • Your equations each define a plane. Where any two intersect you get a line. If all three intersect at a single point, that point is the unique solution. Otherwise, where any two intersect you get a line, meaning an infinite number of points. If somehow, none intersect (meaning the planes are all parallel), then there is no solution. Thanks @Christoph for setting me straight. – abalter Jan 30 '16 at 22:04

1 Answers1

6

You can eliminate z from the first two equations to give x=1 and the line of intersection of the first two planes z=7+2y, and then solve with the remaining equation to give the point (1,-2,3). You can verify this with numpy.linalg.solve:

In [11]: M = np.array([[4., -2., 1.], [-2., 4., -2.], [1., -2., 4.]])

In [12]: b = np.array([11., -16., 17.])

In [13]: np.linalg.solve(M, b)
Out[13]: array([ 1., -2.,  3.])

In Matplotlib, the planes can be plotted with plot_surface, the line of intersection of the first two (in blue) with plot and a marker used for the point at which the third plane (in green) intersects the line.

Make the planes a bit transparent with alpha=0.5 and because the planes re flat, there's no need for marker lines to tile the surfaces (you can set the row strides and column strides to something large):

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x, y = np.linspace(-8,8,100), np.linspace(-8,8,100)
X, Y = np.meshgrid(x,y)
Z1 = 11 - 4*X + 2*Y
Z2 = (16 - 2*X + 4*Y) / 2
Z3 = (17 - X + 2*Y) / 4

ax.plot_surface(X,Y,Z1, alpha=0.5, rstride=100, cstride=100)
ax.plot_surface(X,Y,Z2, alpha=0.5, rstride=100, cstride=100)


ax.plot((1,1),(-8,8),(-9,23), lw=2, c='b')
ax.plot_surface(X,Y,Z3, alpha=0.5, facecolors='g', rstride=100, cstride=100)
ax.plot((1,),(-2,),(3,), lw=2, c='k', marker='o')

plt.show()

enter image description here

xnx
  • 24,509
  • 11
  • 70
  • 109