numerical integration using trap()
Afficher commentaires plus anciens
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?
Réponses (1)
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 commentaires
Bersin Tekmen
le 23 Avr 2022
Star Strider
le 23 Avr 2022
‘But dont I need a for-loop to iterate through the matrix?’
I did not realise that ‘filtered’ is an image. The loop depends on what you are integrating. If there are three channels, then a separate loop for each channel could be necessary, however the trapz calls would be the same otherwise in my code. Integrating individual points will always produce in a zero result.
It will also be necessary to save the results of the trapz calls to a variable (either a scalar or a vector), with or without a loop. If they are not specifically saved, they will get overwritten with the next result that is not saved to a specific variable.
Experiment with your image with my code to see if the result is appropriate.
Bersin Tekmen
le 23 Avr 2022
Star Strider
le 23 Avr 2022
@Bersin Tekmen — I would not use the loops. Do something similar to what I wrote.
Bersin Tekmen
le 23 Avr 2022
Bersin Tekmen
le 23 Avr 2022
Star Strider
le 24 Avr 2022
@Bersin Tekmen — The array incompatibility may relate to the sizes of ‘x’ and ‘y’ with respect to the matrix. I have no idea what they are, so I cannot suggest a specific way to do the multiplications. (In my code, the matrix is square, so everything works.)
Bersin Tekmen
le 24 Avr 2022
Star Strider
le 24 Avr 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.
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 ?
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

