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 ;)

采纳的回答

Jan
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
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)
y1 = 1.0000 + 1.0000i
y2 = (1+x)^2.5
y2 = 0
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
y3 = 2×2
1.0000 5.6569 0 1.0000

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by