matlab --错误:索引必须是正整数或逻辑 -- 如何制作电影
我有这个 matlab 代码,它给了我标题中的错误。
% 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 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))
....
end
另外,我如何才能更有效地编写最后一个循环(带有情节)以及如何创建电影? 已编辑-->已解决
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 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))
....
end
Also ,how can i write more efficiently the last loop (with the plots) and how can i create a movie?
EDITED-->SOLVED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 i 和 j = 1 时,u(j,i-1) 和 u(j-1,i) 未定义;请记住,MATLAB 矩阵索引从 1 开始,而不是从 0 开始。
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.