I would like to use the integral command in scipy and have a function that gets multiplied by each element in the array once.
import math
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as integrate
from scipy.integrate import quad, romberg
import scipy.special as special
from numpy import sqrt
yes = np.array([0,1])
def integrate(x,yes):
return x+yes
result = quad(integrate,0,1,args=(yes))
print(result)
when I do this I get the error only size-1 arrays can be converted to Python scalars
But if I do this
import math
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as integrate
from scipy.integrate import quad, romberg
import scipy.special as special
from numpy import sqrt
yes = np.array([0])
def integrate(x,yes):
return x+yes
result = quad(integrate,0,1,args=(yes))
print(result)
it gives me this (0.5, 5.551115123125783e-15) Which is exactly what I want, but I would like it for each element in the array.
Is there a way to write a for loop? I've also heard of scipy.integrate.quad_vec, but that was not working. Thank you in advance
argsis supposed to get a tuple.(yes)is not a tuple, it is justyes.args=(yes,)is the correct way of passing variableyesto your function. That said,quadcan only integrate one value; your function needs to return a scalar.