# sine wave plot

11 949 views (last 30 days)

Show older comments

Hi,

I am having some trouble plotting a sine wave and i'm not sure where i am going wrong.

i have

t = [0:0.1:2*pi]

a = sin(t);

plot(t,a)

this works by itself, but i want to be able to change the frequency. When i run the same code but make the change

a = sin(2*pi*60*t)

the code returns something bad. What am i doing wrong? How can i generate a sin wave with different frequencies?

##### 14 Comments

Gokul Krishna N
on 13 Oct 2021

Just been reading the comments in this question. Hats off to you, sir @Walter Roberson

### Accepted Answer

Rick Rosson
on 24 Apr 2012

Please try:

%%Time specifications:

Fs = 8000; % samples per second

dt = 1/Fs; % seconds per sample

StopTime = 0.25; % seconds

t = (0:dt:StopTime-dt)'; % seconds

%%Sine wave:

Fc = 60; % hertz

x = cos(2*pi*Fc*t);

% Plot the signal versus time:

figure;

plot(t,x);

xlabel('time (in seconds)');

title('Signal versus Time');

zoom xon;

HTH.

Rick

##### 8 Comments

MD RAJIBUL HOSSAIN RUBEL
on 28 May 2021

### More Answers (10)

Mike Mki
on 29 Nov 2016

Dear Mr. Rick, Is it possible to create knit structure in Matlab as follows:

##### 0 Comments

Robert
on 28 Nov 2017

aaa,

What goes wrong: by multiplying time vector t by 2*pi*60 your discrete step size becomes 0.1*2*pi*60=37.6991. But you need at least two samples per cycle (2*pi) to depict your sine wave. Otherwise you'll get an alias frequency, and in you special case the alias frequency is infinity as you produce a whole multiple of 2*pi as step size, thus your plot never gets its arse off (roundabout) zero.

Using Rick's code you'll be granted enough samples per period.

Best regs

Robert

##### 0 Comments

Junyoung Ahn
on 16 Jun 2020

clear;

clc;

close;

f=60; %frequency [Hz]

t=(0:1/(f*100):1);

a=1; %amplitude [V]

phi=0; %phase

y=a*sin(2*pi*f*t+phi);

plot(t,y)

xlabel('time(s)')

ylabel('amplitude(V)')

##### 0 Comments

shampa das
on 26 Dec 2020

Edited: Walter Roberson
on 31 Jan 2021

clc; t=0:0.01:1; f=1; x=sin(2*pi*f*t); figure(1); plot(t,x);

fs1=2*f; n=-1:0.1:1; y1=sin(2*pi*n*f/fs1); figure(2); stem(n,y1);

fs2=1.2*f; n=-1:0.1:1; y2=sin(2*pi*n*f/fs2); figure(3); stem(n,y2);

fs3=3*f; n=-1:0.1:1; y3=sin(2*pi*n*f/fs3); figure(4); stem(n,y3); figure (5);

subplot(2,2,1); plot(t,x); subplot(2,2,2); plot(n,y1); subplot(2,2,3); plot(n,y2); subplot(2,2,4); plot(n,y3);

##### 0 Comments

soumyendu banerjee
on 1 Nov 2019

%% if Fs= the frequency u want,

x = -pi:0.01:pi;

y=sin(Fs.*x);

plot(y)

##### 0 Comments

wilfred nwakpu
on 1 Feb 2020

%%Time specifications:

Fs = 8000; % samples per second

dt = 1/Fs; % seconds per sample

StopTime = 0.25; % seconds

t = (0:dt:StopTime-dt)'; % seconds

%%Sine wave:

Fc = 60; % hertz

x = cos(2*pi*Fc*t);

% Plot the signal versus time:

figure;

plot(t,x);

xlabel('time (in seconds)');

title('Signal versus Time');

zoom xon;

##### 0 Comments

sevde busra bayrak
on 24 Aug 2020

sampling_rate = 250;

time = 0:1/sampling_rate:2;

freq = 2;

%general formula : Amplitude*sin(2*pi*freq*time)

figure(1),clf

signal = sin(2*pi*time*freq);

plot(time,signal)

xlabel('time')

title('Sine Wave')

##### 1 Comment

Ayesha Noor
on 5 Aug 2021

help me solving this numerical.

Consider x(t) = 1.5sin(πt)[u(t) − u(t − 1)] and h(t) = 1.5[u(t) − u(t − 1.5)] − u(t − 2) + u(t − 2.5). Compute the output y(t) = x(t) ∗ h(t) using MATLAB.

Mehrab Pretum
on 22 Jun 2021

Generate an analog signal using the following equation ,

Signal = 2*sin(2*pi*20*t)+0.4*cos(2*pi*100*t)+0.1*sin(2*pi*500*t)+0.05*randn(size(t));

. Show the signal in time and frequency domain and calculate the capacity using Shannon capacity formula .

• Show the quantize signal considering 6 equally distributed levels and provide image for one cycle of the original signal and quantized signal ( using subplot ) .

##### 1 Comment

Walter Roberson
on 24 Jun 2021

First Last
on 28 Jun 2021

Edited: Walter Roberson
on 5 Aug 2021

t = [0:0.1:2*pi]

a = sin(t);

plot(t,a)

##### 2 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!