How to plot sine wave with given number of cycles?

6 views (last 30 days)
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!

Answers (2)

Abraham Boayue
Abraham Boayue on 2 Mar 2018
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 on 2 Mar 2018
t = a(1):(a(2)-a(1))/(M-1):a(2);
  1 Comment
Abraham Boayue
Abraham Boayue on 2 Mar 2018
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].

Sign in to comment.

Categories

Find more on Formatting and Annotation 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!