Info

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

invalid index in program

4 views (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
shiv gaur
shiv gaur on 24 Feb 2022
sorry jan this is edit program that is running program this is the program of lorenz using time step
we have to plan match with below program using power series now program is right
p=8/3;
r=25;
sigma=10;
npoints =500000;
dt = 0.0001;
a = zeros(npoints,1);
b = zeros(npoints,1);
c = zeros(npoints,1);
time = zeros(npoints,1);
a(1)=1;
b(1)=1;
c(1)=1;
for step = 1:npoints-1
a(step+1)=a(step)+sigma*(b(step)-a(step))*dt;
b(step+1)=b(step)+(-a(step)*c(step)+r*a(step)-b(step))*dt;
c(step+1)=c(step)+(a(step)*b(step)-p*c(step))*dt;
time(step+1) = time(step) + dt;
end;
subplot (2,1,1);
plot(time,z,'b' );
xlabel('time');
ylabel('z');
subplot (2,1,2);
plot (x,z,'g' );
xlabel('x');
ylabel('z')
2.power series
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);
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,sumb)
why not giving match plot
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!