Complex Numbers where thy are not supposed to be
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Adrian Alexander
le 8 Avr 2021
Commenté : Adrian Alexander
le 12 Avr 2021
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 commentaires
Réponse acceptée
Jan
le 8 Avr 2021
Modifié(e) : Jan
le 8 Avr 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 commentaires
Steven Lord
le 8 Avr 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)
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!