Index exceeds matrix dimensions error

조회 수: 9 (최근 30일)
JaffaCakes
JaffaCakes 2021년 4월 21일
댓글: JaffaCakes 2021년 4월 21일
Hi, I'm running a 105x100 matrix, and it's in line 74 (indicated+attached) that index exceeds matrix dimensions, how can I fix this please?
clear all
format short g
M=100
N=105
tend=5
%Paramters
K=6;
P=0.005
L=201
S=0.2
I=0.3
rw=1
h0=30
c0=3.5
Q=-30
dr=(L-rw)/N
dt=tend/M
D=K/(2*S);
a=D*dt/dr^2
B=D*dt/dr;
y=dt/I*dr;
if dr<2*rw
display('dr<=2*rw')
end
while dr>2*rw
N=input('enter new N')
dr=(L-rw)/N
end
if a<=0.5
display('a<=0.50')
end
while a>=0.5
M=input('enter new M')
dt=tend/M;100
a=D*dt/dr^2
if a>=0.5
display('a not satisfied')
end
end
A=setupmatrix(N,M);
%Boundary conditions for h
A(:,1)=h0; %when t=0
A(N,:)=h0; %when r=L
A
for j=1:M
A(1,j+1)=A(1,j)+2*a*A(2,j)^2-2*a*A(1,j)^2 + (a-(B/2*rw))*(2*dr*Q)/(pi*rw*K)+(P*dt)/S;
end
A
%index into Matrix h
for j=1:M
for i=2:N-1
r=rw+(i-1)*dr ;
A(i,j+1)=A(i,j)+(a+(B/2*r))*A(i+1,j)^2-2*a*A(i,j)^2+(a-(B/2*r))*A(i-1,j)^2+P*dt/S ;
end
end
A
C=setupmatrix(N,M)
d=P*dt+(I-S)*(A(i,j+1)-A(i,j))
for i=1:N
r=rw+(i-1)*dr ;
C(i,1)=c0/(1+exp(-((r-180)/4)));
end
C(N,:)=c0;
for j=1:M
for i=1:N
q=(-K/2)*(A(i+1,j)-A(i,j)/dr); %LINE 74 ERROR
C(i,j+1)=-y*(q/A(i,j))*C(i+1,j)+(1+y*(q/A(i,j)))-d/I*(A(i,j))*C(i,j)
end
end
FUNCTION setupmatrix :
function A = setupmatrix(N,M)
A=zeros(1,M)
i=2:N
A(i,:)=zeros
end
  댓글 수: 4
the cyclist
the cyclist 2021년 4월 21일
FYI, your setupmatrix function is completely unnecessary:
A = zeros(N,M);
accomplishes the same thing, much more compactly and efficiently.
JaffaCakes
JaffaCakes 2021년 4월 21일
I see I'll try this one out. Thanks a lot :)

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
David Hill 2021년 4월 21일
for j=1:M
for i=1:N%changing to N-1 allows script to run
q=(-K/2)*(A(i+1,j)-A(i,j)/dr);%A is 105x101 and N=105; therefore, you are indexing A(106,j) on last loop of i (you cannot do that)
C(i,j+1)=-y*(q/A(i,j))*C(i+1,j)+(1+y*(q/A(i,j)))-d/I*(A(i,j))*C(i,j)
end
end
  댓글 수: 1
JaffaCakes
JaffaCakes 2021년 4월 21일
편집: JaffaCakes 2021년 4월 21일
Amazing makes sense now, Thanks very much :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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