keep getting an error message
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Torsten
on 9 Dec 2023
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 Comments
Torsten
on 9 Dec 2023
Initialize
global_stiffness_matrix = zeros(size(x, 2) * 2);
outside the for-loop.
More Answers (0)
See Also
Categories
Find more on Structural Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!