Help with plotting triangular wave
古いコメントを表示
Hi guys. I am struggling with a homework question.
*A triangular wave with period T may be written as: 1/(2n+1)^2 * cos((2n+1)*w0*t) (this is a series, n starts at 0 and goes on until infinity). where w0 = 2pi/T. This wave form is sampled, with a sampling time of TS = T/200, to yield the sampled signal x(n).
Use MATLAB to demonstrate how the series converges to the triangular wave.
Generate a plot(properly labelled) with 6, 10 and 30 terms for a value of T = 2.*
The code i inputted into matlab is
t=2; Ts=t/200; w=(2*pi)/t; n=0:9999; x=((2*n+1).^-2).*(cos((2*n+1)*w*Ts)); plot(x)
When i plot this it doesn't give a triangular wave. I must have done something wrong or missed a detail. Any help would be appreciated.
Thank you very much
回答 (1 件)
- You evaluate x(t) only at a single point t=Ts. You're supposed to evaluate at many sampling times, t, spaced apart by Ts.
- You haven't summed over n.
- You will make life easier on yourself (and on us, and on your graders) if you define T, Ts, and t in your code the same way as the homework exercise defines them. Instead, your code changes T to t.
6 件のコメント
David
2012 年 12 月 31 日
Brian B
2012 年 12 月 31 日
Hi David,
If this course is your first introduction to MATLAB, you should first make sure you understand how to use the plot function. Type
doc plot
and see the first example. In particular, you will want to use something like
plot(t,y)
instead of
plot(y,t)
assuming t is your time vector.
Then you may find it helpful to start by plotting just the first term of the series (for which n=0). This should be a signal, that is, a function of time. You will need to plot the values of that signal at several points in time in order to see what it looks like. Your code above only computes a value for t=0.
At which times should you plot the signal? You'll have to use the sample time TS (or Ts), and your understanding of the function x0(t) = 1/(2*0+1)^2 * cos((2*0+1)*w0*t) to determine appropriate samples.
Brian
I think you want
t=-T/2:Ts:T/2;
and later
plot(t,s)
David
2013 年 1 月 1 日
編集済み: Image Analyst
2013 年 1 月 1 日
Image Analyst
2013 年 1 月 1 日
Try it like this:
n=0;
T=2;
Ts=T/200;
t=-T/2 : Ts : T/2;
w=(2*pi)/T;
s = 0;
figure;
maxTerms = 6; % also use 10 and 30
for n = 0 : maxTerms - 1
s = s + ((2*n+1)^-2) * (cos((2*n+1)*w*t));
end
% Make wave start at 0
s = s - s(1);
plot(t,s)
grid on;
hold on;
maxTerms = 10;
for n = 0 : maxTerms - 1
s = s + ((2*n+1)^-2) * (cos((2*n+1)*w*t));
end
% Make wave start at 0
s = s - s(1);
plot(t,s)
maxTerms = 30;
for n = 0 : maxTerms - 1
s = s + ((2*n+1)^-2) * (cos((2*n+1)*w*t));
end
% Make wave start at 0
s = s - s(1);
plot(t,s)
Using this code i do get a triangular wave but when i plot for n=10,30 terms the graph doesn't really change much.
Looks fine to me. The difference between nmax=10 and nmax=30 is subtle, but I still do see a noticeable sharpening of the triangle. At some point, i.e., as convergence occurs, it is supposed to stop changing. visibly
カテゴリ
ヘルプ センター および File Exchange で Pie Charts についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!