标题基于Python实现Gauss-Seidel迭代法方程组计算(二)
一、首先根据习题的方程完成下面的代码
x11=round(-0.4x2-0.2x3-2.4,7)
x22=round(0.25x1-0.5x3+5,7)
x33=round(-0.2x1+0.3x2+0.3,7)
二、整个的代码如下:
x1=0
x2=0
x3=0
print('Gauss-Seidel')
for i in range(10):
x1 = round(-0.4 * x2 - 0.2 * x3 - 2.4, 7)
x2= round(0.25 * x1 - 0.5 * x3 + 5, 7)
x3= round(-0.2 * x1 + 0.3 * x2 + 0.3, 7)
print('|',i+1,'|',x1,'|',x2,'|',x3,'|')
三、运行结果如下:
D:\Users\anaconda3\envs\tools1\python.exe D:/pythonProject/Gauss-Seidel.py
Gauss-Seidel
| 1 | -2.4 | 4.4 | 2.1 |
| 2 | -4.58 | 2.805 | 2.0575 |
| 3 | -3.9335 | 2.987875 | 1.9830625 |
| 4 | -3.9917625 | 3.0105281 | 2.0015109 |
| 5 | -4.0045134 | 2.9981162 | 2.0003375 |
| 6 | -3.999314 | 3.0000028 | 1.9998636 |
| 7 | -3.9999738 | 3.0000748 | 2.0000172 |
| 8 | -4.0000334 | 2.999983 | 2.0000016 |
| 9 | -3.9999935 | 3.0000008 | 1.9999989 |
| 10 | -4.0000001 | 3.0000005 | 2.0000002 |
Process finished with exit code 0