keep getting an error message
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structural Analysis 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!