Why does the command window say "Array indices must be positive integers or logical values" when this code is executed?

p = 280000; % Principal loan amount
r = 0.0475; % Interest rate as decimal
y = [10, 20, 30]; % Number of years for loan
% Calculations
m = (p(r/12)) / (1-(1+(r/12)^(-12*y(1)))); % Monthly cost
Array indices must be positive integers or logical values.
I tried multiple values but it keeps giving me the same result. The numbers are positive and the index is not incorrect.
fprintf('For a loan duration of %g years and a principal amount of %.2f at an interest rate of %.4f%%, the monthly payment will be %.2f', y(1), p, r, m)

 採用された回答

Torsten
Torsten 2023 年 9 月 8 日
移動済み: dpb 2023 年 9 月 8 日
Maybe you mean
(p*(r/12))
instead of
(p(r/12))
?

1 件のコメント

Angelica
Angelica 2023 年 9 月 8 日
移動済み: dpb 2023 年 9 月 8 日
Yes, that fixed it. Thank you. Sorry for the silly mistake.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2023a

タグ

質問済み:

2023 年 9 月 8 日

移動済み:

dpb
2023 年 9 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by