Asked by Joachim Wagner
on 5 May 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.

Answer by Stephan
on 5 May 2019

Accepted Answer

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

Sign in to comment.

Answer by Joachim Wagner
on 5 May 2019

Star Strider
on 5 May 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
on 5 May 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.

Sign in to comment.

Answer by Joachim Wagner
on 5 May 2019

Edited by Joachim Wagner
on 5 May 2019

Right perfect, it now becomes clear. Thanks a lot to all of you!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.