利用割线法求解一元函数极小值
2017-04-27 23:44
471 查看
优化理论__利用割线法求解一元函数极小值(Python实现)
1 问题
对于下述一元函数:ϕ(α)=4(1024α−4)4+(2α−1)2(1)
求取ϕ(α)在[0,+∞]上的极小点α0。
2 分析
利用割线法的算法进行求解,在给定初值α−1和α0的情况下,迭代进行求解,求解算法如下:αk+1=ϕ′(xk)x(k−1)−ϕ′(xk−1)x(k)ϕ′(xk)−ϕ′(x(k−1))(2)
3 编程实现
利用Python进行实现:import sys def derivative(x, dx=1e-6): # Computes the numerical derivative of a function. return (g(x+dx) - g(x)) / dx def g(x): return 4*(1024*x-4)**4 + (2*x-1)**2 def secant_method(aList): a = aList[-1] b = aList[-2] numerator = float(derivative(a)*b - derivative(b)*a) denominator = float(derivative(a) - derivative(b)) step_length = numerator / denominator if g(step_length) < g(aList[-1]): aList.append(step_length) secant_method(aList) else: return aList aList = [1, 0.5] finalList = secant_method(aList) answer = finalList[-1] print(answer)
得到结果:α0=3.67×10−3
4 遇到的问题
问题
最开始采用以下代码:aList = [1, 0.5] finalList = secant_method(aList) answer = finalList[-1] print(answer)
报错如下:
Traceback (most recent call last): File "G:/PyCharm/PycharmProjects/Optimization/An introduction to optimization/diff.py", line 25, in <module> answer = finalList[-1] TypeError: 'NoneType' object is not subscriptable Process finished with exit code 1
问题原因
报错原因在于列表被函数调用是一个in-place过程,将其赋给另一个对象时,传递的是一个None类型的数据解决办法
修改为以下代码即可:aList = [1, 0.5] secant_method(aList) answer = aList[-1] print(answer)
相关文章推荐
- MATLAB中利用最速下降法求解多元函数的局部极小值
- 【翻译】利用加速度求解位置的算法——三轴传感器
- 利用粒子群算法求解非线性二层规划问题(matlab)
- 数据结构第十一周项目(四)——利用遍历思想求解图问题
- 第十二周--利用遍历思想求解图问题(BFS)
- 连载 | 理解线性代数04 利用行列式求解 Ax = b
- 项目4 - 利用遍历思想求解图问题(1-5)
- 第十二周 项目4-利用遍历思想求解图问题(2)
- 十二周 项目四 利用遍历思想求解图问题(5)
- 利用快速排序求解k-th小数
- 利用遍历思想求解图问题(输出一些简单回路)
- 第十二周项目4-利用遍历思想求解图问题
- 十二周项目四--利用遍历思想求解图问题(6-7)
- 第十二周项目4——利用遍历思想求解图问题(1)
- 第十二周项目4——利用遍历思想求解图问题(3)-输出所有路径
- 第十二周项目四 利用遍历思想求解图问题(3、4)
- 第十二周项目4-利用遍历思想求解图问题(2-广度优先遍历)
- 第十二周 利用遍历思想求解图问题 (设计简单路径)
- 二叉树按层从上到下 从左到右输出。利用队列求解。
- 第十二周项目4 利用遍历思想求解图问题(2)