# Why am I receiving the error message "Array indices must be positive integers or logical values"?

2 views (last 30 days)
Katya Britton on 18 May 2022
Commented: Katya Britton on 18 May 2022
Hello, I am getting stuck on computing one of the components of a model that I am working on. Attempting to compute the component u_z returns the error "Array indices must be positive integers or logical values." I am stumped on what is going wrong as the other component, u_r, is being computed and returning an array with no problems. Attached below is the code and the error:
% Define parameters
R = 5000;
r_max = 45;
z_max = 4000;
ch_o = z_max / 0.22;
ch_i = ch_o / 12.5;
sf = r_max / (0.2357 * R);
r = -2000:0.1:2000;
z = 6000;
% Plot results
plot(r, Compute_u(R, ch_o, ch_i, sf, r, z))
function u = Compute_u(R, ch_o, ch_i, sf, r, z)
u_r = ((sf * R^2)./(2 .* r)) .* (1-exp(-(r./R).^2)) .* (exp(-(z/ch_o))-exp(-(z/ch_i)));
u_z = (-(sf .* exp(-(r./R).^2))) .* (ch_i(exp(-(z/ch_i))) - ch_o(exp(-(z/ch_o))-1));
u = sqrt(u_r^2 + u_z^2);
end

Jan on 18 May 2022
Edited: Jan on 18 May 2022
ch_i(exp(-(z/ch_i)))
ch_o(exp(-(z/ch_o))-1)
Here exp(-(z/ch_i)) and exp(-(z/ch_o))-1 are used as indices of the variables ch_i and ch_o. Are you sure that this is wanted? Both are scalars, so indexing is not useful at all. Maybe you want:
ch_i * (exp(-(z/ch_i)))
ch_o * (exp(-(z/ch_o))-1)
By the way, you can write this leaner as:
ch_i * exp(-z/ch_i)
ch_o * exp(-z/ch_o) - 1
Katya Britton on 18 May 2022
It's working now! Thank you so much for your help.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by