Help me with the function that creat triangle wave.

3 views (last 30 days)
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 Comments
Jan
Jan on 17 Sep 2013
"It doesn't show" does not help to understand the problem. What is happening instead?
Nguyen
Nguyen on 17 Sep 2013
it mean the plot is blank, i cant see the wave

Sign in to comment.

Answers (1)

Jan
Jan on 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 Comments
Nguyen
Nguyen on 17 Sep 2013
thank, i know which part is wrong, can you fix it for me?
Image Analyst
Image Analyst on 17 Sep 2013
Edited: Image Analyst on 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!