如何在Python中从一组线性方程式绘制平面? [英] How to draw planes from a set of linear equations in Python?

查看:387
本文介绍了如何在Python中从一组线性方程式绘制平面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个方程的线性系统:

I have a linear system with three equations:

x 1 -2x 2 + x 3 = 0
2x 2 -8x 3 = 8
-4x 1 + 5x 2 + 9x 3 = -9

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

解集为(29,16,3),这是这些平面的交点.

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

希望有人可以使用Matplotlib在3D空间中绘制这些平面,以便清楚地可视化问题.

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

推荐答案

您的第三个等式说:

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

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

或者一般来说,您的方程式是

or in general an equation of yours is

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

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

正常是(a,b,c)

  • 如果a不为0,则平面上的点为(-d/a,0,0)
  • 如果b不为0,则平面上的点为(0,-d/b,0)
  • 如果c不为0,则平面上的点为(0,0,-d/c)

您可以将其插入到绘制库中,该库中包含法向矢量和平面上的一个点,并执行3次(每个平面一个).

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()

这篇关于如何在Python中从一组线性方程式绘制平面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