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

조회 수: 1 (최근 30일)
Katya Britton 2022년 5월 18일
댓글: Katya Britton 2022년 5월 18일
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 2022년 5월 18일
편집: Jan 2022년 5월 18일
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
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Katya Britton 2022년 5월 18일
It's working now! Thank you so much for your help.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by