Sine function produces unexpected results
古いコメントを表示
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.
採用された回答
その他の回答 (2 件)
Joachim Wagner
2019 年 5 月 5 日
0 投票
2 件のコメント
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
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
2019 年 5 月 5 日
編集済み: Joachim Wagner
2019 年 5 月 5 日
0 投票
カテゴリ
ヘルプ センター および File Exchange で Special Values についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!