simple matlab code displays different graph

1. simple matlab code displays different graph

Hi,

I don't understand why the following simple sine wave code displays different waveform when simulated repeatedly?

fc=10;
fs=2*fc;
ts=1/fs;
t=0:ts:50;
x=sin(2*pi*fc*t);
plot(t,x);

the first run showed a correct sine wave, when i rerunned the code I get the following waveform-

sinewave.bmp

thanks

•

2. Re: simple matlab code displays different graph

See the amplitude its 10^-13 which you can assume to zero.

Its negligible.
What do you mean by rerun.

- - - Updated - - -

Have a look at this link:-

Code:
```% This Script file Plots the Sine Wave with respect to time

clc; %Used to clear the screen (Just Clears the Screen)

clear; %Used to clear the Variable Window (This clears the Memory)

fSampling = 1000; %Sampling Frequency

t = 0:1/fSampling:1; %Time Interval

f = 3; %Carrier Frequency

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

plot(t,sine_wave); %For Plotting the Sine Wave with Respect to time

xlabel('Time Axis');

ylabel('Sine Wave Amplitude');

title('Sine Wave Plot');

grid on;```
Try re-running this code.

•

3. Re: simple matlab code displays different graph

hi xpress, thanks but my code is fine, when the sampling frequency is increased the waveform is fine...

I don't understand why the figure is like the one attached when I set the sampling frequency fs=2*fc that is twice the carrier???

at fs=2fc the signal waveform is like an exponential increasing sinusoid.

•

4. Re: simple matlab code displays different graph

Since fs=2*fc in this case sampling timing (ts) occurs on time 2*pi*fc*(1/2fc), 2*pi*fc*(2/2fc), 2*pi*fc*(3/2fc) etc. at this moment ( pi, 2pi, 3pi,...) sin funciton is zero.
So you see graph around zero. You need to increase the sampling freq.

Another words, in this case you always sample sin wave at zero crossing points, so sampled values are zero.
PS: But i don't understand why it is plotted correctly at the first run.

5. Re: simple matlab code displays different graph

Sampling Frequency >= 2* Signal Frequency

Nyquist Criteria, so in order to reconstruct your signal, Sampling frequency must be 2 times greater than the Signal Frequency.
The greater Sampling Frequency the better signal you will make but will also increase the bandwidth

--[[ ]]--