Need help plotting Fourier series in matlab

Click For Summary

Discussion Overview

The discussion revolves around plotting a Fourier series in MATLAB. Participants explore the formulation of the series, the implementation of the plotting code, and the validity of the equation used for the Fourier series.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant seeks help with plotting a Fourier series equation in MATLAB, specifically the series defined by \( y=\sum_{n=1}^\infty (3\sin(2\pi nx))/(n\pi) \).
  • Another participant provides MATLAB code to plot the series over the domain \(-2\pi \le x \le 2\pi\) for \(n\) up to 10,000, including suggestions for using `linspace()` to generate points.
  • A participant questions whether the use of `linspace()` is the issue and expresses uncertainty about why the plot does not resemble a Fourier series.
  • Another participant clarifies that `linspace(-2\pi, 2\pi, N)` can be used to generate \(N\) evenly spaced points.
  • One participant realizes that the equation may be incorrect, noting that it tends to 0 as \(n\) approaches infinity, which they argue should not happen for a Fourier series.
  • A later reply challenges the derivation of the equation, suggesting that the participant may need to start over with their understanding.
  • Another participant shares that the equation was derived from a simple example used to teach Fourier series, expressing concern about potential errors in their approach.
  • Areas of Agreement / Disagreement

    Participants express uncertainty regarding the correctness of the Fourier series equation and its implications. There is no consensus on the validity of the equation or the approach taken to plot it.

    Contextual Notes

    Participants mention the need for clarification on the equation's behavior as \(n\) increases, indicating potential misunderstandings about Fourier series properties. The discussion includes unresolved questions about the correct formulation and plotting techniques.

BigDave11
Messages
9
Reaction score
0
Hi all my first post as I need to seek help!

I have just learned some simple Fourier series stuff and would like to be able to plot my answers in matlab.

Assuming this is correct I was wondering if someone would be able to walk me through plotting this equation in Matlab?

y=\sum_{n=1}^\infty (3sin(2∏nx))/(n∏)
 
Physics news on Phys.org
Here is some quick and dirty code to plot this over the domain -2\pi \le x \le 2\pi for n up to 10,000

Code:
%Use linspace() for the domain, it generates 100 evenly spaced points over
%a specified interval
x = linspace(-2*pi,2*pi);

%Define your function anonymously with two variables, X and N
y = @(X,N) (3./N*pi)*sin(2*pi*N*X);

%Loop through the n values and evaluate the function over the domain x for
%each value of n. Store those values in the rows of Y such that the first
%row of Y is n=1, the second is n=2, etc...
for n = 1:10000
Y(n,:) = y(x,n);
end

%Plot all rows of Y
figure;
plot(x,Y)

%Plot the summation over n
figure(2);
plot(x,sum(Y))

See the plots in the attached files.
 

Attachments

  • 1.jpg
    1.jpg
    21.9 KB · Views: 744
  • 2.jpg
    2.jpg
    37.8 KB · Views: 706
Maybe it is the linespace that is the problem is there a way to evenly space more than 100 points in the domain?

edit: no come to think of it I don't think this will help do you know why this doesn't resemble a Fourier series?
 
Last edited:
linspace(-2*pi, 2*pi, N) gives N evenly spaced points.
 
Wow just realized the equation must be wrong as it tends to 0 as n tends to infinity which should not happen.
 
So where did you derive the equation from? It sounds like you need to start over.
 
yeh it was a simple example used to teach me about Fourier series. I thought would be fun to see how the series works out but I fear I may have done it wrong

f(x)=3 ; 0<x<1
=-3 ; -1<x<0

Sorry about the formatting still new to me
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K