Help with "Index exceeds matrix dimensions" please

조회 수: 2 (최근 30일)
Zachary Thompson
Zachary Thompson 2016년 10월 18일
편집: Swarooph 2016년 10월 18일
Hi, This is the Part of the code I am stuck with-
clc
clear all
E=70e9; %%Youngs modulus (Pa)
A=0.001; %%Area (m)
ks=3e6; %%spring stiffness (Nm)
P=10e3; %%pulling force (N)
nC=[0 0;0 3; 0 7;4 3]; %%Nodal coordinate data
nEl=4; %%No of Elements
nDOF=size(nC,1)*2; %%Number of degrees of freedom
ElConn=[1 4;2 4;3 4;4 5] %%Element Conectivity
%% Formulate Element Stiffness matrices
k= zeros(4,4,nEl);
for count=1:nEl;
n1=ElConn(count,1);
n2=ElConn(count,2);
x1=nC(n1,1)
x2=nC(n2,1)
y1=nC(n1,2);
y2=nC(n2,2);
le=sqrt((x2-x1).^2+(y2-y1).^2);
kdash=((E*A)/le)*[1 -1;-1 1];
l=(x2-x1)/le;
m=(y2-y1)/le;
L=[l m 0 0;0 0 l m];
k(:,:,count)=L'*kdash*L;
end
The Error I am Getting is in line 22 "x2=nC(n2,1)" I think the values I should be getting for x2 are 0, 3, 7 and 3
Thanks in advance for any help

답변 (1개)

Swarooph
Swarooph 2016년 10월 18일
편집: Swarooph 2016년 10월 18일
It seems like during the 4th iteration, n2 is being set to 5 and then x2 is being assigned a value of nC(5,1). However nC is of size 4x2. Hence the error.
Use the MATLAB debugger to help with these types of issues. This documentation will help.
Specifically you can also enter debug mode when encountering errors using the dbstop if error option. Check the documentation here.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by