How do I create a numpy array containing the values of a multi-variable function?
3 answers
I seem to have found a way:
# define the range of x,y,z
x_range = numpy.linspace(x_min,x_max,x_num)
y_range = numpy.linspace(y_min,y_max,y_num)
z_range = numpy.linspace(z_min,z_max,z_num)
# create arrays x,y,z in the correct dimensions
# so that they create the grid
x,y,z = numpy.ix_(x_range,y_range,z_range)
# calculate the function of x, y and z
sinxyz = numpy.sin(x+y+z)
+5
source to share
The numpy.mgrid function will work equally well:
x,y,z = numpy.mgrid[x_min:x_max:x_num, y_min:y_max:y_num, z_min:z_max:z_num] sinxyz = numpy.sin(x+y+z)
edit: to make it work x_num
, y_num
and z_num
must have a clear number, followed by j
, for example,x,y = numpy.mgrid[-1:1:10j, -1:1:10j]
-1
source to share