Results 1 to 2 of 2

Math Help - MATLAB Plotting Fourier series coefficients

  1. #1
    Newbie
    Joined
    Nov 2006
    Posts
    10

    MATLAB Plotting Fourier series coefficients



    I'm trying to plot the above fourier series coefficients, what am I doing wrong? here is my matlab code:

    Code:
    clear
    time=-1:0.01:1;
    l=0.5;
    
    x=0; %initialize to 0 
    
    for n=1:40
    if (n==2*l), x=x-(1/(pi*l)); end;  
    if (n==(2*l+1)), x=x+(2/((pi*(2*l+1))))*(1+(2*(-1)^l)/(pi*(2*l+1))); end;
    plot(time,real(x))
    xlabel('time (sec)')
    ylabel('x(t)')
    title(['n=', int2str(n), ' Fourier Series Approximation'])
    pause(1)
    end
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    May 2006
    Posts
    244
    Quote Originally Posted by synnexster View Post


    I'm trying to plot the above fourier series coefficients, what am I doing wrong? here is my matlab code:

    Code:
    clear
    time=-1:0.01:1;
    l=0.5;
     
    x=0; %initialize to 0 
     
    for n=1:40
      if (n==2*l), x=x-(1/(pi*l)); end;  
      if (n==(2*l+1))
         x=x+(2/((pi*(2*l+1))))*(1+(2*(-1)^l)/(pi*(2*l+1)));
      end;
      plot(time,real(x))
      xlabel('time (sec)')
      ylabel('x(t)')
      title(['n=', int2str(n), ' Fourier Series Approximation'])
      pause(1)
    end
    1. Don't use l as a variable name, it is too easy to confuse it with 1.

    2. l is a constant, so the loop only does anything when n=1, and n=2.

    3. It looks as though you intend x to be an array, but its only a scalar.

    4. It is not clear what you are trying to do here.

    The following creates an array of the coefficients:

    Code:
    clear
     
    b=zeros(1,40); %initialize to a row vector of 40 0's 
     
    for n=1:40
      if (mod(n,2)==0)
         L=n/2;
         b(n)= -1/(pi*L); 
      else
         L=(n+1)/2;
         b(n)=2/((pi*(2*L+1))))*(1+(2*(-1)^L)/(pi*(2*L+1));
      end
    end
    .
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: January 3rd 2012, 11:53 AM
  2. Replies: 2
    Last Post: July 5th 2010, 02:09 PM
  3. Fourier Series Coefficients
    Posted in the Calculus Forum
    Replies: 3
    Last Post: April 12th 2010, 01:43 PM
  4. Matlab: Plotting Fourier-Series
    Posted in the Math Software Forum
    Replies: 3
    Last Post: May 18th 2009, 06:17 AM
  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