In [1]:
%matplotlib inline
import numpy as np
import scipy.optimize as optimize
import math
from matplotlib import pylab as plt
In [2]:
# заданная функция
def f(x):
return np.sin(x/5.0) * np.exp(x/10.0) + 5 * np.exp (-x/2.0)
# функция принимает только целые значения
def h(x):
return int(f(x))
x = np.arange(1,30.1,0.1)
In [3]:
plt.plot(x,[h(i) for i in x])
plt.plot(x,[f(i) for i in x])
Out[3]:
In [4]:
result = []
In [5]:
x_bfg = 30
res = optimize.minimize(h,x_bfg,method="BFGS")
result.append(round(res.fun,2))
In [6]:
bounds = [(1.0,30.0)]
res = optimize.differential_evolution(h,bounds)
result.append(round(res.fun,2))
res
Out[6]:
In [7]:
result
Out[7]:
In [8]:
file_obj = open('result-week3-Task3.txt', 'w')
file_obj.writelines(res + ' ' for res in map(str, result))
file_obj.close()
In [ ]: