fourier series matlab

Discussion in 'Programmer's Corner' started by adrenalina, Feb 5, 2011.

  1. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    hello everyone. I made a script in matlab to graph the fourier series of a function, but I am not sure if it is the best way of doing it. it takes a while to calculate not a lot but if I lower the step size to say .01 or increase the number of terms it does take a lot longer. So I am wondering if there is a faster way of doing it. the f(x) is x^2(e^(-x)) from [-3,3]

    This is my code
    Code ( (Unknown Language)):
    1.  
    2. syms an;
    3. syms n;
    4. syms x;
    5. y = sym(0);
    6. L = 3;
    7. inc = .05;
    8. m = -3;
    9.  
    10. an = int(x^2*exp(-x)*cos(n*pi*x/L),x,-L,L)*(1/L);
    11. bn = int(x^2*exp(-x)*sin(n*pi*x/L),x,-L,L)*(1/L);
    12. a0 = int(x^2*exp(-x),x,-L,L)*(1/L);
    13. y = .5 *a0;
    14.  
    15. for i=1:25
    16.    
    17.     y = y + subs(an, n, i)*cos(i*pi*x/3) + subs(bn, n, i)*sin(i*pi*x/3);
    18.    
    19. end
    20.  
    21. w = [-L:inc:L];
    22.  
    23. for i=1:length(w)
    24.     w(i) = subs(y, m);
    25.     m = m + inc
    26. end
    27.  
    28. x = [-L:inc:L];
    29. plot(x,w)
    30.  
    first it calculates an, bn, and a0 with the function int(). In the first for loop it calculates the series to the nth term (n is equal to 25 in this case). so now it has a variable y with only x's no n's. then I allocate space for w which is the y values of the series. according to matlab its faster to allocate the space for a variable instead of adding values to it in the for. in the second for it calculates the y value for the series in increments of .05 for x. at the end it plots x and w.
    I'm still a beginner in matlab so any help in making this code quicker and better would be appreciated.
     
    Last edited: Feb 5, 2011
  2. adrenalina

    Thread Starter Active Member

    Jan 4, 2011
    78
    3
    I've been messing with the code and I made it much much faster I got rid of the second for loop since I noticed its not necessary. its faster with an increment in x of .0001 compared to .05 with the old code.

    Code ( (Unknown Language)):
    1.  
    2. syms an;
    3. syms n;
    4. syms x;
    5. y = sym(0);
    6. L = 3;
    7. inc = .0001;
    8.  
    9.  
    10. an = int(x^2*exp(-x)*cos(n*pi*x/L),x,-L,L)*(1/L);
    11. bn = int(x^2*exp(-x)*sin(n*pi*x/L),x,-L,L)*(1/L);
    12. a0 = int(x^2*exp(-x),x,-L,L)*(1/L);
    13. y = .5 *a0;
    14.  
    15. for i=1:25
    16.    
    17.     y = y + subs(an, n, i)*cos(i*pi*x/3) + subs(bn, n, i)*sin(i*pi*x/3);
    18.    
    19. end
    20.  
    21.  
    22. x = [-L:inc:L];
    23.  
    24. w = subs(y, x);
    25.  
    26. plot(x,w)
    27.  
     
  3. faiza saher

    New Member

    Dec 31, 2010
    1
    0
    plz any one tell how to write a matlab code for fourier series
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,453
    3,371
    To compute the Fourier Transform of v, use

    y = fft(v)
     
Loading...