How to numerically calculate volume under the curve?

9 Ansichten (letzte 30 Tage)
Young Chan Jung
Young Chan Jung am 23 Okt. 2024
Bearbeitet: Young Chan Jung am 23 Okt. 2024
Hello, I am plotting surface profile with x,y,z data.
I am now able to plot the surfaces, thank you for the help.
The surface was scanned in x-direction, z-direction is the height. After one x-z scan, the x-z data for the next y is recorded and so on.
I have been using trapazoidal rule (trapz command) to find area under the curve.
Is there a command or built in methods for the calculation of volume under the curve?
Thank you.

Akzeptierte Antwort

John D'Errico
John D'Errico am 23 Okt. 2024
Bearbeitet: John D'Errico am 23 Okt. 2024
You have a surface. (Not a curve as I might say.) How do you compute the area under that surface? Simple. Just call trapz twice. Thus once in x, once in y. And trapz can do the job easily enough.
First, I'll make up some data to form a surface, where I know the true answer just so we can see if it works. (Well, I know it works...)
x = linspace(0,pi,20);
y = linspace(0,pi,30)';
z = sin(x).*sin(y);
surf(x,y,z)
Now, I expect the area under the surface to be 4.
syms X Y
int(int(sin(X)*sin(Y),X,[0,pi]),Y,[0,pi])
ans =
4
WHEW! The memory still works. You lose it if you don't use it. ;-)
dx = x(2) - x(1);
dy = y(2) - y(1);
Now, if you use trapz to integrate along one of the dimensions (first I did x), we get a vector. So ONE result for each value of y.
trapz(dx,z,2)
ans = 30×1
0 0.2157 0.4290 0.6371 0.8379 1.0288 1.2076 1.3723 1.5208 1.6516
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
It is exactly as we did before, with nested calls to int. Integrate on x and then y.
trapz(dy,trapz(dx,z,2),1)
ans = 3.9870
Nd that worked quite well. Not exact, but then, this is just trapezoidal rule.
  1 Kommentar
Young Chan Jung
Young Chan Jung am 23 Okt. 2024
Bearbeitet: Young Chan Jung am 23 Okt. 2024
Thank you very much. I was somehow only getting an scalar value, not a vector, after using trapz command. That's why I thought doing second trapz in y direction with a scalar value was strange. I will try and see if this works with my data, since my y values are a bit different from linspace. Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sahas
Sahas am 23 Okt. 2024
As per my understanding, you calculated the area under the curve using MATLAB's "trapz" function and would like to calculate the volume under the curve using other inbuilt functions.
Refer to the following sample code below which is using "trapz" and "integral2" functions and modify it as suitable to you:
% Define a grid for X and Y
x = [0, 1, 2, 3, 4]; % x-direction
y = [0, 1, 2]; % y-direction
% Define Z as a matrix, where each row corresponds to an x-z scan at a specific y
% For simplicity, let's assume Z = X^2 at each y
Z = [0, 1, 4, 9, 16; % y = 0
0, 1, 4, 9, 16; % y = 1
0, 1, 4, 9, 16];% y = 2
% Calculate the area under each x-z profile
area_x = trapz(x, Z, 2); % Integrate along each row (x-direction)
% Integrate the resulting areas over the y-direction
volume_trapz = trapz(y, area_x);
disp(volume_trapz);
44
% Define the function for Z
Z_fun = @(x, y) x.^2;
% Define the limits for x and y
x_min = 0;
x_max = 4;
y_min = 0;
y_max = 2;
% Calculate the volume using integral2
volume_integral2 = integral2(Z_fun, x_min, x_max, y_min, y_max);
disp(volume_integral2);
42.6667
Refer to the following MathWorks documentation link for more information on the "trapz" and "integral2" functions:
Hope this is beneficial!
  1 Kommentar
Young Chan Jung
Young Chan Jung am 23 Okt. 2024
Thank you for the answer. I was thinking about similar method.
I might be wrong, but isn't 'area_x' the whole area of one whole x-z line as a scalar value? or does this give area of each x section as an array?
To be more clear, my data looks kinda like this:
x y z
------------ first y line --------
0 0 1 (1)
1 0 3 (2)
2 0 5 (3)
3 0 7 (4)
------------ second y line ------
0 1 2 (5)
1 1 4 (6)
2 1 6 (7)
3 1 8 (8)
and so one
If I were to use trapazoidal rule for volume, I need to do it for each 4 points, like the volume of (1)-(2)-(5)-(6), next (2)-(3)-(6)-(7).. and add them all together to get the total volume.
If your code gives area_x as an array for each xi to xj, i think this is perfect, but as of my understanding, trapz gives area of whole x-z curve. Sorry if i am wrong on this.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by