I am unsure why my MATLAB code is causing errors and not matching hand calculations?

3 次查看(过去 30 天)
Hand calculation is:
(125*25^3/12+125*25*(52.7-12.5))^2 + (15*160^3/12+15*160*(80+25-52.7))^2
ans = 2.7599e+13
= 16897581.42 or 1.69x10^7mm^4
MATLAB code error
% Dimensions of T-beam
T1 = 125; % Width of flange
T2 = 25; % Width of web
T3 = 160; % Thickness of flange
T4 = 15; % Height of web
T5 = 40; % Distance to K
% Compute the area and first moment of area for each rectangular section
A1 = T1*T2;
y1 = T2/2;
AM1 = A1*y1;
A2 = T3*T4;
y2 = T3/2;
AM2 = A2*y2+T2;
% Compute the total area and first moment of area
A = A1 + A2;
AM = AM1 + AM2;
% Compute the centroid location in the x & y direction
y_bar = ((A1*(y1))+(A2*((y2)+T2)))/((A1)+(A2));
x_bar = T1/2;
% Moment of Inertia
Ixx=((T1*T2^3)/12)+(A1*(y_bar-y1))^2+((T4*T3^3)/12)+(A2*(y2+T2-y_bar))^2;
fprintf('The Moment of Inertia is %s mm^4.\n',Ixx);
The Moment of Inertia is 3.153873e+10 mm^4.
Incorrect - should be 16897581 or 1.69x10^7 as per word calc? - Error in the main Moment of inertia code.

回答(1 个)

Andreas Goser
Andreas Goser 2023-3-14
Could it be a need to write ".^2" rather than "^2"?
  3 个评论
Andreas Goser
Andreas Goser 2023-3-14
Me recommendation is to make the comparison step-by-step so you can see in which calculation the issue occurs. It also helpt to share the full code again once you made an edit.
I just noticed the .^2 vs °2 as this can happen with beginnners. Also ./ vs. / is a frequent issue.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by