优化scipy iPython错误

时间:2013-04-02 02:15:10

标签: optimization scipy ipython

在Jamie的帮助post之后,我知道我需要使用scipy.optimize。但是,我一直收到以下错误:

Traceback (most recent call last):
  File "./hw7problem5.py", line 19, in <module>
    print(max_R)
NameError: name 'max_R' is not defined


#!/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)
    max_r = scipy.optimize.fmin(lambda r: 1 / f(r), 20)

x = np.arange(1, 25, 0.001)
pylab.plot(x, f(x), 'r')
pylab.show()

print(max_R)

2 个答案:

答案 0 :(得分:1)

max_R应为max_r。 Python区分大小写。

您也不会将您的功能结果存储在任何地方:

x = np.arange(1, 25, 0.001)
max_r = f(x)

pylab.plot(x, max_r, 'r')
pylab.show()

print(max_r)

答案 1 :(得分:0)

这似乎是由于工作和输出正确的解决方案。

#!/usr/bin/env python
#  Plotting the energy for circular Hohmann transfer

import scipy
import matplotlib
import numpy as np
import pylab
from scipy.optimize import fmin


def f(R):
    return ((1 / np.sqrt(R)) - ((np.sqrt(2) * (1 - R)) / (np.sqrt(R
        * (1 + R)))) - 1)

x = np.arange(1, 20, .001)
max_r = fmin(lambda r: 1.0 / f(r), 20)

pylab.plot(x, f(x), 'r')
pylab.show()

print(max_r)