why there is array error and how to solve it?
"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
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!