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

 Réponse acceptée

Star Strider
Star Strider le 29 Mai 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 commentaires

Kushal Chode
Kushal Chode le 29 Mai 2019
Thank you for the swift response, Star Strider.
yeah your are right the x,y and P represent an area in 2D plane.
How can I reshape P data? I mean which format should it be, x y p representing a matrix.
I can import the data as matrix, representing x y p but when I am doing trapz an output of 3 values from 96 values. Did I do anythig wrong.
Star Strider
Star Strider le 29 Mai 2019
My pleasure.
You need to reshape your 3 vectors each into a matrix. This is not difficult. If you attach your matrix as a .txt file, it should be easy to demonstrate this. (The examples of these that I have seen posted vary a bit in their format, so it is not possible to write general code to reshape them.)
Kushal Chode
Kushal Chode le 29 Mai 2019
Please find the data attached.
There is absolutely no way to read your file. I tried every function I could think of, and nothing worked. The problem is:
Warning: File contained a byte-order mark for UTF-16 which does not match the encoding used to
open the file. These bytes will be skipped.
I could not recover the numeric data from the cell arrays.
So, I just copied it to the editor as matrix ‘D’.
Then:
[~,~,v] = unique(D(:,2));
P = reshape(D(:,3), max(v), []);
Parea = trapz(trapz(P))
produced:
Parea =
-1573.85
Kushal Chode
Kushal Chode le 30 Mai 2019
Hi Star Strider,
Sorry for the wrong file format,
Please find the new attached one.
Can you please explain what reshape has done in the pervious statement of yours
No worries. The file format was not the problem. There was some sort of unicode character that prevented MATLAB from importing it corectly, and I could not figure out how to correct for that.
With your present file:
[D,S] = xlsread('TestData.xlsx');
D1r = round(D(:,1),3);
Du = unique(D1r);
D1 = reshape(D(:,1), numel(Du), []);
D2 = reshape(D(:,2), numel(Du), []);
D3 = reshape(D(:,3), numel(Du), []);
figure
surf(D1,D2,D3)
grid on
xlabel('Z')
ylabel('Y')
zlabel('P');
IntPY = trapz(D1(:,1),D3); % Integrate ‘P’ Down Columns
IntP = trapz(D2(1,:),IntPY) % Integrate ‘IntPY’ Across Rows
producing:
IntP =
-1.605790306975311
The reshape function is necessary here in order for your data to integrate properly. (Your data are gridded.) First, my code detects the number of unique elements in the first column of the matrix created by your file. It turns out that the first two columns repeat with a cycle of ‘numel(Du)’, here 5. The reshape call then creates matrices out of the vectors in the ‘D’ matrix, so they can be plotted correctly, and then integrated correctly. (The surf call is not necessary for the integration, however seeing the data helps to guarantee that the results are correct.) To see what reshape does here, look at the original ‘D’ matrix, then look at ‘D1’, ‘D2’ and ‘D3’.
Note that ‘IntP’ is much different from simply summing using ‘trapz(trapz(D3))’ because here I integrated them with respect to their independent variables.
Kushal Chode
Kushal Chode le 30 Mai 2019
Thank you for your detail explantion. I highly appericate it.
Star Strider
Star Strider le 30 Mai 2019
As always, my pleasure.
Pankaj
Pankaj le 16 Déc 2023
Modifié(e) : Pankaj le 16 Déc 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 ?
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
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by