Help me with the function that creat triangle wave.
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
F=20kHz
Tp=period=1/F
Tw=1/2Tp
in 1 Tp
P(t)=1-|(t-Tw)/Tw| if 0<=t<=Tw
P(t)=0 Tw<t<=Tp
Creat 5 pulse with P(t) using function
I try:
function TriangleWave=P(t)
f=20000;
Tp=1/f;
Tw=Tp/2;
for i=1:Tp/100:t
n=mod(t,Tp);
if (n>=0&n<=Tw)
P(t)=1-abs((t-Tw)/Tw);
elseif (n>=Tw&n<=Tp) P(t)=0;
end
end
plot(t,P)
but i doesnt show the wave in plot. What went wrong? please help.
2 commentaires
Jan
le 17 Sep 2013
"It doesn't show" does not help to understand the problem. What is happening instead?
Réponses (1)
Jan
le 17 Sep 2013
What is "t"? At first it seems to be a scalar, but in the PLOT() command you might expect it to be a vector?
2 commentaires
Image Analyst
le 17 Sep 2013
Modifié(e) : Image Analyst
le 17 Sep 2013
Sure he could, but usually for homework what you'll get is hints and guidance rather than outright "fixes". The only thing I see off the top of my head (without running it) is that you should have double ampersands:
n>=0 && n<=Tw
and that you're going to have millions of iterations because Tp/100 is 5e-7. Look in the right hand margin for orange lines indicating errors or warnings.
But this is something that can be solved with the debugger. Did you use it? If not, try that first: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/. Chances are, you'll not need to ask us again and wait a long time for an answer to your homework assignment if you know how to use the debugger. For example, place a breakpoint on the plot() line and see what it tells you the values of t and P are.
Voir également
Catégories
En savoir plus sur Logical 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!