# Sine wave plot using Matlab

I am using the following script to plot a sine wave in Matlab:

clear all;
close all;
clc;

f = input('Enter frequency of the signal ');

t = 0:0.1:100;

x = sin(2*3.141516*f*t);
plot(t,x);


And when I input f = 1000Hz, this is the result:

But I think there is something really strange going on, why is the sine function going downwards from 0 to 10? Shouldn't it go upwards? What am I missing here?

Also, why can't I try to evaluate this function at t = 10 seconds? (This gives a 0 result, and we should get a -1 according to the plot).

• It depends on $t$ whether or not you increase or decrease at $0$. Commented Jan 29, 2018 at 21:48
• The value of the function at t = 10 seconds is -0.993 $\approx$ -1. This is 101th element in the array named 'x'. You can use the command 'x(t == 10)' to see. Commented Jan 29, 2018 at 21:50
• Well, one thing to notice is that if $x$ is the way you've defined it, then it's $$x(t) = \sin(2000 \pi t)$$ which has a period of $1/1000$. So it completes $100,000$ oscillations over the window you're looking at.
– user296602
Commented Jan 29, 2018 at 21:54
• @Dmitry in the answer is right, we sample so seldom on the curve that we miss many periods for each new sample. This introduces aliasing distortion which make higher frequencies look like low frequencies. You will read more about it when you study signals and systems or something like it. Commented Jan 30, 2018 at 23:00

## 1 Answer

Well, this is quite interesting. You evaluate $\sin()$ at points $0,200\pi,400\pi$ and so on. So actually you should get just a straight zero line. This would be the case if you wrote

x = sin(2*pi*f*t);


You shall see that the plot would oscillate around zero with a little noise due to the rounding.

This is the first issue: you've taken the wrong sampling interval. According to the Shannon theorem, the sampling time must be at least $\frac{1}{2f}$, which is much less than your sampling time ($0.0005$ vs. $0.1$).

The next point is that you've written $3.141516$ instead of $\pi$ which amounts to having the error equal to $\epsilon\approx-7.6654\cdot 10^{-5}$. Hence, you evaluate the sine at points $0, - 200\epsilon, - 400\epsilon,\dots$. This explains the wrong direction and the low frequency of the oscillations.

• Yep the Shannon sampling theorem violated by a large margin. Commented Jan 30, 2018 at 22:59
• Oh, how didn't I see that? Thank you very much for the detailed answer!! Commented Jan 31, 2018 at 12:07