5

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?

6
  • 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. Commented Jan 30, 2016 at 21:37
  • Sorry, I mean to plot the equations, not the solution - I will edit. Commented Jan 30, 2016 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 stackoverflow.com/questions/4622057/… for some of the necessary framework. Commented Jan 30, 2016 at 21:43
  • 2
    Well, actually each of the equations defines a plane in space, not a line. Commented Jan 30, 2016 at 21:48
  • @DimiMak edit the question please. That helps future askers. Your answer is here: stackoverflow.com/questions/19410733/… Commented Jan 30, 2016 at 21:57

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.