MATLAB How can I optimize my MATLAB code for faster Fourier series plot?

AI Thread Summary
The discussion centers around optimizing a MATLAB script for plotting Fourier series. The original code uses a for loop and symbolic math, which significantly slows down execution, especially with a larger number of terms. Participants emphasize the importance of matrix operations in MATLAB for improved performance, suggesting that avoiding for loops can lead to substantial speed increases. The use of symbolic math is identified as a major factor in the script's inefficiency. There's a consensus that while a small number of terms (like n=10) can be used for testing, achieving accurate Fourier series approximations typically requires many more terms, which would be impractical with the current approach. The conversation also touches on the necessity of understanding MATLAB's capabilities to effectively utilize its power for serious applications. A helpful resource on vectorization in MATLAB is shared to assist in transitioning from traditional programming methods to more efficient matrix operations.
etf
Messages
179
Reaction score
2
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?
 
Physics news on Phys.org
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.
 
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
 
It should be:
a=input('Unesi lower limit');
b=input('Unesi upper limit: ');
 
analogdesign said:
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.
The slowness isn't due to the 10 loop iterations. It's due to the use of symbolic math in the statements:
Code:
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?
 
olivermsun said:
The slowness isn't due to the 10 loop iterations. It's due to the use of symbolic math in the statements:

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.
 
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. :)
 
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 :)
 
etf said:
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 14 years using my code involving symbolic math :)

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
I would like to try your method but I'm not sure I understand it :(
 
  • #12
Ah very good! I was looking for that help page to link for etf, but I couldn't remember what it was named.
 
  • #13
Thanks a lot guys!
 
Back
Top