Sine function produces unexpected results
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Joachim Wagner
le 5 Mai 2019
Modifié(e) : Joachim Wagner
le 5 Mai 2019
Good morning community,
I've got some troubles using the MATLAB sine function ( sin() ) and I hope somebody can help me to solve this wrong line of thought :)
However, let me describe the issue I'm troubling with. I'm trying to give the sine function a time vector and a frequency to calculate the amplitude as follow:
omega = pi; % define frequency
t = 0:.1:1; % define time vec
A = sin(1i*omega.*t)
The result I get from this operation is the following:
The strange thing and what I really don't understand is, why I do get the correct result, if I'm not using a variable for t but typing the vector definition directly in the function call command as you can see bellow:
Even if I leave off the complex number in the first case, I'm not getting the result I expect, however this shouldn't change anything.
So, I hope there is somebody, who has an idea why it behaves like this. I would be very thankful.
Best regards.
0 commentaires
Réponse acceptée
Stephan
le 5 Mai 2019
Hi,
note:
>> t = 0:0.1:1
t =
Columns 1 through 8
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000
Columns 9 through 11
0.8000 0.9000 1.0000
>> pi .* t
ans =
Columns 1 through 8
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 2.1991
Columns 9 through 11
2.5133 2.8274 3.1416
But now:
>> pi .* 0:0.1:1
ans =
Columns 1 through 8
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000
Columns 9 through 11
0.8000 0.9000 1.0000
Using brackets makes things going to be correct:
>> pi .* (0:0.1:1)
ans =
Columns 1 through 8
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 2.1991
Columns 9 through 11
2.5133 2.8274 3.1416
0 commentaires
Plus de réponses (2)
Joachim Wagner
le 5 Mai 2019
2 commentaires
Star Strider
le 5 Mai 2019
‘The strange thing and what I really don't understand is, why I do get the correct result, if I'm not using a variable for t but typing the vector definition directly in the function call command ...’
Not really strange. It relates to the way MATLAB defined the two vectors in:
omega = pi; % define frequency
t = 0:.1:1; % define time vec
A = sin(1i*omega.*t)
and:
A = sin(1i*omega*0:.1:1)
In the first one, the imaginary operator 1i multiplies the entire vector ‘t’. In the second one, 1i multiplies only the first element of the vector, here 0 (so the first element remains 0 and is not complex) and none of the others.
The second is equivalent to the first if you put parentheses around the vector:
A = sin(1i*omega*(0:.1:1))
John D'Errico
le 5 Mai 2019
It is an order of operators thing. The colon operator is quite low on the totem pole, so other operations are done first.
That means while you might expect that this operation
1 + 0:5
will add 1 to each element of the vector 0:5, in fact, it adds 1 to 0, then uses that as the first operand of colon. So we see this:
1 + 0:5
ans =
1 2 3 4 5
The same applies to what you did.
Voir également
Catégories
En savoir plus sur Special Values 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!