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

27 Ansichten (letzte 30 Tage)
Jakub
Jakub am 8 Nov. 2025
Kommentiert: dpb am 12 Nov. 2025
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 Kommentare
Torsten
Torsten am 9 Nov. 2025
Bearbeitet: Torsten am 9 Nov. 2025
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 am 9 Nov. 2025
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:

Melden Sie sich an, um zu kommentieren.

Antworten (2)

sneha
sneha am 11 Nov. 2025
Bearbeitet: sneha am 11 Nov. 2025
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 Kommentare
Jakub
Jakub am 11 Nov. 2025
Bearbeitet: Jakub am 11 Nov. 2025
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 am 12 Nov. 2025
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);

Melden Sie sich an, um zu kommentieren.


Jakub
Jakub am 12 Nov. 2025
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 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
dpb am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by