Clear Filters
Clear Filters

Index in position 1 exceeds array bounds (must not exceed 1) AT d_theta_plus = (theta(j+1, n) - theta(j, n)) / delta_z;

2 views (last 30 days)
D = 1.51 * (10^(-6));
tou = 2.0 / 3.0;
k_y = 0.37;
k_a = 0.85;
v_w = 0.34;
v_x = 0.66;
L = 100;
N_z = 100;
T = 2400;
N_t = 40;
delta_z = L / N_z;
delta_t = T / N_t;
rho_s = 1;
theta = 0.1;
M_m_a = 1100; % Assuming M_m_a is a constant
% Create arrays for M and theta, assuming they have appropriate initial values
M = zeros(N_z, N_t);
theta_j = zeros(N_z, N_t);
for n = 1:N_t
for j = 1:(N_z-1) % Start from 2 to N_z-1 to avoid exceeding array bounds
% Finite difference approximations for M and theta
d_theta_plus = (theta(j+1, n) - theta(j, n)) / delta_z;
d_theta_minus = (theta(j, n) - theta(j-1, n)) / delta_z;
d_M_plus = (M(j+1, n) - M(j, n)) / delta_z;
d_M_minus = (M(j, n) - M(j-1, n)) / delta_z;
% Update equation for M
M(j, n+1) = (1 / theta(j, n+1)) * ((theta(j, n) * M(j, n)) + (delta_t / delta_z) * ...
((-v_w - v_x) * ((theta(j+1, n) - theta(j-1, n)) / (2 * delta_z) * ...
((M(j+1, n) - M(j-1, n)) / (2 * delta_z))) + ...
(tou * D) * (d_theta_plus * d_M_plus - d_theta_minus * d_M_minus)) + ...
(delta_t * (k_y * rho_s * M(j, n+1)) - (k_a * (1 - (M(j, n+1) / M_m_a)) * theta(j, n) * M(j, n))));
end
end
Index in position 1 exceeds array bounds. Index must not exceed 1.

Answers (2)

Walter Roberson
Walter Roberson on 29 Aug 2023
You initialize theta as a scalar. You have for j=1 to something. You access theta(j+1,something) but when j is 1 that is theta(2,something) which does not exist since theta is scalar

Shubham
Shubham on 29 Aug 2023
Edited: Shubham on 29 Aug 2023
Hi Shwetha,
Based on your code, it seems that there are two problems in the program:
1. Accessing `theta(j+1,n)` instead of `theta_j(j+1,n)`: It appears that you intended to access the variable `theta_j` instead of the general variable `theta`. This could be the reason for the error you encountered. Make sure to update the code to access the correct variable.
2. Out-of-bounds access in the equation to update `M`: The equation `M(j,n+1)` tries to access an element that goes out of bounds when `n` reaches `N_t`. This will result in an error. Please ensure that the loop or condition controlling the value of `n` is within the valid range to avoid this issue.
Additionally, it is mentioned in the code comment that `j` starts from 2, but it seems that it starts from 1 in the code. Please verify and adjust the code accordingly.
I hope this clarifies the issues in the code.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!