I'm trying to solve an integral inside a for loop where the value of the integral changes with every loop.
Ni_1D = np.array([(x/2)*(x-1), x-x**2, (x/2)*(x+1)])
Tn= [10.0, 10.0, 10.0]
Tt=[0, 0, 0]
y_coor = [0.0, 1.25, 2.5]
x_coor = [4.0, 4.0, 4.0]
dN1D = np.array([x-.5, x-2*x, x+.5])
for i in range(3):
def f(x):
return Ni_1D[i]*(Tn[i]*y_coor[i]+Tt[i]*x_coor[i])* dN1D[i]
fx[i] = integrate.quad(f,-1,1)
But I keep getting the error "cannot convert expression to float." Obviously something is wrong with my first input to quad.
I can't figure out how to get the correct input for f for quad(). When I paste in the function manually everything works fine. For example for i=1 f=-x*(-12.5x**2 + 12.5x). When I define the function direction as -x*(-12.5x**2 + 12.5x) everything works fine.
When code doesn't work as expected, look at the pieces. Don't assume anything.
For specific
ivalue:Or wrapping that in the function:
Regardless of what argument you supply, the result is still a
sympymulobject, not some number. Thexin the sympy expression is not the function argument.To supply a number for a symbol in a sympy expression, you have to use
subs. Andquadis not going to do that for you.If you really must use sympy, use
lambdify, for example as inDoing the sympy lambdify every time
quadwants a value is going to slow down the evaluation considerably.sympyis not fast.