首页 > 解决方案 > Finding the second integral of the 2D array

问题描述

I have the following 2D array:

N = 6

M = np.zeros((N,N))
for i in range(N-1):
    for j in range(N-1):
        if i==0 or j==0:
            M[i,j] = 0
        else:
            M[i,j] = 1

Basically, it is the array of zeros on the sides and ones in the middle.

I want to calculate the following integral:

enter image description here

So, I use the code below:

dx=dy=d=1
My, Mx = np.gradient(M, d, d)

Lx = 0
p = 1/(M.shape[0]*M.shape[1])
for i in range(N-1):
    for j in range(N-1):
        Lx += p*math.sqrt(1+(Mx[i,j])**2)*dx*dy
    

However, the value that I am getting out of the calculations is not the matching the one it should be: My answer: 0.77 Correct answer: 0.98

Is there anything I am doing wrong?

标签: pythonnumpymathnumerical-methods

解决方案


推荐阅读