keep getting an error message
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
Accepted Answer
More Answers (0)
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!