MATLAB Answers

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

2 views (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

Show 1 older comment
David Hill
David Hill on 24 Sep 2020
Your problem is you have not defined the variable "i" when you call "A(i) = 1;". So matlab doesn't know what index in A you want set to 1.
Adam Danz
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.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!