求解方程 2x−x3=0
解:可以采用不动点迭代的方式求出数值解。
其不动点是 x=3log2x ,故可以采用下式进行不动点迭代:
xn+1=3log2xn
初始值为 x0=2 ,迭代终止条件是 |xn+1−xn|≤100∗np.spacing(1) 其中, np.spacing(1)=2.22044604925e−16
用python计算,迭代44步收敛,得到的解为9.93953514143
#coding=utf8
"""
# Author: waleking
# Created Time : 三 6/27 20:06:16 2012
Last Modified: 三 6/27 21:49:42 2012
# File Name: fixedpoint.py
# Description:
不动点迭代方法
如,解2^x-x^3=0,即解x=3ln(x)/ln(2)
迭代过程:x(n+1)=3ln(x(n))/ln(2)
初始值选择为x(0)=1
迭代终止条件为|x(n+1)-x(n)|<=100*np.spacing(1)
收敛性说明:当初始值小于1.4的时候,不动点迭代不收敛;当初始值大于1.4时,不动点迭代收敛
"""
import numpy as np
import matplotlib.pylab as plt
xlist=list()
#初始值x0=2
x=2
xlist.append(x)
while(1):
y=3*np.log2(x)
xlist.append(y)
if(np.linalg.norm(y-x)<100*np.spacing(1)):
break
else:
x=y
print("solution of 2^x-x^3=0 is %s" % x)
print("value of 2^x-x^3 is %s*%s" % ((np.power(2,x)-np.power(x,3))/(np.spacing(1)),np.spacing(1)))
print("iteration procedure is %s" % xlist)
plt.plot(xlist,np.power(2,xlist),"ok-")
plt.plot(xlist,np.power(xlist,3),"*r-")
base=np.linspace(2,10,1000)
plt.plot(base,np.power(2,base),"g-")
plt.plot(base,np.power(base,3),"b-")
plt.show()
如下图,蓝色为y=x^3,绿色为y=2^x,最终的结果为9.93953514143
