Complex Numbers where thy are not supposed to be

조회 수: 3 (최근 30일)
Adrian Alexander
Adrian Alexander 2021년 4월 8일
댓글: Adrian Alexander 2021년 4월 12일
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
Adrian Alexander
Adrian Alexander 2021년 4월 12일
thank u i got it and made it work ^^

댓글을 달려면 로그인하십시오.

추가 답변 (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