Clear Filters
Clear Filters

Complex Numbers where thy are not supposed to be

6 views (last 30 days)
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 ;)

Accepted Answer

Jan
Jan on 8 Apr 2021
Edited: Jan on 8 Apr 2021
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 Comments
Steven Lord
Steven Lord on 8 Apr 2021
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

Sign in to comment.

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!