Why calling the functions works fine in the first run, but causes error after that?

1 回表示 (過去 30 日間)
Anup
Anup 2023 年 2 月 22 日
回答済み: Steven Lord 2023 年 2 月 22 日
I have a function as below:
function p1 = p1(d,i,N)
a = 1./(d-i);
b = ((1+i)./(1+d)).^N;
p1 = a.*(1-b);
end
I called this function in a code like this:
d = 0.05;
i = 0.04;
N = 10;
p1 = p1(d,i,N);
The code runs fine in the first run, but causes error every other time.
The error is as:
Index in position 1 is invalid. Array indices must be positive integers or logical
values.
Error in test2 (line 5)
p1 = p1(d,i,N);

採用された回答

Steven Lord
Steven Lord 2023 年 2 月 22 日
Don't assign the output from the function p1 to a variable named p1. If you do you will not be able to call the function while the variable exists; attempts to do so will be treated as an attempt to index into the variable. Choose a different name for the output.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by