0
\$\begingroup\$

I am working with matlab in college and I need to generate sine wave whcih represents signal going through a wire.

But its not clear to me what t equals to and how can I multiply \$y\$ by \$t\$

this is the formula

$$y(t) = 1.2\sin(35.000\pi t+2.15\text{ rads})$$

This is my Matlab code

time = 1:1.0:10;
radstodegrees = 2.15*180/pi
y = 1.2*sin(35.000*pi*time*radstodegrees);
y = y*time
plot(y)

It gives me error when I want to multiply y*time

Anyone know where to go from here?

\$\endgroup\$
  • 2
    \$\begingroup\$ You get the error because you are doing an array multiply. That whole command is unnecessary and wrong in any case. "t" is time. Your command should be 1.2*sin(35 * pi * time + 2.15) \$\endgroup\$ – Scott Seidman Nov 19 '14 at 18:20
  • \$\begingroup\$ In Matlab, trig function use radians as inputs -- sin(pi/2)=1. You don't need to (or want to) convert radians to degrees \$\endgroup\$ – Scott Seidman Nov 19 '14 at 18:28
  • \$\begingroup\$ why are you doing y=y*time? \$\endgroup\$ – cjferes Nov 19 '14 at 18:29
  • 2
    \$\begingroup\$ You need finer timesteps if you want to be able to see the 'real' sine waveform. Otherwise you get aliasing. \$\endgroup\$ – mng Nov 19 '14 at 18:42
  • \$\begingroup\$ what time should equal to see the real sine waveform? and to avoid that aliasing? \$\endgroup\$ – corkalom Nov 19 '14 at 18:45
3
\$\begingroup\$
>> time= 1:1:10;
>> y= 1.2*sin(35*pi*time+2.15);
>> plot(time, y);

enter image description here

\$\endgroup\$
  • 4
    \$\begingroup\$ For OP's benefit it's worth noticing that this plot is severely aliased. \$\endgroup\$ – The Photon Nov 19 '14 at 18:58
1
\$\begingroup\$

More reasonably

time = 0:0.00001:1.0;

y= 1.2*sin(35*pi*time+2.15);

plot(time, y)

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Most certainly -- but I'd suggest that your sample rate is overkill -- huge arrays, and it would make any filtering in the bandwidth of interest be fairly difficult. \$\endgroup\$ – Scott Seidman Nov 20 '14 at 0:03
1
\$\begingroup\$

You have many replies as to what is the correct matlab code (see mine below) - note: with a frequency of 17.5Hz, you need a timestep of at least 10-100 time this to reproduce is clearly (

t = 0:0.001:10;
y = 1.2 * sin(35.000*pi*t + 2.15);
plot(t,y)

enter image description here

to add some insight into the the matlab error that you would have experienced at this step

y = y*time

Error using  * 
Incorrect dimensions for matrix multiplication. Check that the number of columns in
the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'. 

for starters there is no need todo time*y as y is already a function of time. The error however is due to time being a 1x10 matrix and y is a 1x10 matrix.

By default "*" will attempt to perform matrix multiplication when both operands are matrix's. You cannot perform matrix multiplication on [1 x 10] by [1 x 10]. You can on [1 x 10][10 x 1] but that is nonsensical for this.

IF you did want to multiple two arrays together and they are [1 x n] then you can use the scalar operator

y.*time

which would produce a [1 x 10] matrix which is the result of an element by element multiplication.

HOWEVER, this isn't what you want so please review the multiple correct implementations shown here. You also missed the phase shift component (2.15) in your original question. NOTE: trig functions by default operate in radians

\$\endgroup\$
0
\$\begingroup\$

An easier way to do this is to use the fplot function. The code would look something like this:

syms time;
y = 1.2*sin(35*pi*time+2.15);
fplot(y, [0 10])

This is what you end up getting for the waveform using this process.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.