How to graph Asin(pi*f*t) with a for loop?

1 次查看(过去 30 天)
In this problem you will compute and then plot three sinusoids.
% The form of each sinusoid will be u=Asin(2*pi*f*t).
%
% a) Initialize t to run from 0 to 1 in 0.01 increments
clear all;
clc;
t=[0:.01:1];
% b) Initialize A to [1/3 2/3 1] and set f = 1
A=[1/3 2/3 1];
f=1;
% c) Initialize u using the zeros command with the length of A and the
% length of t as arguments
u=zeros(length(A),length(t))
% d) Initialize a character array to ['k';'r';'g'] - this array will control
% the line color during plotting
lincol=['k';'r';'g'];
% e) Open a figure window
figure(1),
% f) Use a "for" loop to compute the rows of "y" based on the values of "a"
% Also inside the "for" loop, plot each row of "u" vs "t" with
% linewidth 1.5 and colors as specified in the character arrray that
% you initialized above (a=-0.2, black; a=0, red; a=0.1, green).

回答(2 个)

Vandana Ravichandran
You can use the "sin" function in MATLAB to compute u=Asin(2*pi*f*t). As an example, one iteration of the for loop can be computed and plotted in the following way:
A = 1/3;
t = 0:0.01:1;
f = 1;
result = A * sin(2*pi*f*t);
plot(t, result,'Color','k')
You can extend the above in order to use a for-loop

Vic
Vic 2018-1-21
编辑:Vic 2018-1-21
I do not think this plots the signal properly but this is my attempt. Someone may edit it.
A = 5; %magnitude which decays with time
freqSamplingRate = 8000; %needs to be less than half of the input frequency, f.
tt = -2:(1/freqSamplingRate):2;
a = 0.34; %calculated decaying factor
f = 1000; %frequency(Hz)
for r = 1:2 %loop cycles
xx = A*exp(-a*tt).*cos(2*pi*f*tt); %the signal which is outputted
plot(tt,xx)%plotting the output signal
xlabel('time(s)'), ylabel('Amplitude (units)'); %naming axis
soundsc(xx); %sounding the output signal
pause(3); %pause between each loop
end %ending of loop

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by