Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help plotting Fourier series in matlab

  1. Sep 24, 2013 #1
    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?

    [tex] y=\sum_{n=1}^\infty (3sin(2∏nx))/(n∏)[/tex]
  2. jcsd
  3. Sep 25, 2013 #2


    User Avatar
    Gold Member

    Here is some quick and dirty code to plot this over the domain [itex]-2\pi \le x \le 2\pi[/itex] for n up to 10,000

    Code (Text):

    %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);

    %Plot all rows of Y

    %Plot the summation over n

    See the plots in the attached files.

    Attached Files:

    • 1.jpg
      File size:
      22 KB
    • 2.jpg
      File size:
      39.3 KB
  4. Oct 2, 2013 #3
    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: Oct 2, 2013
  5. Oct 2, 2013 #4

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    linspace(-2*pi, 2*pi, N) gives N evenly spaced points.
  6. Oct 3, 2013 #5
    Wow just realised the equation must be wrong as it tends to 0 as n tends to infinity which should not happen.
  7. Oct 3, 2013 #6


    User Avatar
    Gold Member

    So where did you derive the equation from? It sounds like you need to start over.
  8. Oct 3, 2013 #7
    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
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook