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

1 回表示 (過去 30 日間)
nurhamizah husna
nurhamizah husna 2020 年 9 月 24 日
コメント済み: nurhamizah husna 2020 年 9 月 24 日
%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
nurhamizah husna
nurhamizah husna 2020 年 9 月 24 日
Thank you for the explanation !

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by