2011-03-28 26 views
0

我有这个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 

此外,我怎样才能更有效地写最后一个循环(与剧情),我怎样才能创建一个电影? EDITED - >求解

+0

乔治,您实际上不必编辑您的问题以表明它们已解决。这几乎是绿色复选标记在接受答案时所指示的内容,这是确定对您的问题最有帮助的解决方案的最佳方法。 – gnovice 2011-03-30 15:30:23

回答

1

当i和j = 1时,u(j,i-1)和u(j-1,i)请记住,MATLAB矩阵索引从1开始,而不是0.

+0

你好,你是对的,我更新了。 – George 2011-03-30 12:05:58

相关问题