I am trying to convert this equation 4*L/(2*i-1) into R
I failed to undersatnd why the output is two negative values in matlab
but R'output is a single value
L= 17;
i=4;
(2*i-1) gives -1 + 2; Is it i here is a complex number?

3 件のコメント

Walter Roberson
Walter Roberson 2023 年 11 月 22 日
format long g
L = 17;
4*L/(2*i-1)
ans =
-13.6 - 27.2i
Notice the second number has the i after it. That i indices a complex component.
4*L/(2*sqrt(-1)-1)
ans =
-13.6 - 27.2i
Torsten
Torsten 2023 年 11 月 22 日
編集済み: Torsten 2023 年 11 月 22 日
If you use the expression 4*L/(2*i-1) in R, I guess that "i" was set to a value somewhere before in the R-code. If you set "i" to the same value in your MATLB code, MATLAB will compute the same value for the expression as R does. If you don't set "i" to a value before you compute the expression, MATLAB will interprete "i" as the imaginary unit.
L = 17;
i = 4;
4*L/(2*i-1)
ans = 9.7143
clear i
4*L/(2*i-1)
ans = -13.6000 -27.2000i
Alexander
Alexander 2023 年 11 月 22 日
Never use i or j if you are not dealing with complex numbers. Use ii or jj instead if you have to.

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

 採用された回答

ScottB
ScottB 2023 年 11 月 22 日

1 投票

Yes, Matlab sees "i" as complex. Maybe pick another variable if you are just using scalars.

その他の回答 (0 件)

カテゴリ

製品

リリース

R2022a

質問済み:

BA
2023 年 11 月 22 日

コメント済み:

2023 年 11 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by