Introducing an if statement into time varying sin wave

조회 수: 1 (최근 30일)
Kelly
Kelly 2014년 12월 4일
편집: Mischa Kim 2014년 12월 4일
Hello, I am having some trouble with using a for statement. I want to create the basics of a matlab code, that says if t<x, then matlab will use a sin function, but if t>=x, matlab is to use a function equal to zero. I have been able to create a basic code for a sin function going from 1 to 10 in steps of 0.1, but when i introduce a for statement so i can use and if and else statement it does not seem to work. I am sure this is a very basic matlab coding issue, but I need this simple basis in order to build up my coding for something else.
Anyways, the coding I have is:
clear all
Co=5000/60;
Cc=72; %number of cardiac cycles per minute
Tc=60/Cc; %period of cardiac cycle per second
Ts=(2/5)*Tc;%period of systole in seconds
q=(2*pi)/Ts;
I = (Co*Tc)/60*q;
t=[1:0.1:10];
if t>1
A=I*sin(t);
else A=0;
end
plot(t,A)
I have tried including a for statement, but this also does not seem to work. Could anyone help me at all with this? It would be greatly appreciated! Thanks :D

답변 (1개)

Mischa Kim
Mischa Kim 2014년 12월 4일
편집: Mischa Kim 2014년 12월 4일
Kelly, use something like
Co = 5000/60;
Cc = 72; % number of cardiac cycles per minute
Tc = 60/Cc; % period of cardiac cycle per second
Ts = (2/5)*Tc; % period of systole in seconds
q = (2*pi)/Ts;
I = (Co*Tc)/60*q;
t = [1:0.1:10];
A = zeros(size(t)); % allocate memory for A
for ii = 1:numel(t) % start loop
if t(ii)>3 % check ii-th element of t
A(ii) = I*sin(t(ii)); % and assign ii-the element of A
else
A(ii) = 0;
end
end
plot(t,A)
Not quite sure what you mean by x in t<x so I used a random time stamp in the if-else condition.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by