python实现迭代法求方程组的根
2024-08-31 14:13:32
有方程组如下:
迭代法求解x,python代码如下:
import numpy as np
import matplotlib.pyplot as plt A = np.array([[8, -3, 2], [4, 11, -1], [6, 3, 12]])
b = np.array([[20, 33, 36]]) # 方法一:消元法求解方程组的解
result = np.linalg.solve(A, b.T)
# print('Result:\n', result) # 方法二:迭代法求解方程组的解
B = np.array([[0, 3/8, -2/8], [-4/11, 0, 1/11], [-6/12, -3/12, 0]])
f = np.array([[20/8, 33/11, 36/12]])
error = 1.0e-6
steps = 100
xk = np.zeros((3, 1)) # initialize parameter setting
errorlist = []
for k in range(steps):
xk_1 = xk
xk = np.matmul(B, xk) + f.T
print('xk:\n', xk)
errorlist.append(np.linalg.norm(xk-xk_1))
if errorlist[-1] < error:
print('iteration: ', k+1)
break # 把误差画出来
x_axis = [i for i in range(len(errorlist))]
plt.figure()
plt.plot(x_axis, errorlist)
结果如下:
【参考文献】
《机器学习算法原理与编程实践》郑捷,第五章第一节
最新文章
- PhpStorm 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项
- 更新App版本的流程
- js中创建数组的方法
- 【代码笔记】iOS-截屏功能
- 用Okhttp框架登录之后的Cookie设置到webView中(转)
- iOS开发--二维码的生成
- docker管理shipyard中文版v3.0.2更新
- git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin
- openssl知识点总结
- OAF_VO系列3 - Binding Style绑定方式
- struts2,hibernate4,spring3配置时问题汇总及解决办法
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
- ☀【Grunt】no such file or directory, imagemin
- FoxOne---一个快速高效的BS框架--(2)
- c语言中break continue goto return和exit的区别 联系(筛选奇数和goto求和)
- 深信服模式(先做减法,必须拜访客户三次、研究需求方向,把产品的问题控制住,快速反应,在未来十年,绝大部分业务都会搬到Internet上来,实现All on Internet)good
- hdu 1728 搜索求最少的转向次数
- SQL注入攻击[详解]
- Mybateis mapper 接口 example 用法
- 《Java大学教程》--第1章 步入Java世界
热门文章
- 05—动态sql
- centos 解决 mysql command not found
- hlslcc
- CodeForces - 837E - Vasya&#39;s Function | Educational Codeforces Round 26
- 响应json数据之发送ajax的请求
- Clone failed: Could not read from remote repository
- Java数据库小项目01--实现用户登录注册
- 洛谷P2787 语文1(chin1)- 理理思维
- Python常用模块之hashlib模块
- Linux下MongoDB非正常关闭启动异常解决方法