Index exceeds matrix dimensions error

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 comentarios

David Hill
David Hill el 21 de Abr. de 2021
What is setupmatrix() function?
Oh srry, forgot to include this, It just calculates the matrix for NXM as zeros
function A = setupmatrix(N,M)
A=zeros(1,M)
i=2:N
A(i,:)=zeros
end
FYI, your setupmatrix function is completely unnecessary:
A = zeros(N,M);
accomplishes the same thing, much more compactly and efficiently.
JaffaCakes
JaffaCakes el 21 de Abr. de 2021
I see I'll try this one out. Thanks a lot :)

Iniciar sesión para comentar.

 Respuesta aceptada

David Hill
David Hill el 21 de Abr. de 2021
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 comentario

JaffaCakes
JaffaCakes el 21 de Abr. de 2021
Editada: JaffaCakes el 21 de Abr. de 2021
Amazing makes sense now, Thanks very much :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 21 de Abr. de 2021

Comentada:

el 21 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by