SignalProcessing
IntegrateData2D
calculate the volume under a 2-D data set
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
IntegrateData2D( X, Y, Z, options )
IntegrateData2D( Z, options )
X, Y
-
one-dimensional rtables or lists of independent data values of type realcons
Z
two-dimensional rtable or list of dependent data values of type complexcons
method: One of bottomleftcorner, topleftcorner, toprightcorner, bottomrightcorner, averagecorner, and simpson. The default is averagecorner.
sortdata: Either true or false, specifies if the data is to be sorted. The default is true.
steps: List of two numeric values, specifies the constant spacing of the independent data for the IntegrateData2D(Z) calling sequence. The default is [1,1].
tolerance: Positive numeric value which specifies the tolerance for determining if the independent data in X and Y are uniformly spaced. The default is HFloat(5,-15).
uniform: Either true or false, specifies if the independent data in X and Y are to be assumed uniform. The default is false.
The IntegrateData2D command approximates the volume beneath a 2-D data set using the specified method.
For the IntegrateData2D(X,Y,Z) calling sequence, Z must have numelems(X) rows and numelems(Y) columns. The values of Z are assumed to have been generated by some hypothetical function f⁡x,y and satisfy Zi,j=f⁡Xi,Yj for each i and j.
Units are supported in the values of steps and the containers X, Y, and Z (when the rtable shape is a unit).
Internally, the complex case (where Z is complex) is reduced to the real case by applying IntegrateData2D separately to the real and imaginary parts of Z. That is:
IntegrateData2D⁡X,Y,Z=IntegrateData2D⁡X,Y,ℜ⁡Z+I⁢IntegrateData2D⁡X,Y,ℑ⁡Z
For the discussion below, assume the values in Z are real-valued.
When method is any of the "corner methods" and Z has less than two rows or columns, or when method is simpson and Z has less than three rows or columns, the result will be Float(undefined).
Suppose sortdata=true. For the IntegrateData2D(X,Y,Z) calling sequence, X and Y are sorted to be in non-decreasing order and Z is sorted in tandem. For the IntegrateData2D(Z) calling sequence, if one or both values in steps is negative, then the absolute values of these widths is used, and the elements of Z are sorted accordingly. For example, if steps1<0 and 0<steps2, then the rows of Z are sorted in reverse order.
When sortdata=false, the onus is on the user to ensure that the data passed is sorted. For the IntegrateData2D(X,Y,Z) calling sequence, if the data is in fact not sorted, then the result may be useless. For example, if method=simpson and Xi+1=Xi for some i, then a division by zero occurs and the computed volume will be Float(undefined).
For the IntegrateData2D(X,Y,Z) calling sequence, when uniform=true, it is assumed that X and Y are uniformly spaced, with respective spacings X2−X1 and Y2−Y1. When uniform=false, however, the procedure first checks if X and Y are uniform before deciding which auxiliary procedure to use to compute the volume. For X to be considered uniformly spaced, for example, it must satisfy Δi−Δj≤τ for each i and j, where Δi=Xi+1−Xi and τ=tolerance.
Computations using regular/uniform data, compared with irregular/non-uniform data, tend to be quicker and have smaller numerical error in the final result. Regarding speed, for one of the "corner methods" with a fixed number of values in X and Y, the computation typically takes twice as long if X and Y are irregular than if they are regular. For Simpson's Method, the time taken for irregular data is approximately quadruple the time taken for regular data.
The main computations are handled by external C code.
For any of the "corner methods", the following formula is used:
IntegrateData2D⁡X,Y,Z=∑i=1m−1⁡∑j=1n−1⁡zi,j⁢Xi+1−Xi⁢Yi+1−Yi
where m=numelems⁡X and n=numelems⁡Y. For method=bottomleftcorner:
zi,j=Zi,j
For method=topleftcorner:
zi,j=Zi,j+1
For method=toprightcorner:
zi,j=Zi+1,j+1
For method=bottomrightcorner:
zi,j=Zi+1,j
For method=averagecorner:
zi,j=Zi,j+Zi,j+1+Zi+1,j+Zi+1,j+14
Note: method=averagecorner is the 2-D analogue of the Trapezoid Method in 1-D, and for a given sub-box consisting of Xi, Xi+1, Yj, and Yj+1, the volume is that of the best-fit plane through the four points.
We will omit the long and complicated formulas for method=simpson, and include only a description of the derivation. Suppose first that m and n are odd, where m=numelems⁡X and n=numelems⁡Y. Now, partition the grid into non-overlapping sub-boxes with nine points each, formed by points consisting of Xp, Xp+1, Xp+2, Yq, Yq+1, and Yq+2, where p=1..m−12 and q=1..n−12. The formula for the approximate volume over one of these sub-boxes is found by integrating over the sub-box the unique quadratic bivariate polynomial which interpolates these nine points. When one or more of m and n is even, however, sub-boxes of nine points are again considered, but the integration is performed only over the parts that have not yet been counted using the above method.
Any input rtable/list of independent data is converted to an rtable of float[8] datatype having no infinite or undefined values, and an error is thrown if this is not possible. Similarly, the input rtable/list of dependent data is converted to an rtable of either float[8] or complex[8] datatype having no infinite or undefined values, and an error is thrown if this is not possible. For this reason, it is most efficient for any input to already be an rtable having the appropriate datatype. Note: The rtables cannot have a non-unit indexing function, and they need to have rectangular storage.
The IntegrateData2D command is not thread safe.
with⁡SignalProcessing:
Example 1
Consider this quadratic bivariate polynomial:
p≔x,y↦3−2⋅x+5⋅y+4⋅x2−10⋅y⋅x+7⋅y2
a≔0:
b≔10:
c≔0:
d≔20:
plot3d⁡p,a..b,c..d
First, create sample points:
m≔7:
n≔13:
dx≔evalhf⁡b−am−1:
dy≔evalhf⁡d−cn−1:
X≔Vector⁡m,i↦evalhf⁡a+i−1⋅dx,datatype=float8
X≔0.1.666666666666673.333333333333335.6.666666666666678.3333333333333310.
Y≔Vector⁡n,j↦evalhf⁡c+j−1⋅dy,datatype=float8
Z≔Matrix⁡m,n,i,j↦evalhf⁡p⁡Xi,Yj,datatype=float8
Now, compute the volume using Simpson's Method:
α≔IntegrateData2D⁡X,Y,Z,uniform,method=simpson
α≔121933.333333333358
Since the independent data values are uniformly spaced, we can also use the other calling sequence:
IntegrateData2D⁡Z,steps=dx,dy,method=simpson
121933.333333333358
Since Simpson's Method gives the exact answer for such cases, we can compare the result of IntegrateData2D with the expected answer:
β≔evalf15⁡int⁡p⁡x,y,x=a..b,y=c..d
β≔121933.333333333
Example 2
Consider the following function:
f≔x,y↦exp⁡11+x2+y2
f≔x,y↦ⅇ11+x2+y2
plot3d⁡f,0...10,0...10.0
With a uniform spacing of 1.0, Simpson's Method gives the following:
X≔Vector⁡seq⁡0...10.0,1.0,datatype=float8:
Y≔X:
Z≔Matrix⁡numelems⁡X,numelems⁡Y,i,j↦evalhf⁡f⁡Xi,Yj,datatype=float8:
α≔IntegrateData2D⁡X,Y,Z,method=simpson
α≔104.150606423932388
This approximation for the volume is good, but it could be better, as we can see from integrating the original function:
ξ≔int⁡f⁡x,y,x=0...10.0,y=0...10.0,numeric,digits=15
ξ≔104.263869308879
evalhf⁡abs⁡α−ξ
0.113262884946607301
From the plot of the dependent data, we can see that more samples for x and y between 0.0 and 3.0 will improve the approximate volume found:
plots:-matrixplot⁡Z
Since we have the access to the original function, we have the luxury of being able to sample more densely for smaller x and y:
U≔Vector⁡seq⁡0...3.0,0.01,seq⁡4.0..10.0,1.0,datatype=float8:
V≔U:
W≔Matrix⁡numelems⁡U,numelems⁡V,i,j↦evalhf⁡f⁡Ui,Vj,datatype=float8:
β≔IntegrateData2D⁡U,V,W,method=simpson
β≔104.264569012844262
This approximation is much closer:
evalhf⁡abs⁡β−ξ
0.000699703965267417516
Example 3
m≔3:
n≔3:
X≔seq⁡1..m
X≔1,2,3
Y≔seq⁡1..n
Y≔1,2,3
Z≔Matrix⁡m,n,seq⁡1..m⁢n
Z≔123456789
IntegrateData2D⁡X,Y,Z,method=bottomleftcorner
12.
Example 4
Consider the following function and box:
f≔x,y↦sqrt⁡1+sin⁡π⋅x2+3⋅y2
f≔x,y↦1+sin⁡π⋅x2+3⋅y2
a≔0.:
b≔1.0:
c≔−0.5:
d≔1.5:
plot3d⁡f,a..b,c..d
Now, create a Matrix of sample data points:
m≔1000:
n≔1000:
Z≔Matrix⁡m,n,i,j↦evalhf⁡f⁡a+i−1⋅dx,c+j−1⋅dy,datatype=float8:
Finally, approximate the volume, using two different methods:
u≔CodeTools:-Usage⁡IntegrateData2D⁡Z,steps=dx,dy,method=averagecorner
memory used=7.66MiB, alloc change=7.63MiB, cpu time=9.00ms, real time=9.00ms, gc time=0ns
u≔2.06220791967658945
v≔CodeTools:-Usage⁡IntegrateData2D⁡Z,steps=dx,dy,method=simpson
memory used=7.66MiB, alloc change=7.63MiB, cpu time=9.00ms, real time=8.00ms, gc time=0ns
v≔2.06221208164538305
Example 5
Here, will use unsorted and irregular (not uniform) independent data:
f≔x,y↦1.76965408823860471+0.0769328086078387985⋅x−2.65475838418437826⋅y−4.27801178528191528⋅x2−6.15468302818204016⋅y⋅x+7.04479517210091011⋅y2
X≔Array⁡2.44882197894116,0.934363022771893,3.97599950568532,3.82758394074501,1.90779228546504,0.,2.19372179828199,0.172230402514544,4.75111024419177,5.0,1.58549740030430,3.47414311487909,4.11728914163646,0.485658906179238,0.230856953155770,1.38461492480445,0.159164231887103,3.53023044009804,0.855933439057809,3.27738945088778,datatype=float8
X≔2.448821978941160.9343630227718933.975999505685323.827583940745011.907792285465040.2.193721798281990.1722304025145444.751110244191775.1.585497400304303.474143114879094.117289141636460.4856589061792380.2308569531557701.384614924804450.1591642318871033.530230440098040.8559334390578093.27738945088778
Y≔Array⁡6.99076722656686,10.0,5.05957051665142,2.55095115459269,7.51267059305653,2.23811939491137,5.85267750979777,3.40385726666133,9.59743958516081,4.98364051982143,1.18997681558377,1.62611735194631,6.55098003973841,6.79702676853675,0.,2.76025076998578,7.54686681982361,7.09364830858073,6.46313010111265,4.45586200710899,datatype=float8
Y≔6.9907672265668610.5.059570516651422.550951154592697.512670593056532.238119394911375.852677509797773.403857266661339.597439585160814.983640519821431.189976815583771.626117351946316.550980039738416.797026768536750.2.760250769985787.546866819823617.093648308580736.463130101112654.45586200710899
Z≔Array⁡1..numelems⁡X,1..numelems⁡Y,i,j↦evalhf⁡f⁡Xi,Yj,datatype=float8
plot3d⁡f,min⁡X..max⁡X,min⁡Y..max⁡Y
The expected answer, for comparison, is the following:
α≔evalf15⁡int⁡f⁡x,y,x=min⁡X..max⁡X,y=min⁡Y..max⁡Y
α≔5546.55319312876
To approximate the volume, we will use two approaches. For the first approach, just use the sortdata option:
ξ≔IntegrateData2D⁡X,Y,Z,sortdata=true,method=simpson
ξ≔5546.55319312876418
For the second approach, we will manually sort the data:
P≔sortinplace⁡X,output=permutation:
Q≔sortinplace⁡Y,output=permutation:
Z≔ZP,Q:
ζ≔IntegrateData2D⁡X,Y,Z,sortdata=false,method=simpson
ζ≔5546.55319312876418
Example 6
In this example, we show that IntegrateData2D gives the exact answer when the data is generated by a plane.
φ≔x,y↦5.89801239837002367⋅x+6.33497857715261770⋅y−3.14488365480468168
b≔10.0:
c≔0.:
d≔5.0:
plot3d⁡φ,a..b,c..d
For comparison, the int command gives the following:
ν≔evalf15⁡int⁡φ⁡x,y,x=a..b,y=c..d
ν≔2109.13123899635
Using IntegrateData2D with sorted regular data:
m≔15:
n≔20:
X≔Vector⁡m,i↦evalhf⁡a+i−1⋅b−am−1,datatype=float8:
Y≔Vector⁡n,j↦evalhf⁡c+j−1⋅d−cn−1,datatype=float8:
Z≔Matrix⁡m,n,i,j↦evalhf⁡φ⁡Xi,Yj,datatype=float8:
α≔IntegrateData2D⁡X,Y,Z,uniform=true,sortdata=false,method=averagecorner
α≔2109.13123899635002
Using IntegrateData2D with sorted irregular data:
U≔Vector⁡0.,0.344460805029088,1.86872604554379,3.17099480060861,3.81558457093008,4.38744359656398,4.45586200710899,4.89764395788231,6.46313010111265,6.94828622975817,7.65516788149002,7.95199901137063,8.23457828327293,9.50222048838355,10.0,datatype=float8:
V≔Vector⁡0.,0.594988407791883,0.813058675973153,1.11905969745568,1.27547557729635,1.38012538499289,1.70192863333067,2.49182025991071,2.52978525832571,2.92633875489889,3.27549001986920,3.39851338426837,3.49538361328343,3.54682415429036,3.75633529652826,3.77343340991180,4.45451626267899,4.79645712602722,4.79871979258041,5.0,datatype=float8:
W≔Matrix⁡numelems⁡U,numelems⁡V,i,j↦evalhf⁡φ⁡Ui,Vj,datatype=float8:
β≔IntegrateData2D⁡U,V,W,uniform=false,sortdata=false,method=averagecorner
β≔2109.13123899634866
Example 7
The IntegrateData2D command also works with complex data. Here, we find the volume under ⅇx2−1 for x and y in the interval 0,2:
f≔x,y↦exp⁡sqrt⁡x2−1
f≔x,y↦ⅇx2−1
b≔2:
n≔250:
h≔evalhf⁡b−an−1:
Z≔Matrix⁡n,n,i,j↦evalhf⁡f⁡a+i−1⋅h,a+j−1⋅h,datatype=complex8:
v≔IntegrateData2D⁡Z,steps=h,h,method=simpson
v≔7.74723830751058973+1.38245111945337396⁢I
Example 8
Units are supported:
X≔Vector⁡1,2,3,datatype=float8⁢Unit⁡m
X≔1.⁢m2.⁢m3.⁢m
Y≔Vector⁡1,3,5,datatype=float8⁢Unit⁡s
Y≔1.⁢s3.⁢s5.⁢s
Z≔Matrix⁡10,−2,5,7,0,3,−1,6,4,datatype=float8⁢Unit⁡kg
Z≔10.⁢kg−2.⁢kg5.⁢kg7.⁢kg0.3.⁢kg−1.⁢kg6.⁢kg4.⁢kg
h≔1⁢Unit⁡m,2⁢Unit⁡s
h≔m,2⁢s
IntegrateData2D⁡X,Y,Z
23.⁢kg⁢m⁢s
IntegrateData2D⁡Z,steps=h
Starting with Maple 2023, external C code is used for the auxiliary procedures that were formerly implemented in Maple and could be compiled by passing the compiled option. The compiled option is now deprecated, but it is still accepted for backwards compatibility.
The SignalProcessing[IntegrateData2D] command was introduced in Maple 2022.
For more information on Maple 2022 changes, see Updates in Maple 2022.
The SignalProcessing[IntegrateData2D] command was updated in Maple 2024.
See Also
CodeTools[Usage]
int
plots[matrixplot]
SignalProcessing[IntegrateData]
Download Help Document