Having trouble getting a for loop to create similar matrices (error "Index in position 1 exceeds array bounds (must not exceed 1).")

I am solving a system of arrays in matlab for a finite element analysis class. I am using a for loop to create L arrays that after looped will be changed based on the "Element" number that is being presented. The array is created properly the first time through the loop but the second time through it fails on line 7 each time with the error "Index in position 1 exceeds array bounds (must not exceed 1)." The first L array that is created is L=[1 0;0 1;0 0;0 0;0 0]; the second array should be L=[0 0;1 0;0 1;0 0;0 0]; Can anyone figure out how I am getting this error? Also I have used the exact same code before to make 4x5 matrices for L and it worked fine. Below is the function I use to construct the L with some other calculations. The second set of code is the inputs to the function.
function [KG,U] = Stiffness1(Elements,Nodes,E,A,L,BC,F)
KG=zeros(length(Nodes));
for i=1:length(Elements)
k(i)=(E*A(i))/L;
ke=k(i)*[1 -1;-1 1];
L0=zeros(length(Nodes),2);
L0(Elements(i,1),1)=1;
L0(Elements(i,2),2)=1;
Lt=transpose(L0);
ki=L0*ke*Lt;
KG=KG+ki;
end
end
E=10.4e6;
L=2.5;
A=[1 2 3 4];
Elements=[1 2,2 3,3 4,4 5];
Nodes=[0 0;0 2.5;0 5;0 7.5;0 10];
BC=[1 0;2 1;3 1;4 1;5 0];
F=[0 0 0 0 -1000]; %Maybe Divide by A?????
[KG,U] = Stiffness1(Elements,Nodes,E,A,L,BC,F);

Answers (1)

The issue stems from: L0(Elements(i,1),1)=1; Elements is a single row vector and thus when i iterates to 2, it fails. I'd guessing you intended to access a different index.

This question is closed.

Products

Release

R2019a

Asked:

on 20 Sep 2019

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!