Help me with the function that creat triangle wave.

4 ビュー (過去 30 日間)
Nguyen
Nguyen 2013 年 9 月 17 日
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 件のコメント
Jan
Jan 2013 年 9 月 17 日
"It doesn't show" does not help to understand the problem. What is happening instead?
Nguyen
Nguyen 2013 年 9 月 17 日
it mean the plot is blank, i cant see the wave

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2013 年 9 月 17 日
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 件のコメント
Nguyen
Nguyen 2013 年 9 月 17 日
thank, i know which part is wrong, can you fix it for me?
Image Analyst
Image Analyst 2013 年 9 月 17 日
編集済み: Image Analyst 2013 年 9 月 17 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by