Array indices must be positive integers or logical values. error for simple calculation, nothing array related

4 ビュー (過去 30 日間)
Hello, forgive me for this probably dumb question. I am super new to matlab and at the moment I am just trying to use Matlab for tedious calculations. Can you guys help figure out why this error shows up when I am just doing simple calculations? Thank you very much.
My code:
v1 = (4*5*10^-4)/(pi*(0.01)^2);
v2 = (4*5*10^-4)/(pi*(0.02)^2);
g = 9.81;
hb = 2;
deltaH = 1/g(0.5*v1^2-g*hb-0.5*v2^2) - 2.01;
Array indices must be positive integers or logical values.

回答 (1 件)

Umang Pandey
Umang Pandey 2024 年 1 月 24 日
編集済み: Umang Pandey 2024 年 1 月 24 日
Hi William,
The error is because while you are trying to divide the "deltaH" expression by "g", it is instead getting treated as an array instead.
You can reformulate it in the following manner -
v1 = (4*5*10^-4)/(pi*(0.01)^2);
v2 = (4*5*10^-4)/(pi*(0.02)^2);
g = 9.81;
hb = 2;
deltaH = (0.5*v1^2 - g*hb - 0.5*v2^2)/g - 2.01;
Hope this helps!
Best,
Umang
  3 件のコメント
Umang Pandey
Umang Pandey 2024 年 1 月 24 日
Hi Stephen, thanks for the correction! I made a typo I meant to say an array!
William
William 2024 年 1 月 24 日
Thank you guys, that makes sense now. Really appreciate it.

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

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by