Index exceeds the number of array elements (252).

4 ビュー (過去 30 日間)
ummaab66
ummaab66 2020 年 11 月 6 日
回答済み: Gautam 2025 年 7 月 1 日
% Explicit Quickest scheme
clc; clear all;
xmin = 0; % [m]
xmax = 50*1000; % [m]
dx = 200; % [m]
tmax = 0.5*24*3600; % [s]
u = 0.45; % [m/s]
D=10; %[m2/s]
k=0.1/(24*3600); %[1/s]
dt = 50; % [s]
No = (xmax - xmin)/dx;
x = xmin : dx : xmax + dx;
%% initial and Boundary conditions
for i = 1:length(x)
if x(i)<=0
Co(i) = 100;
else
Co(i) = 0;
end
end
C = Co;
Cp = Co;
% Analytical solution
ns = tmax/dt;
for n = 1 : ns
for i = 1 : No+2
if x(i)<=u*tmax
d(i) = 100;
else
d(i) = 0;
end
end
end
% Quickest
ns = tmax/dt;
for n = 1 : ns
for i = 3 : No+2
F=D*dt/(dx)^2;
g=u*dt/dx;
gr= (C(i+1)-C(i))/dx;
gl=(C(i)-C(i-1))/dx;
cr=(C(i+1)-2*C(i)+C(i-1))/dx^2;
cl=(C(i-2)-2*C(i-1)+C(i))/dx^2;
Cp(i) = C(i)-g*((0.5*(C(i)+C(i+1))-0.5*dx*g*gr-0.166*dx^2*(1-g^2-3*F)*cr)-(0.5*(C(i-1)+C(i))-0.5*dx*g*gl-0.166*dx^2*(1-g^2-3*F)*cl))+F*((dx*gr-0.5*dx^2*g*cr)-(dx*gl-0.5*dx^2*g*cl))-k*C(i)*dt;
end
C = Cp;
end
  1 件のコメント
AC_GBX
AC_GBX 2020 年 11 月 6 日
Hey ummabb66,
please use the code insert function to display your code, so it can be understood. What is you exact problem?

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

回答 (1 件)

Gautam
Gautam 2025 年 7 月 1 日
Hello, ummaab66
The error id in the line with the expression
gr= (C(i+1)-C(i))/dx;
The array C has 252 elements and i goes from 3 to 252. So, when you try to access the element C(i+1) when i is 252, MATLAB is not able to find the element and throws the error.

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by