i got this error "Index exceeds the number of array elements (1)." at the first line starting " U(j, n + 1, dt_index)" when the first loop completed for one time
2 views (last 30 days)
Show older comments
Abdelrahman Adel
on 5 Dec 2023
Answered: Walter Roberson
on 6 Dec 2023
i am trying to solve coutte flow problem using dufort-frankel Finite difference method and i got this error "Index exceeds the number of array elements (1)." at the first line starting " U(j, n + 1, dt_index)" when the first loop completed for one time .
why it's happen and how to solve ?
Defining Variables
clc,clear;
v = 0.000217; % Kinematic Viscosity
h = 0.04; dy = 0.001; % Y Dimension
Ny = length(linspace(h,0,round(h/dy))) ;
% Ny = 5 ;
dt_values = [0.002, 0.003]; % Time Dimension
Nt_values = [541, 361];
% Initial Condition
ic = 0;
% Boundary Conditions
BC_Top = 0;
BC_Bottom = 40;
for dt_index = 1:length(dt_values)
dt = dt_values(dt_index);
Nt = Nt_values(dt_index);
% Diffusion Number
d = (v * dt) / (dy^2);
% Defining Domain
U = zeros(Ny + 1, Nt, length(dt_values));
% Initial Condition
U(:, 1, :) = ic;
% Boundary Conditions
U(1, :, :) = BC_Top;
U(end, :, :) = BC_Bottom;
% Solution
for k = 1:length(dt_values)
for n = 1:Nt - 1
if n == 1
for j = 2:Ny
U(j, n + 1, dt_index) = U(j, n, dt_index) + d(dt_index) * (U(j + 1, n, dt_index) - 2 * U(j, n, dt_index) + U(j - 1, n, dt_index));
end
else
for j = 2:Ny
U(j, n + 1, dt_index) = (U(j, n - 1, dt_index) + 2 * d(dt_index) * (U(j + 1, n, dt_index) - U(j, n - 1, dt_index) + U(j - 1, n, dt_index))) / (1 + 2 * d(dt_index));
end
end
end
end
end
0 Comments
Accepted Answer
Walter Roberson
on 6 Dec 2023
for dt_index = 1:length(dt_values)
dt = dt_values(dt_index);
Nt = Nt_values(dt_index);
You extract particular scalars from dt_values and Nt_values, getting scalar dt and Nt
d = (v * dt) / (dy^2);
and use them to calculate scalar d
U(j, n + 1, dt_index) = U(j, n, dt_index) + d(dt_index) * (U(j + 1, n, dt_index) - 2 * U(j, n, dt_index) + U(j - 1, n, dt_index));
and
U(j, n + 1, dt_index) = (U(j, n - 1, dt_index) + 2 * d(dt_index) * (U(j + 1, n, dt_index) - U(j, n - 1, dt_index) + U(j - 1, n, dt_index))) / (1 + 2 * d(dt_index));
in both of those statements you have d(dt_index) -- but d is scalar and so should not be indexed at d_index
0 Comments
More Answers (0)
See Also
Categories
Find more on Boundary Conditions 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!