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

조회 수: 1 (최근 30일)
Brian James
Brian James 2017년 4월 12일
편집: Vic 2018년 1월 21일
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
Vandana Ravichandran 2017년 4월 14일
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