Sine function produces unexpected results

조회 수: 1 (최근 30일)
Joachim Wagner
Joachim Wagner 2019년 5월 5일
편집: Joachim Wagner 2019년 5월 5일
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.

채택된 답변

Stephan
Stephan 2019년 5월 5일
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

추가 답변 (2개)

Joachim Wagner
Joachim Wagner 2019년 5월 5일
All right, that's true. Thank you very much, that fits it for the moment but can maybe still someone explain, why I get those complex numbers in the first case? I don't really get it.
  댓글 수: 2
Star Strider
Star Strider 2019년 5월 5일
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
John D'Errico 2019년 5월 5일
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.

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


Joachim Wagner
Joachim Wagner 2019년 5월 5일
편집: Joachim Wagner 2019년 5월 5일
Right perfect, it now becomes clear. Thanks a lot to all of you!

태그

Community Treasure Hunt

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

Start Hunting!

Translated by