numerical integration using trap()

5 views (last 30 days)
Bersin Tekmen
Bersin Tekmen on 23 Apr 2022
Edited: Torsten on 24 Apr 2022
Hi.
I am trying to solve the functions below numerically using MATLAB
this is how I did it using trapz()
%1.Moment
[row,column] = size(filtered);
Summe = 0;
for y=1:row
for x=1:column
trapz(trapz(x.*double(filtered(y,x))));
end
end
first_Moment_X = Summe;
Summe = 0;
Schwerp_X = first_Moment_X./en_density;
Schwerp_X = double(Schwerp_X);
for y=1:row
for x=1:column
trapz(trapz(y.*double(filtered(y,x))));
end
end
first_Moment_Y=Summe;
Schwerp_Y = first_Moment_Y./en_density;
But I get zero as result. Does anyone know what I do wrong?

Answers (1)

Star Strider
Star Strider on 23 Apr 2022
Since ‘filtered’ is apparently a matrix, perhaps —
filtered = randn(10,10);
%1.Moment
[row,column] = size(filtered);
x=1:column;
Summe = trapz(trapz(x.*double(filtered)));
en_density = trapz(trapz(filtered));
first_Moment_X = Summe;
Summe = 0;
Schwerp_X = first_Moment_X./en_density;
Schwerp_X = double(Schwerp_X)
Schwerp_X = 4.1243
y=1:row;
Summe = trapz(trapz(y.*double(filtered)));
first_Moment_Y=Summe;
Schwerp_Y = first_Moment_Y./en_density
Schwerp_Y = 4.1243
This will produce a non-zero result (unless ‘filtered’ is such that the double integral is zero) and unless it is a square matrix, the multiplication of ‘x’ or ‘y’ may need to be transposed so that the multiplication is conformable to the matrix dimensions. (I have no idea what ‘en_density’ is, so I created it by doing a couble integral of ‘filtered’.)
Make appropriate changes to get the desired result.
The zero result appears to be the integration of individual points rather than of the entire matrix, for example —
Q = trapz(trapz(rand))
Q = 0
.
  11 Comments
Star Strider
Star Strider on 24 Apr 2022
I am lost.
I stil believe that the multiplication of the vector ‘x’ or ‘y’ by the ‘filtered’ matrix should be done using either ordinary matrix-vector multiplication or element-wise (array) matrix-vector multiplication, however I have no idea what the code actually does, so I cannot determine which would be correct. The vector-matrix multiplications must be conformable (the dimensions must match) regardless.
Torsten
Torsten on 24 Apr 2022
Edited: Torsten on 24 Apr 2022
Similar to the integration for the first moments, I wonder how you want to do numerical integration if the results depend on a variable, namely z. Don't you need some kind of symbolic integration here ? Or do you have to perform integration for each element in z-direction separately ?

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!