I have two same errors every time i paste a code to check
Mostra commenti meno recenti
This is my task and here is code:% Load the x-y-z positions into the variable R
load datafile.mat
m = 1.2; % particle mass in kg
% Find the center of mass location in a row vector rcm
rcm = mean(R);
% Find the moment of inertia matrix I, a 3-by-3 matrix
Ixx = sum(m*(R(:,2).^2+R(:,3).^2));
Iyy = sum(m*(R(:,1).^2 + R(:,3).^2));
Izz = sum(m*(R(:,1).^2 + R(:,2).^2));
Ixy = -sum(m * R(:,1) .* R(:,2));
Ixz = -sum(m * R(:,1) .* R(:,3));
Iyz = -sum(m * R(:,2) .*R(:,3));
% Composite main inertia matrix I
I = [ Ixx, Ixy, Ixz;
Ixy, Iyy, Iyz;
Ixz, Iyz, Izz ];
% Find the principal moment of inertia
[~,II] = eig(I);
and the errors are

I tried many different methods to solve this but non of them worked :/, thanks for any help
Risposte (2)
Hello,
Code looks almost correct, but the issue looks like with the definition of the moment of inertia matrix. Specifically, you did not subtract the centre of mass from each position before calculating the inertia tensor. The inertia tensor must be calculated about the centre of mass, not the origin.
You used R(:,1), R(:,2), and R(:,3) directly, which are positions relative to the origin.
Try using positions relative to the center of mass: R – rcm
Replace every instance of R(:,...) in your inertia tensor calculation with Rcm(:,...).
To know more about this topic, you can refer to:
Thanks
6 Commenti
Jakub
il 11 Nov 2025
Dyuman Joshi
il 11 Nov 2025
That definition is correct, however for Inertia tensor the definition used is correct. See here - https://en.wikipedia.org/wiki/Moment_of_inertia#Definition_2
Is it [x1 y1 z1; x2 y2 z2; x3 y3 z3]? or its tranpose? or any other way?
Also -
> What is the message/output when you click on Show Feedback?
> Are the output values supposed to be rounded or to be provided in a certain/specific format?
Jakub
il 11 Nov 2025
In case rounding is not required, it would be better to check how answers/outputs are compared. It is likely that your teacher is using isequal to compare floating point numbers, which is not recommended -
isequal(0.3, 0.2+0.1)
A robust approach is to use tolerances to compare -
tol = 1e-4;
abs(0.3-(0.2+0.1))<tol
%or
isapprox(0.3, 0.2+0.1, RelativeTolerance=tol)
Have any of your classmates successfully solved the problem?
Jakub
il 12 Nov 2025
Jakub
il 12 Nov 2025
2 Commenti
Dyuman Joshi
il 12 Nov 2025
Ixy = Izz;
Ixz = Iyy;
Iyz = Ixx;
This doesn't make sense at all.
I'd ask for a clarification from your teacher regarding this.
And it seems you had to do some rounding (and sorting as well!).
dpb
il 12 Nov 2025
Agree w/ @Dyuman Joshi -- the product of intertia terms in the intertia tensor are products, not the same as the moments around the major axes. I think your original solution is correct and this is the wrong answer given what we know here.
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


