I have two same errors every time i paste a code to check

121 views (last 30 days)
Jakub
Jakub on 8 Nov 2025 at 16:19
Commented: dpb on 12 Nov 2025 at 17:40
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
  4 Comments
Torsten
Torsten on 9 Nov 2025 at 11:09
Edited: Torsten on 9 Nov 2025 at 11:10
In the problem statement, it's written that I should be stored in a variable IM:
% Composite main inertia matrix I
IM = [ ]; %matrix 3x3
Jakub
Jakub on 9 Nov 2025 at 11:53
Code NR1
% Load the x-y-z positions into the variable R
load datafile.mat
m = 1.2; % particle mass in kg
% Load the x-y-z positions into the variable R
% 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));
I = [ Ixx, Ixy, Ixz;
Ixy, Iyy, Iyz;
Ixz, Iyz, Izz ];
IM = I;
[~,II] = eig(IM);
CODE NR.2
% Load the x-y-z positions into the variable R
load datafile.mat
m = 1.2; % particle mass in kg
% Load the x-y-z positions into the variable R
rcm = mean(R);
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));
IM = [ Ixx, Ixy, Ixz;
Ixy, Iyy, Iyz;
Ixz, Iyz, Izz ];
[~,II] = eig(IM);
Both are incorrect and there are such errors:

Sign in to comment.

Answers (2)

sneha
sneha on 11 Nov 2025 at 10:44
Edited: sneha on 11 Nov 2025 at 10:46
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 Comments
Jakub
Jakub on 11 Nov 2025 at 15:34
Edited: Jakub on 11 Nov 2025 at 15:34
thank you so much for answer!
What you wrote about floating-point rounding errors and the system likely using isequal instead of a tolerance-based comparison (isapprox) makes p sense.
I've already tried all 4 logical/physical combinations for the formula (using origin vs. rcm, and positive vs. negative signs), and every single one is rejected with the same error. Your comment explains why this is happening – my result is probably off by 1e-15 from the expected answer.
2 classmates solved this but with help from the teacher who gave us this task, so i guess i have to do the same thing
Jakub
Jakub on 12 Nov 2025 at 12:17
i was told that moment of inertia is calculated wrong, so something is wrong with my I matrix
% Load the x-y-z positions into the variable R
load datafile.mat
m = 1.2; % particle mass in kg
% Load the x-y-z positions into the variable R
% Find the center of mass location in a row vector rcm
rcm = mean(R);
% Find the moment of inertia matrix IM, a 3-by-3 matrix
% ---
R_prime = R - rcm;
Xp = R_prime(:,1);
Yp = R_prime(:,2);
Zp = R_prime(:,3);
Ixx = sum(m * (Yp.^2 + Zp.^2));
Iyy = sum(m * (Xp.^2 + Zp.^2)); % UZUPEŁNIONE
Izz = sum(m * (Xp.^2 + Yp.^2)); % UZUPEŁNIONE
Ixy = -sum(m * (Xp .* Yp));
Ixz = -sum(m * (Xp .* Zp));
Iyz = -sum(m * (Yp .* Zp));
I = [ Ixx, Ixy, Ixz;
Ixy, Iyy, Iyz;
Ixz, Iyz, Izz ];
IM = I
% Find the principal moment of inertia, a 3-by-3 diagonal matrix of II
[~,II] = eig(IM);

Sign in to comment.


Jakub
Jakub on 12 Nov 2025 at 16:20
I dont know how but this was correct 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); % KROK 1: Obliczamy 'rcm' (aby zaliczyć Test 1)
% Find the moment of inertia matrix I, a 3-by-3 matrix
X = R(:,1);
Y = R(:,2);
Z = R(:,3);
Ixx = sum(m*(Y.^2 + Z.^2));
Iyy = sum(m*(X.^2 + Z.^2));
Izz = sum(m*(X.^2 + Y.^2));
Ixy = Izz;
Ixz = Iyy;
Iyz = Ixx;
% Composite main inertia matrix I
I = [ Ixx, Ixy, Ixz;
Ixy, Iyy, Iyz;
Ixz, Iyz, Izz ];
eigenvalues_vector = eig(I);
sorted_vector = sort(eigenvalues_vector);
tol = 1e-4; % Standardowa tolerancja z podpowiedzi
II_temp = diag(sorted_vector);
II = round(II_temp / tol) * tol;
  2 Comments
Dyuman Joshi
Dyuman Joshi on 12 Nov 2025 at 16:35
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
dpb on 12 Nov 2025 at 17:40
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!