'trying to use a meshgrid instead of a double for loop
I was wondering does using a mess grid instead of a double for loop make the code run faster if so how do a do it
def f(x, y):
return np.sin(x)*np.cos(y/5)
print(f(1,2))
def midpoint_I(D, nx, ny):
hx = (D[1] - D[0])/float(nx)
hy = (D[2] - D[3])/float(ny)
I = 0
for i in range(nx):
for j in range(ny):
xi = D[0] + hx/2 + i*hx
yj = D[2] + hy/2 + j*hy
I += hx*hy*f(xi, yj)
return I
D= [0,5,0,5]
N =100
M=100
print(np.absolute(midpoint_I(D,N,M)))
I tried adouble loop it worked but took a bit too long wondering is a meshgrid instead would work faster
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
