Help me with the function that creat triangle wave.
조회 수: 4 (최근 30일)
이전 댓글 표시
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
2013년 9월 17일
"It doesn't show" does not help to understand the problem. What is happening instead?
답변 (1개)
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
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 Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!