Error using for-loop, keep getting "Index in position 1 is invalid. Array indices must be positive integers or logical values."

Hi everyone,
I am currently writing code that will loop through twice and call a Psat(T,A,B,C) function to calculate Psat(i) (twice for each temperature.) Here is my code:
%% Using single-temperature to plot simple P-XY diagram
%% @ T = 273.15
% Propane, Isobutane
ConstantA = [3.98292 4.3281];
ConstantB = [819.296 1132.108];
ConstantC = [-24.417 0.918];
T = 273.15;
for i=1:2
Psat(i) = Psat(T, ConstantA(i), ConstantB(i), ConstantC(i));
end
and the Function Psat:
function Psat = Psat(temp, a, b, c)
Psat = 10^(a - b/(temp+c)); % Antoine's Eqn
Psat = Psat*0.133322;
end
I don't understand why I keep getting index errors. The constant matrix are vectors of size [1,2] and the numbers I'm incrementing over are whole or (+) (AKA 1-2). Even when I was attempting to de-bug, I would remove Psat(i) all together and just loop through for ConstantA(i) and still get the same indexing error?
Thank you!

回答(1 个)

The variable and the function have the same names.
Change the function name and it works —
%% Using single-temperature to plot simple P-XY diagram
%% @ T = 273.15
% Propane, Isobutane
ConstantA = [3.98292 4.3281];
ConstantB = [819.296 1132.108];
ConstantC = [-24.417 0.918];
T = 273.15;
for i=1:2
Psat(i) = Psatfcn(T, ConstantA(i), ConstantB(i), ConstantC(i));
end
Psat
Psat = 1×2
0.6515 0.2100
% and the Function Psat:
function Psat = Psatfcn(temp, a, b, c)
Psat = 10^(a - b/(temp+c)); % Antoine's Eqn
Psat = Psat*0.133322;
end
.

类别

在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

标签

提问:

2023-10-14

评论:

2023-10-15

Community Treasure Hunt

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

Start Hunting!

Translated by