# Plot fourier series in matlab

1. Sep 21, 2014

### etf

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. Sep 22, 2014

### analogdesign

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.

3. Sep 23, 2014

### etf

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

4. Sep 23, 2014

### etf

It should be:
a=input('Unesi lower limit');
b=input('Unesi upper limit: ');

5. Sep 23, 2014

### olivermsun

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?

6. Sep 23, 2014

### analogdesign

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.

7. Sep 23, 2014

### olivermsun

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. :)

8. Sep 23, 2014

### etf

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 :)

9. Sep 23, 2014

### analogdesign

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.

10. Sep 23, 2014

### etf

I would like to try your method but I'm not sure I understand it :(

11. Sep 23, 2014

### analogdesign

12. Sep 23, 2014

### olivermsun

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

13. Sep 23, 2014

### etf

Thanks a lot guys!

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook