Help with plotting triangular wave

  • Thread starter Davidlong
  • Start date
  • #26
28
0
Yes. Strange
 
  • #27
lewando
Homework Helper
Gold Member
1,352
138
I remember you said in an earlier post that there was not much difference between 10 and 30. Are you clearing out vector s between each run? If you are not, try doing s=0 before each loop. If you are still having trouble, post your complete m-file.
 
  • #28
28
0
I didn't look at the actual amplitude values before, only the position of the peak of the waves.

I've posted the m-file
 

Attachments

  • m-file.txt
    824 bytes · Views: 382
  • #29
lewando
Homework Helper
Gold Member
1,352
138
That file you attached did not have the statement "s=0" before the 2nd and 3rd n-loop. Please add it (before or after the maxTerms=...).

Looks like you hardcoded the peak value in the title. There are ways to do it dynamically, but if the amplitudes are all going to be pretty much the same, it will add little value.
 
  • #30
lewando
Homework Helper
Gold Member
1,352
138
Also, one final thing: what is the purpose of doing:

% Make wave start at 0
s = s - s(1);

This introduces an artificial offset to the data. Is this required?
 
  • #31
28
0
Also, one final thing: what is the purpose of doing:

% Make wave start at 0
s = s - s(1);

This introduces an artificial offset to the data. Is this required?

It's not really required. I re-ran is by doing s=0; before all the maxterms and i did get a similar amplitude range for all 3 plots. The peaks don't vary much. The question asks us to provide the peak value hence the reason for hard coding it. Is there a simple bit of code that could automatically provide it?
 
  • #32
lewando
Homework Helper
Gold Member
1,352
138
Try:

m = max(s)
title_string = sprintf('maximum is: %f',m)
title(title_string)

As for your offset-- then don't do it. It is affecting the peak value you are reporting.
 
  • #33
28
0
I guess the peak value is the same for all 3 plots. The only noticeable difference is the sharpening of the edge of the triangle. I assume as convergence occurs there will be less visible change.
 

Attachments

  • graphs.jpg
    graphs.jpg
    46.9 KB · Views: 349
  • #34
lewando
Homework Helper
Gold Member
1,352
138
They should not be the same. They should be close, but N=30 should give you a larger peak than N=6. Just considering the summation at the t=0, cos(0)=1, point.
 
  • #35
28
0
Ah it was because i didn't put m=max(s) before every maxterm. I have did that now and got a larger peak. 1.192, 1.209 and 1.225
 

Related Threads on Help with plotting triangular wave

Replies
0
Views
2K
Replies
4
Views
3K
  • Last Post
Replies
0
Views
2K
  • Last Post
Replies
14
Views
4K
  • Last Post
Replies
3
Views
20K
  • Last Post
Replies
3
Views
6K
Replies
1
Views
2K
  • Last Post
Replies
1
Views
963
Replies
2
Views
2K
Replies
6
Views
7K
Top