不定点迭代法

方程的根

不动迭代法的概念

代码实现

import numpy
import numpy as np
from sympy import *
import math
import matplotlib.pyplot as plt
from sympy.simplify.fu import Ldef detfunction(x):return pow((x+1), 1/3)def erfen(point1, point2, min_area):min = point1max = point2isquit = Truewhile isquit:mid = point1+1/2*(point2-point1)if detfunction(mid) * detfunction(min) <= 0:max = midelif detfunction(mid)*detfunction(max) < 0:min = midif(math.fabs(max-min) < min_area):isquit = Falsereturn min, maxdef calu_result(accuary, point):minarea = 1# erfen()isquit = Truefirst = pointcount = 0       #次数while isquit:# 计算结果res = detfunction(first)if(abs(first-res) < accuary):first = resisquit = Falsefirst = rescount += 1return first, countdef draw(left, right):# x = np.arange(left-5,right+5,0.1)# x_v = x# y = []# for i in range(len(x)):#     res =detfunction(x[i])#     y.append(res)# plt.plot(x,x_v,label = "y=x")# plt.plot(x,y,label = "y = (x+1)^1/3")# # ax = gca()# plt.show()plt.figure(figsize=(12, 8), dpi=72)plt.subplot(1, 1, 1)X = np.linspace(-10,10, 2048, endpoint=True)X_val = XY_val = np.zeros(len(X))for i in range(len(X)):res = detfunction(X[i])Y_val[i] = resplt.plot(X, X_val, linewidth=1.5, linestyle="-", label="y =x")plt.plot(X, Y_val, linewidth=1.5, linestyle="-", label="y =(x+1)^1/3")plt.legend(loc='upper left')plt.xlim(-4.5, 4.5)# 设置纵轴的上下限plt.ylim(-4.5, 4.5)# 设置纵轴记号plt.yticks(np.linspace(-1, 1, 5, endpoint=True))ax = plt.gca()ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')ax.xaxis.set_ticks_position('bottom')ax.spines['bottom'].set_position(('data', 0))ax.yaxis.set_ticks_position('left')ax.spines['left'].set_position(('data', 0))# savefig("sincosin.png",dpi=72) #以72dpi保存图像plt.show()point = float(input("请输入迭代法的初始值"))
accuary = float(input("请输入最大允许误差值"))
res, count = calu_result(accuary, point)
print(f"结果:{res}")
print(f"迭代次数:{count}")
ma = max(res, point)
small = min(res, point)
draw(small*0.8, ma*1.2)

实验样例

函数的图像

样例2


牛顿法、牛顿简化法,牛顿下山法

牛顿法

迭代函数

牛顿简化法

由于牛顿法计算导数时比较困难,就用写于为f(x0)的导数的平行弦与x轴的交点来替代


牛顿下山法



代码实现

牛顿法

def calu_result(diff_function,function,accuary,point,x):is_quit = TrueInit_first = pointcount =0  #次数while is_quit:res1 = function.subs(x,Init_first)res2 = diff_function.subs(x,Init_first)temp = Init_first-(res1/res2)  #每次迭代的xkif abs(temp-Init_first)<accuary:Init_first = temp   #更新xkcount+=1return Init_first,countif(count>100):return temp,countInit_first = tempcount+=1

牛顿简化法

def calu_Simple_result(diff_function,function,accuary,point,x):is_quit = TrueInit_first = pointcount =0   #次数res2 = diff_function.subs(x,Init_first) #平行弦while is_quit:res1 = function.subs(x,Init_first)temp = Init_first-(res1/res2)if abs(temp-Init_first)<accuary:Init_first = tempcount+=1return Init_first,countif(count>100):return temp,countInit_first = tempcount+=1

牛顿下山法

def mountain_result(diff_function,function,accuary,point,x):is_quit = TrueInit_first = pointsecond =pointcount =0weight = 1is_mul = False#计算下山因子while is_quit!=False:res1 = function.subs(x,second)res2 = diff_function.subs(x,second)temp = second-(res1/res2)if abs(detfunction(temp))-abs(detfunction(second)) <0:break#每次减半,直到合适的下山因子weight  = weight/2second = temp# print(weight)while is_quit:#根据下山因子计算res1 = function.subs(x,Init_first)res2 = diff_function.subs(x,Init_first)temp = Init_first - weight*(res1/res2)if abs(temp-Init_first)<accuary:Init_first = tempreturn Init_first,count,weight#设定一个,100轮还没有结束,也退出,相当于一个最大迭代次数if(count>100):return temp,count,weightInit_first = tempcount+=1

