Introducing an if statement into time varying sin wave

1 vue (au cours des 30 derniers jours)
Kelly
Kelly le 4 Déc 2014
Modifié(e) : Mischa Kim le 4 Déc 2014
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

Réponses (1)

Mischa Kim
Mischa Kim le 4 Déc 2014
Modifié(e) : Mischa Kim le 4 Déc 2014
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.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by