iPython中指定域上的最大函数 [英] maximum of a function on a specified domain in iPython
本文介绍了iPython中指定域上的最大函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到以下函数的最大值1< R< 20.如何将其实现到代码中?
I am trying to find the maximum of the following function for 1 < R < 20. How can I implement this into the code?
解决方案应该是R约为15.5左右。
The solution is supposed to be R is approx 15.5 or so.
#!/usr/bin/env python
# Plotting the energy for circular Hohmann transfer
import scipy
import matplotlib
import numpy as np
import pylab
def f(R):
return 1 / np.sqrt(R) - (np.sqrt(2) * (1 - R)) / (np.sqrt(2) * (1 + R)) - 1
x = np.arange(1, 20)
pylab.plot(x, f(x), 'r')
pylab.show()
推荐答案
您可以使用 scipy.optimizie.fmin
:
>>> scipy.optimize.fmin(lambda r: -f(r), 10)
Optimization terminated successfully.
Current function value: -0.134884
Iterations: 16
Function evaluations: 32
array([ 11.44451904])
实际最高值是:
>>> x = np.linspace(1, 20, 1000)
>>> plt.plot(x, f(x))
[<matplotlib.lines.Line2D object at 0x0000000007BAEF98>]
>>> plt.show()
这篇关于iPython中指定域上的最大函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文