三种方法实验结果

样例1

第一行为求导后的表达式,后面的为迭代次数和结果

当取初值为0.6时,应该可能出现,离根比较远的情况,此时牛顿法收敛法比较慢,而牛顿简化法已经不满足收敛条件了,而牛顿下山法可以保证它的收敛性,慢慢逼近根

全部代码

不动点迭代法

import numpy
import numpy as np
from sympy import *
import math
import matplotlib.pyplot as plt
from sympy.simplify.fu import Ldef detfunction(x):return 1/exp(x)def erfen(point1, point2, min_area):min = point1max = point2isquit = Truewhile isquit:mid = point1+1/2*(point2-point1)if detfunction(mid) * detfunction(min) <= 0:max = midelif detfunction(mid)*detfunction(max) < 0:min = midif(math.fabs(max-min) < min_area):isquit = Falsereturn min, maxdef calu_result(accuary, point):minarea = 1# erfen()isquit = Truefirst = pointcount = 0       #次数while isquit:# 计算结果res = detfunction(first)if(abs(first-res) < accuary):first = resisquit = Falsefirst = rescount += 1return first, countdef draw(left, right):# x = np.arange(left-5,right+5,0.1)# x_v = x# y = []# for i in range(len(x)):#     res =detfunction(x[i])#     y.append(res)# plt.plot(x,x_v,label = "y=x")# plt.plot(x,y,label = "y = (x+1)^1/3")# # ax = gca()# plt.show()plt.figure(figsize=(12, 8), dpi=72)plt.subplot(1, 1, 1)X = np.linspace(-10,10, 2048, endpoint=True)X_val = XY_val = np.zeros(len(X))for i in range(len(X)):res = detfunction(X[i])Y_val[i] = resplt.plot(X, X_val, linewidth=1.5, linestyle="-", label="y =x")plt.plot(X, Y_val, linewidth=1.5, linestyle="-", label="y =1/e^x")plt.legend(loc='upper left')plt.xlim(-4.5, 4.5)# 设置纵轴的上下限plt.ylim(-4.5, 4.5)# 设置纵轴记号plt.yticks(np.linspace(-1, 1, 5, endpoint=True))ax = plt.gca()ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')ax.xaxis.set_ticks_position('bottom')ax.spines['bottom'].set_position(('data', 0))ax.yaxis.set_ticks_position('left')ax.spines['left'].set_position(('data', 0))# savefig("sincosin.png",dpi=72) #以72dpi保存图像plt.show()point = float(input("请输入迭代法的初始值"))
accuary = float(input("请输入最大允许误差值"))
res, count = calu_result(accuary, point)
print(f"结果:{res}")
print(f"迭代次数:{count}")
ma = max(res, point)
small = min(res, point)
draw(small*0.8, ma*1.2)

牛顿迭代法

