nrb
nrb

Reputation: 5

MATLAB sine wave plot is not correct

I am new to MATLAB and I wrote some code to generate a sine wave. However the graph is not correct. Here is the screenshot of my code and the plot

MATLAB

What is the problem? Please help!

Upvotes: 0

Views: 2603

Answers (1)

rayryeng
rayryeng

Reputation: 104555

MATLAB plots discrete points and simply draws a straight line to connect neighbouring points together. Your time points are one second (1s) in between, and you are specifying a frequency of 100 Hz. In addition, because your sampling time is a multiple of the period of your sine wave, substituting all of those values of t would thus make the sin result equal to 0, though there is some numerical imprecision. Specifically, if you look at the y-axis, you'll see that the magnitude of your numbers is around 10^{-13}. However even if you escape this, the sampling time is TOO LARGE for the specified frequency of your wave and so this huge gap in between points is visualized as that jagged wave that you see in your graph.

The solution is to simply make your sampling time smaller. Try something small, like 1e-4 for example:

t = 0:1e-4:0.05;
f = 100;
A = 2;
x = A*sin(2*pi*f*t);
plot(t,x);

We get this now:

enter image description here

Upvotes: 5

Related Questions