필터 지우기
필터 지우기

keep getting an error message

조회 수: 1 (최근 30일)
Hector Meza
Hector Meza 2023년 12월 9일
댓글: Torsten 2023년 12월 9일
i keep getting an error mesage when im trying to write a script for school. Error message reads "Index in position 1 exceeds array bounds. Index must not exceed 2.Error in gp (line 29)
dx = x(node2, 1) - x(node1, 1);" how can i fix this problem.
% Define the truss geometry
L = 0.3; % Length of horizontal and vertical members (m)
Lsqrt2 = sqrt(2) * L; % Length of inclined members (m)
E = 100e9; % Young's modulus (N/m²)
A = 1e-4; % Cross-sectional area (m²)
% Define the node coordinates
x = [0, 0.3, 0.3, 0; 0, 0, 0.3, 0.3,];
% Define the connectivity matrix
connectivity = [1, 2, 3, 1; 1, 4, 3, 2; 2, 4, 1, 3];
% Define the external loads
Fx13 = [10000; 0; 0; 0];
Fx14 = [-10000; 0; 0; 0];
Fy13 = [0; 10000; 0; 0];
Fy14 = [0; -10000; 0; 0];
% Initialize the element stiffness matrix
k = zeros(4, 4);
% Loop over the elements
for i = 1:size(connectivity, 1)
% Extract the node numbers for the current element
node1 = connectivity(i, 1);
node2 = connectivity(i, 2);
% Calculate the cosine and sine of the element angle
dx = x(node2, 1) - x(node1, 1);
dy = x(node2, 2) - x(node1, 2);
cos_theta = dx / sqrt(dx^2 + dy^2);
sin_theta = dy / sqrt(dx^2 + dy^2);
% Calculate the element stiffness matrix
k(1, 1) = (E * A * cos_theta ^ 2) / L;
k(2, 2) = (E * A * sin_theta ^ 2) / L;
k(1, 2) = (E * A * cos_theta * sin_theta) / L;
k(2, 1) = k(1, 2);
% Assemble the global stiffness matrix
global_stiffness_matrix = zeros(size(x, 1) * 2);
global_stiffness_matrix(2 * node1 - 1, 2 * node1 - 1) = global_stiffness_matrix(2 * node1 - 1, 2 * node1 - 1) + k(1, 1);
global_stiffness_matrix(2 * node1 - 1, 2 * node1) = global_stiffness_matrix(2 * node1 - 1, 2 * node1) + k(1, 2);
global_stiffness_matrix(2 * node1, 2 * node1 - 1) = global_stiffness_matrix(2 * node1, 2 * node1 - 1) + k(2, 1);
global_stiffness_matrix(2 * node1, 2 * node1) = global_stiffness_matrix(2 * node1, 2 * node1) + k(2, 2);
global_stiffness_matrix(2 * node2 - 1, 2 * node2 - 1) = global_stiffness_matrix(2 * node2 - 1, 2 * node2 - 1) + k(1, 1);
global_stiffness_matrix(2 * node2 - 1, 2 * node2) = global_stiffness_matrix(2 * node2 - 1, 2 * node2) + k(1, 2);
global_stiffness_matrix(2 * node2, 2 * node2 - 1) = global_stiffness_matrix(2 * node2, 2 * node2 - 1) + k(2, 1);
end
Index in position 1 exceeds array bounds. Index must not exceed 2.

채택된 답변

Torsten
Torsten 2023년 12월 9일
x is a 2x4 matrix.
So as soon as node1 or node2 in the lines
% Calculate the cosine and sine of the element angle
dx = x(node2, 1) - x(node1, 1);
dy = x(node2, 2) - x(node1, 2);
are greater than 2, MATLAB will error.
Maybe you mean
% Calculate the cosine and sine of the element angle
dx = x(1, node2) - x(1, node1);
dy = x(2, node2) - x(2, node1);
?
  댓글 수: 2
Hector Meza
Hector Meza 2023년 12월 9일
but now im getting this error
Index in position 1 exceeds array bounds. Index must not exceed 4.
Error in gp (line 47)
global_stiffness_matrix(2 * node2 - 1, 2 * node2 - 1) =
global_stiffness_matrix(2 * node2 - 1, 2 * node2 - 1) + k(1, 1);
Torsten
Torsten 2023년 12월 9일
Initialize
global_stiffness_matrix = zeros(size(x, 2) * 2);
outside the for-loop.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structural Analysis에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by