import numpy
import numpy as np
from sympy import *
import math
import matplotlib.pyplot as plt
from sympy.simplify.fu import Ldef draw(left, right):# x = np.arange(left-5,right+5,0.1)# x_v = x# y = []# for i in range(len(x)):#     res =detfunction(x[i])#     y.append(res)# plt.plot(x,x_v,label = "y=x")# plt.plot(x,y,label = "y = (x+1)^1/3")# # ax = gca()# plt.show()plt.figure(figsize=(12, 8), dpi=72)plt.subplot(1, 1, 1)X = np.linspace(-10,10, 2048, endpoint=True)X_val = XY_val = np.zeros(len(X))for i in range(len(X)):res = detfunction(X[i])Y_val[i] = resplt.plot(X, X_val, linewidth=1.5, linestyle="-", label="y =x")plt.plot(X, Y_val, linewidth=1.5, linestyle="-", label="y =(x+1)^3-x-1")plt.legend(loc='upper left')plt.xlim(-4.5, 4.5)# 设置纵轴的上下限plt.ylim(-4.5, 4.5)# 设置纵轴记号plt.yticks(np.linspace(-1, 1, 5, endpoint=True))ax = plt.gca()ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')ax.xaxis.set_ticks_position('bottom')ax.spines['bottom'].set_position(('data', 0))ax.yaxis.set_ticks_position('left')ax.spines['left'].set_position(('data', 0))# savefig("sincosin.png",dpi=72) #以72dpi保存图像# 在屏幕上显示plt.show()def getdiff(function,x):return diff(function,x)def detfunction(x):return pow(x,3)-x-1def calu_Simple_result(diff_function,function,accuary,point,x):print("牛顿简化法")is_quit = TrueInit_first = pointcount =0res2 = diff_function.subs(x,Init_first)while is_quit:res1 = function.subs(x,Init_first)temp = Init_first-(res1/res2)print(f"第{count}的值{temp}")if abs(temp-Init_first)<accuary:Init_first = tempcount+=1return Init_first,countif(count>100 or abs(temp-Init_first)>100000):return temp,countInit_first = tempcount+=1def calu_result(diff_function,function,accuary,point,x):print("牛顿法")is_quit = TrueInit_first = pointcount =0    while is_quit:res1 = function.subs(x,Init_first)res2 = diff_function.subs(x,Init_first)temp = Init_first-(res1/res2)print(f"第{count}的值:{temp}")if abs(temp-Init_first)<accuary:Init_first = tempcount+=1return Init_first,countif(count>100):return temp,countInit_first = tempcount+=1def mountain_result(diff_function,function,accuary,point,x):print("牛顿下山法")is_quit = TrueInit_first = pointsecond =pointcount =0weight = 1is_mul = Falsewhile is_quit!=False:res1 = function.subs(x,second)res2 = diff_function.subs(x,second)temp = second-(res1/res2)if abs(detfunction(temp))-abs(detfunction(second)) <0:breakweight  = weight/2second = temp# print(weight)while is_quit:res1 = function.subs(x,Init_first)res2 = diff_function.subs(x,Init_first)temp = Init_first - weight*(res1/res2)print(f"第{count}的值{temp}")if abs(temp-Init_first)<accuary:Init_first = tempreturn Init_first,count,weightif(count>100):return temp,count,weightInit_first = tempcount+=1x = symbols('x')
function = detfunction(x)
diff_function = getdiff(function,x)
print(diff_function)
point = float(input("请输入迭代法的初始值"))
accuary = float(input("请输入最大允许误差值"))
res, count = calu_result(diff_function,function,accuary, point,x)
res2,count2 = calu_Simple_result(diff_function,function,accuary, point,x)
res3,count3,weight = mountain_result(diff_function,function,accuary, point,x)
if count!=100:print(f"牛顿法结果:{res}")print(f"迭代次数:{count}")
# else:
#     print(f"迭代次数:{count}")
#     print(f"牛顿法结果:{res},达到最大迭代次数")if count2!=100:print(f"牛顿简化法结果:{res2}")print(f"迭代次数:{count2}")
# else:
#     print(f"牛顿简化法结果:{res2}")
#     print(f"迭代次数:{count2},达到最大迭代次数")if count3!=100:print("下山因子为:{}".format(weight))print(f"牛顿下山法结果:{res3}")print(f"迭代次数:{count3}")
ma = max(res, point)
small = min(res, point)
draw(small*0.8, ma*1.2)

总结

本文是学习的一些笔记,创作不易,点个爱心是缘分,不点是本分,如果有问题可以私信或者在下面评论,大家可以一起学习!!!

