I have a smooth function f(x) = sin(x / 5) * exp(x / 10) + 5 * exp(-x / 2) The task is to find a minimum of a non-smooth function h(x) = int(f(x)) on the interval of 1 to 30. In other words, each value of f (x) is converted to type int and the function takes only integer values.
I am using 2 methods to find a minimum from scipy.optimize: minimize and differential_evolution. The minimize gives me the result of -5 whereas differential_evolution gives Index error: tuple index out of range The question is why and what is wrong?
Here is the code:
import math
import numpy as np
from scipy.optimize import minimize
from scipy.optimize import differential_evolution
from scipy.linalg import *
import matplotlib.pyplot as plt
def f(x):
return np.sin(x / 5.0) * np.exp(x / 10.0) + 5 * np.exp((-x / 2.0))
def h(x):
return f(x).astype(int)
x = np.arange(1, 30)
y = h(x)
plt.plot(x, y)
plt.show()
#res = minimize(h, 30, method='BFGS')
#print res
res = differential_evolution(h, [1, 30])
print res