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

조회 수: 119 (최근 30일)
Jakub
Jakub 2025년 11월 8일 16:19
댓글: dpb 2025년 11월 12일 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
Torsten
Torsten 2025년 11월 9일 11:09
편집: Torsten 2025년 11월 9일 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 2025년 11월 9일 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:

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

답변 (2개)

sneha
sneha 2025년 11월 11일 10:44
편집: sneha 2025년 11월 11일 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
Jakub
Jakub 2025년 11월 11일 15:34
편집: Jakub 2025년 11월 11일 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 2025년 11월 12일 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);

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


Jakub
Jakub 대략 23시간 전
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
Dyuman Joshi
Dyuman Joshi 2025년 11월 12일 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 대략 21시간 전
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.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by