How to plot sine wave with given number of cycles?

4 次查看(过去 30 天)
I was given a question in class to plot sine wave x(t)=A_1*sin(2*π*f_1*t+θ_1 )+A_2*sin(2*π*f_2*t+θ_2 )+A_3*sin(2*π*f_3*t+θ_3 ) with number of cycles as a user input. Please help me!

回答(2 个)

Abraham Boayue
Abraham Boayue 2018-3-2
clear variables
close all
% Define the amplituted vector
A_1 = 1;
A_2 = 2;
A_3 =3;
A =[A_1 A_2 A_3];
% Choose the phase in radians
q_1 = pi/2;
q_2 = pi/4;
q_3 = pi/8;
theta = [q_1 q_2 q_3];
% Choose the frequency in Hz
f_1 = 1;
f_2 = 2;
f_3 = 3;
f = [f_1 f_2 f_3];
disp(' ')
disp(' Enter the time circle between, (choose b higher than a) a and b ');
a = input (' In the form [a b] '); % enter [-2 2] for example
disp(' ')
M = 500; % Take 500 samples of the function the, higher the better
t = a(1):4/(M-1):a(2);
y = zeros(1,M);
N = 3; % Number of terms in x(t)
for k = 1:N
y = y+ A(k)*sin(2*pi*f(k)*t+theta(k));
end
plot(t,y,'linewidth',2,'color','m')
a= title('x(t) : sum of sine waves');
set(a,'fontsize',14);
a= xlabel('t [-2 2]');
set(a,'fontsize',20);
a = ylabel('y');
set(a,'fontsize',20);
a = zlabel('z');
set(a,'fontsize',20);
grid

Abraham Boayue
Abraham Boayue 2018-3-2
t = a(1):(a(2)-a(1))/(M-1):a(2);
  1 个评论
Abraham Boayue
Abraham Boayue 2018-3-2
Replace the expression for t in full code with the one above to improve the quality of the program; if not, it will only work for the vector [-2 2].

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by