有方程组如下:

迭代法求解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)

结果如下:

【参考文献】

《机器学习算法原理与编程实践》郑捷,第五章第一节

最新文章

  1. PhpStorm 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项
  2. 更新App版本的流程
  3. js中创建数组的方法
  4. 【代码笔记】iOS-截屏功能
  5. 用Okhttp框架登录之后的Cookie设置到webView中(转)
  6. iOS开发--二维码的生成
  7. docker管理shipyard中文版v3.0.2更新
  8. git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin
  9. openssl知识点总结
  10. OAF_VO系列3 - Binding Style绑定方式
  11. struts2,hibernate4,spring3配置时问题汇总及解决办法
  12. 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
  13. ☀【Grunt】no such file or directory, imagemin
  14. FoxOne---一个快速高效的BS框架--(2)
  15. c语言中break continue goto return和exit的区别 联系(筛选奇数和goto求和)
  16. 深信服模式(先做减法,必须拜访客户三次、研究需求方向,把产品的问题控制住,快速反应,在未来十年,绝大部分业务都会搬到Internet上来,实现All on Internet)good
  17. hdu 1728 搜索求最少的转向次数
  18. SQL注入攻击[详解]
  19. Mybateis mapper 接口 example 用法
  20. 《Java大学教程》--第1章 步入Java世界

热门文章

  1. 05—动态sql
  2. centos 解决 mysql command not found
  3. hlslcc
  4. CodeForces - 837E - Vasya&#39;s Function | Educational Codeforces Round 26
  5. 响应json数据之发送ajax的请求
  6. Clone failed: Could not read from remote repository
  7. Java数据库小项目01--实现用户登录注册
  8. 洛谷P2787 语文1(chin1)- 理理思维
  9. Python常用模块之hashlib模块
  10. Linux下MongoDB非正常关闭启动异常解决方法