Plotting Series in Matlab: Code for Two Series with Cosine and Sine Functions

Click For Summary

Discussion Overview

The discussion revolves around plotting two mathematical series in Matlab, specifically involving cosine and sine functions. Participants seek assistance in writing the appropriate code for these series, which are defined over specified ranges for the variable t and involve summations over n.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant outlines the two series to be plotted, specifying the mathematical expressions and the ranges for t and n.
  • Another participant suggests constructing vectors for n and t to compute the series, emphasizing the need for a 2D matrix to represent the sums.
  • A participant questions the correctness of the parentheses in the expressions, seeking clarification on whether the summation is correctly interpreted.
  • There is a correction regarding the inclusion of terms after the sigma notation in the first series, with a participant reiterating the strategy of avoiding direct code provision.
  • Some participants discuss the potential use of loops in Matlab, expressing a preference for avoiding them in favor of more elegant solutions.
  • There is mention of alternative approaches, including the possibility of using 1D vectors for each term instead of a 2D matrix, with a focus on the mathematical structures needed for implementation.
  • Clarifications are made regarding the range of n in the second series, with emphasis on using only odd integers.

Areas of Agreement / Disagreement

Participants express various viewpoints on how to approach the coding problem, with no consensus reached on a single method or solution. Disagreements exist regarding the interpretation of the mathematical expressions and the best coding practices in Matlab.

Contextual Notes

There are unresolved questions about the correct interpretation of the series definitions, particularly concerning the summation limits and the handling of odd integers in the second series. The discussion also highlights the complexity of implementing the series in Matlab without providing explicit code.

Mphil1984
Messages
3
Reaction score
0
I am going to plot two series in Matlab. They are as following. please help me to write the appropriate code for them:

1-f(t)=(2/pi)+(4/pi)*Ʃ(1/(1-4*n^2))*cos(2*n*pi*t) -1≤t≤1 n=1 forN=200

2-f(t)=(4/0.25^2)*Ʃ(sin(0.25*n*pi)/(n*pi)^2)*sin(n*pi*t) -2≤t≤2 n=1,3,5 forN=200
 
Physics news on Phys.org
Welcome to PF:
You want to construct a vector for n, the use that and your formula to compute a vector of each element in the series.

The complication appears to be that you have two variables: n and t.
So you'll probably have to construct vectors for the different frequencies as well as the coefficients ... with the aim of ending up with a 2D matrix whose columns are the elements of the sum (corresponding to each n) and the rows are the functions at discrete samples (corresponding to values of t).

Summing the columns provides a row vector whose entries are f(t) sampled for discrete values of t. Then you can plot(t,f).
 
Could you please confirm the parentheses are correct in the two expressions? As written, I interpret the summation to be over n, ie {sum [1/(1-4*n^2)*cos(2*n*pi*t)] over n=1..200} but the expression 1 appears to be written as cos(2*n*pi*t)*{sum[1/(1-4*n^2)] over n=1..200} (et sim for 2).

Do you want f as a function of t and variable n or just as a function of t with the specified range for n?
 
I ust figured everything after the sigma was included in the sum.

for 1.f(t)=\frac{2}{\pi} + \frac{4}{\pi} \sum_{n=1}^{200} \frac{\cos(2n\pi t)}{1-4n^2}... and for 2.f(t)=64\sum_{n=1}^{200} \frac{\sin(\frac{n\pi}{4})}{(n\pi)^2}\sin(n\pi t)

I still think the strategy I've outlined is the way to go here - we should avoid actually providing the code though.
 
Simon Bridge said:
Welcome to PF:
You want to construct a vector for n, the use that and your formula to compute a vector of each element in the series.

The complication appears to be that you have two variables: n and t.
So you'll probably have to construct vectors for the different frequencies as well as the coefficients ... with the aim of ending up with a 2D matrix whose columns are the elements of the sum (corresponding to each n) and the rows are the functions at discrete samples (corresponding to values of t).

Summing the columns provides a row vector whose entries are f(t) sampled for discrete values of t. Then you can plot(t,f).

That would be a possible solution.What if we do not consider sum command?
 
Simon Bridge said:
I ust figured everything after the sigma was included in the sum.

for 1.f(t)=\frac{2}{\pi} + \frac{4}{\pi} \sum_{n=1}^{200} \frac{\cos(2n\pi t)}{1-4n^2}... and for 2.f(t)=64\sum_{n=1}^{200} \frac{\sin(\frac{n\pi}{4})}{(n\pi)^2}\sin(n\pi t)

I still think the strategy I've outlined is the way to go here - we should avoid actually providing the code though.

In the second one n=1,3,5. everything else is right.
 
Well, here's how it might be done in Mathcad.

I have gut feeling (a colon sensation, one might say) that there is sum way of doing a similar thing in Matlab. I have a forboding that loops may occur to somebody, but they should probably be avoided.

attachment.php?attachmentid=48583&stc=1&d=1340570867.jpg
 

Attachments

Mphil1984 said:
That would be a possible solution.What if we do not consider sum command?
What exact steps you use are up to you - I have not suggested any exact code, just the mathematical structures you'd need to employ.

Technically you don't even need a 2D matrix to store all the terms, you could use a different 1D vector for each of the 200 terms instead. The approach outlines is a "brute force" method - there will be more elegant approaches.

Note: for the second one where n=1,3,5...200 (or is it n=1,3,5...399 for N=200 terms?)
Anyway = in MATLAB you need only do the step, so all the odd numbers are n=1:2:199; for all the odd numbers from 1 to 200. Writing it out in sigma notation is trickier than that... I need to sum over integer m from 0 to 99 and n=2m+1 and it looks like:f(t)=64\sum_{n=1}^{200} \sin\left ( \frac{(2n-1)\pi}{4}\right ) \frac{\sin((2n-1)\pi t)}{((2n-1)\pi)^2}... for the first 200 odd numbers.
[One way matlab/octave is easier than pencil and paper :) ]

Note - the mathcad notes look like what I was suggesting only using jargon.
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K