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

Plot fourier series in matlab

  1. Sep 21, 2014 #1

    etf

    User Avatar

    Hi!
    Here is my m-file for Fourier series plot:

    clear
    clc
    syms n
    a0=input('Enter coefficient a0: ');
    an=input('Enter coefficient an: ');
    bn=input('Enter coefficient bn: ');
    a=input('Enter lower boundary: ');
    b=input('Enter upper boundary: ');
    t=linspace(a,b,10000);
    sum=0;
    for n=1:10 %%n could be any number, bigger n - better approximation
    sum=sum+(subs(an,'n',n).*cos(2.*n.*pi.*t./(b-a))+subs(bn,'n',n).*sin(2.*n.*pi.*t./(b-a)));
    end
    series=a0+sum;
    plot(t,series)
    grid

    Problem is, it is so slow! How should I modify it in order to increase speed?
     
  2. jcsd
  3. Sep 22, 2014 #2

    analogdesign

    User Avatar
    Science Advisor

    Don't use a for loop. Do a matrix operation. That's what Matlab is designed for and it is much, much faster when you use it in that way.
     
  4. Sep 23, 2014 #3

    etf

    User Avatar

    What do you mean exactly by matrix operation? Here is how I solved it
    clear
    clc
    a0=input('Enter a0: ');
    an=input('Enter an: ','s');
    bn=input('Enter bn: ','s');
    a=input('Unesi upper limit: ');
    b=input('Unesi lower limit: ');
    k=input('Enter number of terms: ');
    t=linspace(a,b,10000);
    %original=input('Unesi originalnu funkciju f(t): ');
    suma=0;
    for n=1 : k
    ebn = evalin('caller',bn);
    ean = evalin('caller',an);
    suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
    end
    red=a0+suma;
    plot(t,red)
    grid
     
  5. Sep 23, 2014 #4

    etf

    User Avatar

    It should be:
    a=input('Unesi lower limit');
    b=input('Unesi upper limit: ');
     
  6. Sep 23, 2014 #5

    olivermsun

    User Avatar
    Science Advisor

    The slowness isn't due to the 10 loop iterations. It's due to the use of symbolic math in the statements:
    Code (Text):
    syms n
    ...
    subs(…)
    Also, does the OP really want to ask for an, bn only once each, or should they be inside a loop as well?
     
  7. Sep 23, 2014 #6

    analogdesign

    User Avatar
    Science Advisor

    I agree but I assumed the n=10 was for debugging. The OP will get a pretty poor Fourier series with n=10. At any rate, it's a good idea to use Matlab correctly because treating it like a regular programming language will end up biting you in the butt.
     
  8. Sep 23, 2014 #7

    olivermsun

    User Avatar
    Science Advisor

    I agree with your sentiment, but in this case I first want to be sure the OP understands what he/she is trying to do, independent of Matlab. :)
     
  9. Sep 23, 2014 #8

    etf

    User Avatar

    I used small number of n (n=10) to show that for even small number of terms it takes a lot of time for plot. For good approximion one should use thousands of terms and it wouldn't be completed in next 100 years using my code involving symbolic math :)
     
  10. Sep 23, 2014 #9

    analogdesign

    User Avatar
    Science Advisor

    Totally agree with what you and Oliver said. However, it's not a bad idea to think in terms of matrix operations because if you end up using Matlab for anything serious it will take forever to work if you have a lot of for loops. I've seen speed differences over over 100 times by doing large matrix operations instead of large for loops.

    That said, if you're just using it for a class and not for work or research, it might not be a big deal. Just keep in mind that to really unlock the power of Matlab you need to think in terms of matrices.
     
  11. Sep 23, 2014 #10

    etf

    User Avatar

    I would like to try your method but I'm not sure I understand it :(
     
  12. Sep 23, 2014 #11

    analogdesign

    User Avatar
    Science Advisor

  13. Sep 23, 2014 #12

    olivermsun

    User Avatar
    Science Advisor

    Ah very good! I was looking for that help page to link for etf, but I couldn't remember what it was named.
     
  14. Sep 23, 2014 #13

    etf

    User Avatar

    Thanks a lot guys!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Plot fourier series in matlab
  1. Fourier series (Replies: 1)

Loading...