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

1 view (last 30 days)
%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 Comments
Adam Danz
Adam Danz on 24 Sep 2020
Edited: Adam Danz on 24 Sep 2020
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

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 24 Sep 2020
Edited: Adam Danz on 24 Sep 2020
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.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!