numerical integration using trap()

조회 수: 5 (최근 30일)
Bersin Tekmen
Bersin Tekmen 2022년 4월 23일
편집: Torsten 2022년 4월 24일
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?

답변 (1개)

Star Strider
Star Strider 2022년 4월 23일
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
Star Strider
Star Strider 2022년 4월 24일
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 2022년 4월 24일
편집: Torsten 2022년 4월 24일
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 ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by