Info

此问题已关闭。 请重新打开它进行编辑或回答。

error: index must be a positive integer or logical -- create movie --SOLVED

1 次查看(过去 30 天)
EDITED--> SOLVED
Hello, i have this matlab code and it gives me the error i have in title.
% solution of the scalar wave 1d equation
time=1000;
steps=1000;
a=input('Please enter the value of the ratio cdt/dx :');
%preallocate matrix u
u=zeros(steps,time);
%fill matrix u
for i=1:time-1
for j=1:steps-1
if (i==1)
u(j,i)=0; EDIT--> u(j,i)=1;% initial condition
else EDIT --> j==1 becomes j==2
if (j==*2* && i>=*2* && i<=50) % 50 is the time step for the pulse
u(j,i)=50;
else
%solution of wave equation
u(j,i+1)=a*a*(u(j+1,i)-2*u(j,i)+u(j-1,i))+2*u(j,i)-u(j,i-1);-->here is the error
end
end
end
end
for k=1:steps EDIT-->> if mod(i,100)==0
figure(i/100)
plot(u(:,i+1))
plot(u(k,1:100))
plot(u(k,1:200))
plot(u(k,1:300))
plot(u(k,1:400))
plot(u(k,1:500))
plot(u(k,1:600))
plot(u(k,1:700))
plot(u(k,1:800))
plot(u(k,1:900))
plot(u(k,1:999))
end
Also ,how can i write more efficiently the last loop (with the plots) and how can i create a movie in this example? Thank you.
EDITED-->>SOLVED

回答(1 个)

Sean de Wolski
Sean de Wolski 2011-3-29
It's right here:
u(j-1,i))
j = 1; and thus j-1 = 0 which is not a valid index.
to plot more effectively
hold on
before the for-loop so the plots overlay.
  1 个评论
George
George 2011-3-29
Hello.Yes thats the problem.I am trying to change the i and j to start from 2,not from 1.But i am taking straight lines with plot,so sth i have wrong.I have done the code in c++ and there i have "for (int k=1;k<steps;k++){
data1<<k<<"\t"<<u[k][100]<<endl;
data2<<k<<"\t"<<u[k][200]<<endl;......" ,where u is float u[steps][time] and data are files . Isn't ok my approach?

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by