Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
George
George 2011년 3월 29일
마감: MATLAB Answer Bot 2021년 8월 20일
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