gpt4 book ai didi

python - 如何从 Python 中的一组线性方程式中绘制平面?

转载 作者:行者123 更新时间:2023-11-28 19:49:28 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

你的第三个等式说:

-4x + 5y + 9z - 9 = 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 次(每个平面一次)。

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

关于python - 如何从 Python 中的一组线性方程式中绘制平面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19410733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com