7
A
回答
10
import scipy.optimize as optimize
def func(x):
return -x**3+1
# This finds the value of x such that func(x) = x, that is, where
# -x**3 + 1 = x
print(optimize.fixed_point(func,0))
# 0.682327803828
的Python代碼定義fixed_point
是SciPy的/優化/ minpack.py。確切位置取決於安裝在哪裏scipy
。你可以發現,在鍵盤上輸入
In [63]: import scipy.optimize
In [64]: scipy.optimize
Out[64]: <module 'scipy.optimize' from '/usr/lib/python2.6/dist-packages/scipy/optimize/__init__.pyc'>
這裏是SciPy的0.7.0代碼:
def fixed_point(func, x0, args=(), xtol=1e-8, maxiter=500):
"""Find the point where func(x) == x
Given a function of one or more variables and a starting point, find a
fixed-point of the function: i.e. where func(x)=x.
Uses Steffensen's Method using Aitken's Del^2 convergence acceleration.
See Burden, Faires, "Numerical Analysis", 5th edition, pg. 80
Example
-------
>>> from numpy import sqrt, array
>>> from scipy.optimize import fixed_point
>>> def func(x, c1, c2):
return sqrt(c1/(x+c2))
>>> c1 = array([10,12.])
>>> c2 = array([3, 5.])
>>> fixed_point(func, [1.2, 1.3], args=(c1,c2))
array([ 1.4920333 , 1.37228132])
See also:
fmin, fmin_powell, fmin_cg,
fmin_bfgs, fmin_ncg -- multivariate local optimizers
leastsq -- nonlinear least squares minimizer
fmin_l_bfgs_b, fmin_tnc,
fmin_cobyla -- constrained multivariate optimizers
anneal, brute -- global optimizers
fminbound, brent, golden, bracket -- local scalar minimizers
fsolve -- n-dimenstional root-finding
brentq, brenth, ridder, bisect, newton -- one-dimensional root-finding
"""
if not isscalar(x0):
x0 = asarray(x0)
p0 = x0
for iter in range(maxiter):
p1 = func(p0, *args)
p2 = func(p1, *args)
d = p2 - 2.0 * p1 + p0
p = where(d == 0, p2, p0 - (p1 - p0)*(p1-p0)/d)
relerr = where(p0 == 0, p, (p-p0)/p0)
if all(relerr < xtol):
return p
p0 = p
else:
p0 = x0
for iter in range(maxiter):
p1 = func(p0, *args)
p2 = func(p1, *args)
d = p2 - 2.0 * p1 + p0
if d == 0.0:
return p2
else:
p = p0 - (p1 - p0)*(p1-p0)/d
if p0 == 0:
relerr = p
else:
relerr = (p-p0)/p0
if relerr < xtol:
return p
p0 = p
raise RuntimeError, "Failed to converge after %d iterations, value is %s" % (maxiter,p)
2
試試SymPy庫。這裏有一個relevant example:
>>> solve(x**3 + 2*x**2 + 4*x + 8, x)
[-2*I, 2*I, -2]
我不知道哪種算法SymPy用來解方程,雖然。
相關問題
- 1. 具有固定點
- 2. 即使在不同的迭代過程中,CGContextShowTextAtPoint也具有固定大小?
- 3. 定點迭代
- 4. 求解等式迭代
- 5. 用迭代迭代迭代求解矩陣行
- 6. 在Matlab中實現積分方程的迭代求解
- 7. 並行迭代算法用於求解線性方程組
- 8. 將求解和dsolve結合起來求解具有微分和代數方程的方程系統
- 9. 如何求解具有求和的方程?
- 10. 如何迭代直到Clojure中的一個固定點?
- 11. xslt-1.0迭代固定值列表
- 12. 迭代固定次數的樹形圖
- 13. 具有固定查詢的Prolog程序
- 14. 具有迭代解決方案的Java迴文(布爾方法)(while循環)
- 15. 求解方程
- 16. 解析帶有固定位置的令牌的程序代碼
- 17. 求解所有解的三角方程
- 18. 的forEach()迭代沒有定義方法
- 19. 線性迭代求解器與直接求解器的穩定性
- 20. 在ruby中求解迭代算法
- 21. vba求解 - 爲迭代添加延遲
- 22. 求解兩個在Java代數方程
- 23. 用Matlab求解代數方程
- 24. 具有固定部件
- 25. 求解具有時間延遲的耦合微分方程
- 26. 的Python/Sympy:求解方程具有不同值的
- 27. 求解兩個方程的系統具有兩個未知數
- 28. 求解方程慢
- 29. 方程求解X
- 30. CPU上最快的多線程迭代稀疏求解器?