Info

This question is closed. Reopen it to edit or answer.

invalid index in program

1 view (last 30 days)
shiv gaur
shiv gaur on 24 Feb 2022
Closed: Rik on 24 Feb 2022
p=8/3;
r=25;
sigma=10;
npoints =5000;
dt = 0.1;
a = zeros(npoints,1);
b = zeros(npoints,1);
c= zeros(npoints,1);
t = zeros(npoints,1);
//t=1:dt:100;
Invalid use of operator.
a(1)=1;
b(1)=1;
c(1)=1;
suma=0;
sumb=0;
sumc=0;
sumS=0;
sumZ=0;
for i = 1:npoints-1
sumS=sumS+a(i)*c(i-npoints);
sumZ=sumZ+a(i)*b(i-npoints);
a(i+1)=((sigma)/(i+1))*(b(i)-a(i));
b(i+1)=(1/(i+1))*(r*a(i)-b(i)-sumS);
c(i+1)=(1/(i+1))*(sumZ-p*c(i));
suma=suma+a(i).*(t^i);
sumb=sumb+b(i).*(t^i);
sumc=sumc+c(i).*(t^i);
t(i+1) = t(i) + dt;
end;
plot(t,suma)
pl revise the program
  20 Comments
Rik
Rik on 24 Feb 2022
c
l
i
c
k
t
h
e
f
o
r
m
a
t
b
u
t
t
o
n
s
Click the link Jan gave you. Also click this.
Feel free to reopen the question and edit your last comment.

Answers (2)

Walter Roberson
Walter Roberson on 24 Feb 2022
npoints =5000;
for i = 1:npoints-1
sumS=sumS+a(i)*c(i-npoints);
First iteration: i is 1, and a(1) is being indexed; as you initialized a(1) that is valid so far.
Then c(i-npoints) is being indexed. That is c(1-5000) which is c(-4999) which is not a valid index.

Jan
Jan on 24 Feb 2022
Use the debugger to examine the cause of problems. Type this in the command window:
dbstop if error
Run the code again until it stops at the error.
A guess: c(i-npoints) cannot work, because the index is negative.

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by