Complex Numbers where thy are not supposed to be
4 ビュー (過去 30 日間)
古いコメントを表示
Hi I had this Problem a number of times now and I dont get it.
I get Complex Numbers as results, however i need normal Numbers.
I simplified the code. its a sinus going through an dsp algorithm for an audio plugin.
The negative Halfwave of the Sinus causes the output to become Complex.
1-1^2.5 = 0
but when i run the code i get
1-1^2.5= 1.0000 + 1.0000i
How can i tell matlab that no Complex Calculus is needed ?
x=[0 ; 1 ; -1 ; 0]
N = length(x);
y=zeros(N,1)
for n=1:N
y(n,1) = (1+x(n,1)^2.5)
end
%thank u in advance ;)
0 件のコメント
採用された回答
Jan
2021 年 4 月 8 日
編集済み: Jan
2021 年 4 月 8 日
1 - 1^2.5 == 0 % Let's ignore the rounding errors...
This is correct. But you calculate something else:
x = -1;
1 + x^2.5
This is:
1 + (-1)^2.5
which is 1 + 1i. The parentheses matter. Or if you store the -1 in a variable, the code is equivalent to:
1 + (x)^2.5
"How can i tell matlab that no Complex Calculus is needed?"
You can't. The result of 1+(x)^2.5 with a negative x is complex and you cannot convert this to 0 - except with catching these values explicitly:
y = zeros(N, 1);
for n = 1:N
if x(n) >= 0
y(n) = 1 + x(n)^2.5;
end
end
2 件のコメント
Steven Lord
2021 年 4 月 8 日
Another possibility is that the poster intended to raise the expression to the 2.5 power not just the variable.
x = -1;
y1 = (1+x^2.5)
y2 = (1+x)^2.5
Of course in this case there's no need for a loop with the example as written if you use the elementwise power operator .^ instead of the matrix power operator ^ as below.
x = [0 1; -1 0];
y3 = (1+x).^2.5
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Fourier Analysis and Filtering についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!