"Array indices must be positive integers or logical values."

1 次查看(过去 30 天)
%define parameter
u= 1; %Liq velocity
D= 1; %Diffusion Coef
%domain and step
Lx= 81450; %length pipeline (m)
nx= 50; %num step in space(x)
nt= 100; %num time step
dx= Lx/(nx-1); %width space step
%satisfy CFL condition (ensure stability)
c=1; %speed
C=0.1; %Courant number (CFL condition <1)
dt= C*dx/c; %width each time step
%field variable
A = zeros(1,nx); %H2S concentration
x= linspace (0,Lx, nx); %distance
%initial condition
A(i)= 1; %conc. at initial
t=0; %at time=0
%loop
for n=1:nt
Ac= A; %save concentration into Ac for later used
t=t+dt; %new time
%new concentration
for i=2:nx-1
A(i)= Ac(i) + ((dt* D)/(dx*dx))* ((Ac(i+1)- 2*Ac(i)+ Ac(i-1)));
end
%boundary condition
A(1)=0; A(end)=0; %Dirichlet
%visualize
plot(x,A); set(gca,'ylis', [0 100]);
xlabel('Distance in pipeline'); ylabel('H2S concentration');
title(sprintf ('H2S diffusion'));
pause(0.01);
end
return;
  4 个评论
Adam Danz
Adam Danz 2020-9-24
编辑:Adam Danz 2020-9-24
It's not that Matlab doesn't know what i is, assuming it's not defined by the user. It's that i takes on the value of the imaginary unit when not otherwise defined.
>> clear
>> i
ans =
0 + 1i

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-9-24
编辑:Adam Danz 2020-9-24
If this is the complete script, the error is caused in this line where i is not defined and therefore takes the default value of the imaginary unit which is not a positive integer nor a logical value.
A(i)= 1; %conc. at initial
If this is not where the error is generated, provide the entire error message and indicate which line causes the error.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by