赞
踩
求方程组
{
5
x
1
+
2
x
2
+
x
3
=
−
12
−
x
1
+
4
x
2
+
2
x
3
=
20
2
x
1
+
−
3
x
2
+
10
x
3
=
2
\left\{ 5x1+2x2+x3=−12−x1+4x2+2x3=202x1+−3x2+10x3=2
上面方程当然是非常容易求解的,但是方程规模变大的时候(比如系数矩阵是100w* 100w的时候,这时候,用这个写就会比较简单了。。)
# @Time : 2018/5/22 17:23
# @Author : Sean
# @Site :
# @File : 1.py
# @Software: PyCharm
import numpy as np
A = np.array([[5.0, 2, 1], [-1, 4, 2], [2, -3, 10]])
B = np.array([-12.0, 20, 3])
x0 = np.array([0.0, 0, 0])
x = np.array([0.0, 0, 0])
times = 0
while True:
for i in range(3):
temp = 0
for j in range(3):
if i != j:
temp += x0[j] * A[i][j]
x[i] = (B[i] - temp) / A[i][i]
calTemp = max(abs(x - x0))
times += 1
if calTemp < 1e-4:
break
else:
x0 = x.copy()
print(times)
print(x)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。