フィルターのクリア

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 Center および File ExchangeStructural Analysis についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by