迭代加速——逐次超松弛迭代法(Python实现)
2023-09-14 09:05:25 时间
目录
1、概述
逐次超松弛迭代法是在高斯赛德尔迭代法的基础上发展而来的,高斯赛德尔迭代法上一节我们已经讲过了,可以对比起来看。然后逐次超松弛迭代法中有一个很重要的思想:权重思想。我们下面直接上代码:
2、代码
import numpy as np
##超松弛迭代法
def SOR(n, A, B, x0, x, eps, k, w):
times = 0
while times < k:
for i in range(n):
temp = 0
temps = x0.copy()
for j in range(n):
if i != j:
temp += x0[j] * A[i][j]
x[i] = (1-w)*x[i]+w * ((B[i] - temp) / A[i][i])
x0[i] = x[i].copy()
calTemp = max(abs(x - temps))
times += 1
if calTemp < eps:
print("精确度等于{0}时,逐次超松弛迭代法需要迭代{1}次收敛".format(eps,times))
return (x, times)
else:
x0 = x.copy()
print("在最大迭代次数内不收敛", "最大迭代次数后的结果为", x)
return None
def main():
k = 100 # 最大迭代次数
n = 3
w=1.005
A = np.array([[8,-1,1], [2,10,-1], [1,1,-5]])
B = np.array([1,4,3])
x0 = np.array([1.0, 1, 1])
x = np.array([0.0, 0, 0])
eps = 10 ** (-3)
Sor=SOR(n, A, B, x0, x, eps, k, w)
print('come on...............................')
print("迭代值为:",Sor)
if __name__ == '__main__':
main()
3、结果
精确度等于0.001时,逐次超松弛迭代法需要迭代4次收敛
come on...............................
迭代值为: (array([ 0.22502629, 0.30559256, -0.49387565]), 4)
Process finished with exit code 0
相关文章
- python re.compile() 详解——Python正则表达式「建议收藏」
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- aic准则python_Python数据科学:线性回归
- python编程是啥-Python编程「建议收藏」
- 如何在 Python 中使用断点调试
- 【Python】python文件打开方式详解——a、a+、r+、w+、rb、rt区别[通俗易懂]
- python语言一般用于什么_PYthon
- 【说站】python itertools函数的多种用法
- python上的表白代码_用Python实现表白代码
- python制作自动交易程序_Python如何实现自动化交易
- python定义函数求和_Python定义函数实现累计求和操作
- Python 元组和集合
- 【错误记录】Mac 中 IntelliJ IDEA 运行 Python 程序报错 ( End of statement expected )
- 【Android 逆向】使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 反汇编二进制机器码 | 打印反汇编数据 )
- Python @函数装饰器及用法(超级详细)
- Linux下如何快速卸载Python环境(linux如何卸载python)
- 开发Linux下Python编程:实现自己的开发梦想(linux下python)
- MySQL与Python搭配,实现数据库操作。(mysql-python)
- Python __iter__和__reversed__:实现迭代器
- Linux查看Python版本的有效方法(linux查看python版本)
- Python操作Redis实现数据持久化(python操作redis)
- Python脚本实现Linux系统管理及自动化部署(python写linux)
- 使用Python连接MySQL数据库,实现高效数据交互(python连接mysql)
- 在Python中简单调用MySQL(python调用mysql)
- Python实现MySQL数据库的读取(python读取mysql)
- Linux下运行Python脚本的步骤简介(linux如何运行python脚本)
- Linux下Python编程:从入门到精通(linux下python编程)
- python编程-将Python程序转化为可执行程序[整理]
- python爬虫入门教程之点点美女图片爬虫代码分享