I need to integrate the expression
using scipy's integrate module.
I have tried
def K(t,s):
return np.exp(lbda*(T-s))*np.exp(lbda*t)/(1-np.exp(lbda*T))
def sigma(s):
return np.sin(2*np.pi*s)
def integral(t):
result = integrate.quad(K*sigma, 0, T)
return result
But obviously it's wrong since K
has two variables, one of which would be undefined. How do I proceed?
For reference the function integral would be called with different values for t
.
The first parameter of quad
must be a callable whose first parameter is the integration variable. You'll have to create a new function (or use a lambda expression) for the integrand that calls your existing functions. It is also probably a good idea to make all the parameters that you'll use into function arguments, to avoid the need for global variables. You can pass these in via the args
parameter of quad
.
For example,
def K(t, s, lbda, T):
return np.exp(lbda*(T-s))*np.exp(lbda*t)/(1-np.exp(lbda*T))
def sigma(s):
return np.sin(2*np.pi*s)
def integrand(s, t, lbda, T):
return K(t, s, lbda, T) * sigma(s)
def integral(t, lbda, T):
result = integrate.quad(integrand, 0, T, args=(t, lbda, T))[0]
return result
Then the code that uses these function might look something like this:
# Define the parameters
lbda = 1.25
T = 8.0
# Define the t value.
t = 0.5
# Evaluate the integral at t.
y = integral(t, lbda, T)