I have a function involves a double integration from -10 to 10 in x and -10 to 10 in y, and I am using the built in quadrature function in SciPy to do this. However, the exact function that needs to be integrated needs user input.
The function is A*cos(x+y), where A needs to be a user input function.
For example, sometimes the function is a constant, and A is 2. So the integral will be of 2*cos(x+y) from -10 to 10 in both x and y. I want the user to input 2 when prompted for A.
But sometimes, the user may want A to be sin(x). So the integral will be of sin(x)*cos(x+y) from -10 to 10 again. I want the user to input np.sin(x) when prompted for A.
Or maybe the user might need A to be np.pi*y*e^(x/3).
A is a string when user inputs value. How do I get it to become a piece of code such that if the user inputs np.sin(x), then later on in the code at the quadrature, Python reads it as np.sin(x)*np.cos(x+y)?
I'm okay if the user inputs wrongly (say, he misspells np.sin as np.siin) and an error is returned.
What I have written is below:
import numpy as np
from scipy.integrate import dblquad
A = input('Enter your function here: ')
def function(x,y):
return A*(np.cos(x+y))
integral = dblquad(function, -10, 10, lambda x:-10, lambda x:10)
print integral
Appreciate any help given.