I have a smooth function f(x) = sin(x / 5) * exp(x / 10) + 5 * exp(-x / 2) The task is to explore 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 trying to build h(x) with help of matplotlib.
import math
import numpy as np
from scipy.optimize import minimize
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 int(f(x))
x = np.arange(1, 30)
y = h(x)
plt.plot(x, y)
plt.show()
The code doesn't work. Running the code raises
TypeError: only length-1 arrays can be converted to Python scalars
Using VS I get:


freturns an array. To convert a numpy array to integer you needarray.astype(int).