Results 1 to 3 of 3

Math Help - Plotting Fourier series approximation for a square wave using MATLAB.

  1. #1
    MHF Contributor alexmahone's Avatar
    Joined
    Oct 2008
    Posts
    1,074
    Thanks
    7

    Plotting Fourier series approximation for a square wave using MATLAB.

    Suppose f(x) is a square wave of period 2\pi such that

    f(x)=1 for 0<x<\pi

    f(x)=-1 for \pi<x<2\pi

    The Fourier series for f(x) is

    f(x)=\frac{4}{\pi}\left[\frac{\sin x}{1}+\frac{\sin 3x}{3}+\frac{\sin 5x}{5}+\ldots\right]

    I tried plotting this using MATLAB (using the first 10 terms):

    Code:
    x=linspace(-4*pi,4*pi,100);
    y=0;
    k=1;
        for n=1:10
            y=y+4*sin(k*x)/pi*k;
            k=k+2;
        end
    plot(x,y)
    axis([-4*pi 4*pi -2 2])
    xlabel('x')
    ylabel('Square wave approximation using first 10 terms')
    I have attached my output but it doesn't look anything like a square wave. What am I doing wrong?
    Attached Thumbnails Attached Thumbnails Plotting Fourier series approximation for a square wave using MATLAB.-square.png  
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: Plotting Fourier series approximation for a square wave using MATLAB.

    Hi alexmahone!


    Perhaps you should write

    y=y+4*sin(k*x)/(pi*k);

    instead of:

    y=y+4*sin(k*x)/pi*k;
    Follow Math Help Forum on Facebook and Google+

  3. #3
    MHF Contributor alexmahone's Avatar
    Joined
    Oct 2008
    Posts
    1,074
    Thanks
    7

    Re: Plotting Fourier series approximation for a square wave using MATLAB.

    Quote Originally Posted by ILikeSerena View Post
    Hi alexmahone!


    Perhaps you should write

    y=y+4*sin(k*x)/(pi*k);

    instead of:

    y=y+4*sin(k*x)/pi*k;
    You are just amazing!

    I now get:
    Attached Thumbnails Attached Thumbnails Plotting Fourier series approximation for a square wave using MATLAB.-square.png  
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Plotting a square wave using MATLAB
    Posted in the Math Software Forum
    Replies: 1
    Last Post: January 3rd 2012, 02:16 PM
  2. Fourier Series of Square Wave
    Posted in the Calculus Forum
    Replies: 0
    Last Post: December 13th 2009, 01:31 PM
  3. Matlab: Plotting Fourier-Series
    Posted in the Math Software Forum
    Replies: 3
    Last Post: May 18th 2009, 06:17 AM
  4. MATLAB Plotting Fourier series coefficients
    Posted in the Math Software Forum
    Replies: 1
    Last Post: January 25th 2009, 01:29 PM
  5. Plotting fourier series approximation of F(t)
    Posted in the Calculus Forum
    Replies: 2
    Last Post: October 30th 2008, 03:56 PM

Search Tags


/mathhelpforum @mathhelpforum