Issue
I am trying to find the maximum of the following function for 1 < R < 20. How can I implement this into the code?
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()
Solution
You can use 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])
Which is where the maximum actually is:
>>> x = np.linspace(1, 20, 1000)
>>> plt.plot(x, f(x))
[<matplotlib.lines.Line2D object at 0x0000000007BAEF98>]
>>> plt.show()
Answered By - Jaime
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.