Double integral of numerical data

22 views (last 30 days)
Hi,
Thank you for viewing the question,
I want to perform double intergal of numerical data I have. It represents pressure at every point and the data is imported as column vectors, x, y (respresenting area in 2D) and P. I have to get the force, as force is given as double_intergal (p(x,y)). Trapz is a option available in Matlb to perform intergation on numerical data, how can I use trapz as double integral? and is there any method alternate to trapz which I can use.
Thank you
Cheers
KC

Accepted Answer

Star Strider
Star Strider on 29 May 2019
... how can I use trapz as double integral?
If your x, y, and P vectors represent an area in 2D, they may be gridded. You would first need to use the reshape function to get them into matrices (this is not difficult), then use trapz on each dimension:
P = rand(5,6); % Create ‘P’ Matrix
I1 = trapz(P) % First Integration
Result = trapz(I1) % Second Integration
  10 Comments
Pankaj
Pankaj on 16 Dec 2023
Edited: Pankaj on 16 Dec 2023
@Star Strider Thanks for the detailed ans.I have a question, lets say I have a three column data set x, y, p(x,y) and the data is such that for each value of x, there is 100 values of y , so basically the first column values are repeating 100 times for 100 values of y and for each pair of x,y there is a p(x,y) present. And there are 200 unique x and 100 unique y. How to get the double integration of p(x,y) in that case ?
Star Strider
Star Strider on 16 Dec 2023
I get the impression that the data are gridded. If so, you first need to reshape them all to matrices, then choose the appropriate row or column of each ‘x’ and ‘y’ matrix that increment (rather than being constant), and use that and the matching dimension of ‘p’ to do each integration.
That would go something like this —
x = [1;2;3;1;2;3;1;2;3];
y = [1;1;1;2;2;2;3;3;3];
p = randn(9,1);
xr = reshape(x, 3, [])
xr = 3×3
1 1 1 2 2 2 3 3 3
yr = reshape(y, 3, [])
yr = 3×3
1 2 3 1 2 3 1 2 3
pr = reshape(p, 3, [])
pr = 3×3
-1.0117 1.0616 -0.3688 1.2490 -0.5830 -0.1692 -1.6868 0.9415 0.2320
int2 = trapz(xr(:,1), pr, 2)
int2 = 3×1
0.3714 -0.0430 0.2141
intp = trapz(yr(1,:), int2)
intp = 0.2497
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!