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!

Answers (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
.

Categories

Products

Asked:

on 14 Oct 2023

Commented:

on 15 Oct 2023

Community Treasure Hunt

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

Start Hunting!