不动点迭代法和牛顿迭代法相关推荐

  1. 数值计算方法——不动点迭代和牛顿迭代法

    不动点迭代和牛顿迭代法 MATLAB基础 feval函数 format long syms x 简单迭代法[不动点迭代] Newton 迭代法 作业 MATLAB基础 feval函数 用于求函数值 基 ...

  2. 基于Python实现Aitken迭代法和牛顿迭代法

    目录 简单迭代法 简单迭代法的Aitken加速算法 基于Pyhton实现的Aitken加速算法 牛顿迭代法 基于Pyhton实现的牛顿迭代法 对于非线性方程,我们可以使用迭代的方式求出近似解.下面介绍 ...

  3. 非线性方程求解 :二分迭代法和牛顿迭代法

    在机器人算法开发中,经常会遇到求解非线性方程.非线性方程的求解十分困难,这里介绍两种方法: 1. 二分法 2.牛顿迭代法 定义: 非线性方程,就是因变量与自变量之间的关系不是线性的关系,这类方程很多, ...

  4. 方程组线性化方法和牛顿迭代法基础

    方程组线性化方法和牛顿迭代法基础 非线性方程组线性化和牛顿迭代法 参考书籍:GPS原理与接收机设计 谢钢 非线性方程,就是因变量与自变量之间的关系不是线性的关系,这类方程很多,例如平方关系.对数关系. ...

  5. 分别用二分法和牛顿迭代法求解方程x3 – 3x – 1 = 0在x = 2附近的实根

    编写程序,分别用二分法和牛顿迭代法求解方程x3 – 3x – 1 = 0在x = 2附近的实根,要求计算精确到小数点后七位数字为止,并将求出的近似结果与理论值2cos20 相比较,二分法的初始迭代区间 ...

  6. 数值分析-----不动点迭代和牛顿迭代(Python)

    目录 1 概述 1.1 迭代法 1.2 Newton迭代法 2 不动点迭代 2.1 基本思想 2.2 案例及实现                 ​ 3 牛顿迭代 3.1 基本思想 3.2 案例及实现 ...

  7. 二分法和简单迭代法的优缺点_二分法和牛顿迭代法求解方程的比较.doc

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp高等教育&nbsp>&nbsp理学 二分法和牛顿迭代法求解方程的比较.doc5页 本文档一共 ...

  8. 题目:任意给定一个浮点数,计算这个浮点数的立方根。(基于二分法和牛顿迭代法)(基于Java实现)

    题目:任意给定一个浮点数,计算这个浮点数的立方根.(基于二分法和牛顿迭代法)(基于Java实现) 首先,来分析一下这道题,其实在leetcode上做了求解根号3的题之后,对于这种求解立方根的题,基本上 ...

  9. 数值计算方法——Jacobi迭代法和G-S迭代法

    Jacobi迭代法和G-S迭代法 没完成的代码,仅做储存 矩阵常用操作 Jacobi迭代实现 G-S迭代实现 没完成的代码,仅做储存 function [L,D,U]=LU(A) n=length(A ...

最新文章

  1. VIM7.3添加中文帮助文档
  2. hdoj 2544 最短路
  3. 只知道java有反射可以说是动态语言,动态链接,早期晚期绑定、虚方法这些概念你知道吗
  4. 第八届蓝桥杯java b组第十题
  5. Vmware虚拟机使用Nat方式连接笔记本无线网卡
  6. 用gpg加密软件加密文件
  7. 地籍图 cad cass 二次开发 过滤器过滤扩展数据组码1000的解决方案
  8. 用flash做古诗动画_《古诗三首》Flash动画课件
  9. 我的姥爷走了,今后再也没有这个严厉而又较真儿的人
  10. 易语言编写倒计时小程序
  11. 关于Keil debug 出现cannot access target shutting down debug session 错误提示
  12. 微软 Visual Studio 2019 正式发布
  13. 谷粒商城简介(1~5集)
  14. 单片机炫彩灯实训报告_51单片机呼吸灯实验报告.doc
  15. 奔图P3022D黑白激光打印机 评测
  16. 【opencv】在图片上画角并且进行角度检测
  17. 什么是必选?和招标有什么区别?
  18. PIR人体感应AC系列感应器投光灯人体感应开关等应用定制方案
  19. PingCAP Clinic 服务:贯穿云上云下的 TiDB 集群诊断服务
  20. 进程的通信 - 命名管道

热门文章

  1. OVIS数据集代码解析
  2. SourceInsight使用技巧之更改背景色
  3. python实现智能语音翻译
  4. PHP语言Laravel9+Layui搭建的系统后台框架
  5. 【论文阅读】(2020)Knapsack polytopes: a survey(上)
  6. 下载Windows Terminal送Cascadia Code字体
  7. 测试开发真的不难(5)如何让IDEA实时显示内存
  8. excel批量提取超链接
  9. python代码命名规范
  10. NetVLAD: CNN architecture for weakly supervised place recognition