matlab --error: index must be a positive integer or logical -- how to make movie
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)=1; EDITED-->>from u(j,i)=0 % initial condition
else EDIT -->> j==1 and i==1 becomes j==2,i==2
if (j==2 && i>=2 && i<=50) % 50 is the time step for the pulse
u(j,i)=50;
else
%solution of wa开发者_C百科ve 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))
....
end
Also ,how can i write more efficiently the last loop (with the plots) and how can i create a movie? EDITED-->SOLVED
u(j,i-1) and u(j-1,i) are undefined when i and j = 1; remember, MATLAB matrix indices start from 1, not 0.
精彩评论