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

Click For Summary

Discussion Overview

The discussion revolves around optimizing MATLAB code for plotting Fourier series. Participants explore various coding techniques to improve execution speed, focusing on the use of matrix operations versus for loops, and the implications of symbolic math in the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests avoiding for loops in favor of matrix operations to enhance performance, as MATLAB is optimized for such operations.
  • Another participant questions the meaning of "matrix operation" and presents their own code that still uses a for loop, indicating a lack of clarity on the proposed optimization.
  • Concerns are raised about the use of symbolic math, which may be a significant factor in the code's slowness, rather than the number of iterations in the loop.
  • Some participants express that using a small number of terms (n=10) for demonstration purposes is insufficient for a good Fourier series approximation, suggesting that thousands of terms would be necessary for accuracy.
  • There is a discussion about the importance of understanding MATLAB's capabilities and the potential performance issues that arise from treating it like a traditional programming language.
  • A participant shares a link to MATLAB documentation on vectorization as a resource for those looking to improve their coding practices.

Areas of Agreement / Disagreement

Participants generally agree that using matrix operations is preferable to for loops for performance reasons, but there is no consensus on the best approach to implement these optimizations. The discussion remains unresolved regarding the specifics of how to effectively refactor the original code.

Contextual Notes

There are unresolved questions about the use of symbolic math and its impact on performance, as well as the appropriateness of the number of terms used in the Fourier series approximation. Additionally, the clarity of the proposed matrix operation method is still in question.

Who May Find This Useful

This discussion may be useful for MATLAB users interested in optimizing their code for mathematical computations, particularly those working with Fourier series or similar numerical methods.

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!
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
Replies
5
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
7
Views
4K
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K