# sine wave plot

5,476 次查看（过去 30 天）
aaa 2012-4-24

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 个评论显示 13更早的评论隐藏 13更早的评论
Gokul Krishna N 2021-10-13
Just been reading the comments in this question. Hats off to you, sir @Walter Roberson

### 采纳的回答

Rick Rosson 2012-4-24
%%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
##### 10 个评论显示 9更早的评论隐藏 9更早的评论
Walter Roberson 2022-11-21
I do not find any questions posted by you ?

### 更多回答（9 个）

Mike Mki 2016-11-29
Dear Mr. Rick, Is it possible to create knit structure in Matlab as follows:
##### 1 个评论显示 无隐藏 无
DGM 2022-11-27

Junyoung Ahn 2020-6-16
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)')
##### 2 个评论显示 1更早的评论隐藏 1更早的评论
Walter Roberson 2023-1-8
I think the intent was to give 100 samples per cycle.

Robert 2017-11-28
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 个评论显示 -1更早的评论隐藏 -1更早的评论

shampa das 2020-12-26

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 个评论显示 -1更早的评论隐藏 -1更早的评论

soumyendu banerjee 2019-11-1
%% if Fs= the frequency u want,
x = -pi:0.01:pi;
y=sin(Fs.*x);
plot(y)
##### 0 个评论显示 -1更早的评论隐藏 -1更早的评论

wilfred nwakpu 2020-2-1
%%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 个评论显示 -1更早的评论隐藏 -1更早的评论

sevde busra bayrak 2020-8-24
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')
##### 0 个评论显示 -1更早的评论隐藏 -1更早的评论

Ana Maria 2023-3-15
Implement a function to generate a column vector containing a sine wave, sin(2πf(t)t), with a growing frequency, f(t) from f(0) = f1 to f(T) = f2. The inputs of the function are the duration, T in seconds, the frequencies, f1 and f2, in Hz and the sampling rate, fs, in samples per second x = chirpT one(T, f1, f2, fs)
##### 2 个评论显示 1更早的评论隐藏 1更早的评论
DGM 2023-3-15

You're copying and pasting an assignment text. This is not an answer, so it doesn't belong here as an answer. I'm compelled to keep things where they belong and remove them when they don't.
This is ultimately your task to perform. The information already present on this page is largely sufficient to complete it. I'm sure with enough effort, you can find even more specific examples elsewhere on the forum.
If you want to ask a question, please open a new question using the 'ask' button at the top of the page. If and when you do, ask an actual question, but also be prepared to prove that you've exhausted what due diligence provides.
EDIT:
To prove the point, I'm just going to grab the answer directly above and make one simple change. Other than changing the specific parameters (a matter of choice), the only real change is that instead of being a scalar, freq is a vector generated from two specified values.
% these are parameters
samplerate = 500;
duration = 2;
flim = [0 8];
% both these vectors have the same size
time = 0:1/samplerate:duration; % time is a linear vector
freq = linspace(flim(1),flim(2),numel(time)); % freq is a linear vector
Generating a vector of uniformly-spaced values is very basic MATLAB stuff. The remaining change necessary to make freq work as a vector is also basic (literally one single character), but I have to leave something for you to do.

Avinash Mishra 2023-5-5，9:04
1. Write a MATLAB script to plot a graph with the following specifications.
2. Consider two vectors t and y.
3. t starts with 0, ends at 15, and contains 400 elements.
y = exp(-0.5*t). *Cos (2*pi. *t).
• Plot y against t. Use a blue (b) dashed (--) line for the plot.
Mark the minimum value m of the vector y by adding a point to the plot. This point should be a red asterisk marker and must be added after the blue line.
##### 1 个评论显示 无隐藏 无
DGM 2023-5-5，17:51
Why did you paste your homework as an answer? This is not an answer to the question, nor is it a question.

### 类别

Find more on 2-D and 3-D Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by