フィルターのクリア

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

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by