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

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

dpb
dpb 2025년 11월 8일
편집: dpb 2025년 11월 8일
Where do the instructions for Step 2 say to store the result?
this is the code i had to modify to solve this task, so i think i have to store the result in I and then use it with function eig, but there is always error "Was I defined correctly?" ,
% 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 =
Izz =
Ixy =
Ixz =
Iyz =
% Composite main inertia matrix I
I = [ ]; %matrix 3x3
% Find the principal moment of inertia, a 3-by-3 diagonal matrix of II
[~,II] = eig(I);
Torsten
Torsten 2025년 11월 9일
편집: Torsten 2025년 11월 9일
In the problem statement, it's written that I should be stored in a variable IM:
% Composite main inertia matrix I
IM = [ ]; %matrix 3x3
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:

댓글을 달려면 로그인하십시오.

답변 (2개)

sneha
sneha 2025년 11월 11일
편집: sneha 2025년 11월 11일

1 개 추천

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

Hi, thank you for your answer, unfortunately it doesnt work too,
here is code my teacher gave, and we need to modify it,
% 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 =
Izz =
Ixy =
Ixz =
Iyz =
% Composite main inertia matrix I
I = [ ]; %matrix 3x3
% Find the principal moment of inertia, a 3-by-3 diagonal matrix of II
[~,II] = eig(I);
and i did so
% Load the x-y-z positions into the variable R
load datafile.mat
m = 1.2; % particle mass in kg
% Center of mass (row vector)
rcm = mean(R, 1);
% Positions relative to the center of mass
Rcm = R - rcm;
x = Rcm(:,1);
y = Rcm(:,2);
z = Rcm(:,3);
% Inertia components about the COM (point masses)
Ixx = m * sum(y.^2 + z.^2);
Iyy = m * sum(x.^2 + z.^2);
Izz = m * sum(x.^2 + y.^2);
% Products of inertia (negative sign convention)
Ixy = - m * sum(x .* y);
Ixz = - m * sum(x .* z);
Iyz = - m * sum(y .* z);
% Inertia matrix about COM in the given xyz frame
I = [ Ixx Ixy Ixz;
Ixy Iyy Iyz;
Ixz Iyz Izz ];
% Store also in IM if the template/tests expect IM
IM = I;
% Principal moments of inertia (diagonal matrix)
[~, II] = eig(I);
But the errors are still the same, and now i dont know where the mistake can be
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
@Jakub, how is the 3x3 input matrix R defined?
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?
Hi
  1. Matrix R layout: R is 100×3. Each row is one particle position [x y z] in the given frame. Verified with: size(R) % returns 100 3
  2. Show Feedback message:
3.Formatting / rounding: No rounding required. I am returning raw double arrays:
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)
ans = logical
0
A robust approach is to use tolerances to compare -
tol = 1e-4;
abs(0.3-(0.2+0.1))<tol
ans = logical
1
%or
isapprox(0.3, 0.2+0.1, RelativeTolerance=tol)
ans = logical
1
Have any of your classmates successfully solved the problem?
Jakub
Jakub 2025년 11월 11일
편집: Jakub 2025년 11월 11일
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
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);

댓글을 달려면 로그인하십시오.

Jakub
Jakub 2025년 11월 12일
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

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!).
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2025년 11월 8일

댓글:

dpb
2025년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by