numerical integration using trap()
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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?
0 comentarios
Respuestas (1)
Star Strider
el 23 de Abr. de 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)
y=1:row;
Summe = trapz(trapz(y.*double(filtered)));
first_Moment_Y=Summe;
Schwerp_Y = first_Moment_Y./en_density
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))
.
11 comentarios
Star Strider
el 24 de Abr. de 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
el 24 de Abr. de 2022
Editada: Torsten
el 24 de Abr. de 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 ?
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

