In [19]:
import numpy as np
from scipy.integrate import trapz
from matplotlib import pylab as plt
%matplotlib inline

In [20]:
def grid_vect(a,b,N):
    x = np.linspace(a, b, N, endpoint=True)
    h = x[1] - x[0]
    return np.mgrid[a:b+h:h, a:b+h:h].reshape(2,-1).T

def get_meshgrid(a,b,N):
    x = np.linspace(a, b, N, endpoint=True)
    return np.meshgrid(x,x)

def f(x):
    return x[0]+2*x[1]

xy = grid_vect(0,1,5)
#print(xy)
#fig = plt.figure(figsize=(10,5))
#plt.grid(True)
#plt.scatter(xy[:,0],xy[:,1], marker='x', color = 'r', s = 50)

In [21]:
xg,yg = get_meshgrid(0,1,5)
#fig = plt.figure(figsize=(10,5))
#plt.grid(True)
#plt.scatter(xg,yg, marker='x', color = 'r', s = 50)

In [23]:
from mpl_toolkits.mplot3d.axes3d import Axes3D
fig = plt.figure(figsize=(14,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')

p = ax.plot_surface(xg, yg, f([xg,yg]), rstride=4, cstride=4, linewidth=0)



In [24]:
np.trapz(np.trapz(f([xg,yg]),xg), yg[:,0])


Out[24]:
1.5

In [6]:
np.trapz(f(xg,yg)[1],xg[1])


Out[6]:
1.0

In [7]:
np.trapz(f(xg,yg),xg)


Out[7]:
array([0.5, 1. , 1.5, 2. , 2.5])

In [18]:
xg.shape


Out[18]:
(5, 5)

In [9]:
yg[:,0]


Out[9]:
array([0.  , 0.25, 0.5 , 0.75, 1.  ])

In [12]:
f(xg,yg)


Out[12]:
array([[0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.5 , 0.75, 1.  , 1.25, 1.5 ],
       [1.  , 1.25, 1.5 , 1.75, 2.  ],
       [1.5 , 1.75, 2.  , 2.25, 2.5 ],
       [2.  , 2.25, 2.5 , 2.75, 3.  ]])

In [13]:
xg[1]


Out[13]:
array([0.  , 0.25, 0.5 , 0.75, 1.  ])

In [15]:
f(xg,yg)[1]


Out[15]:
array([0.5 , 0.75, 1.  , 1.25, 1.5 ])

In [17]:
xy.shape


Out[17]:
(25, 2)