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

In summary: You're awesome!In summary, the conversation discusses a method for increasing the speed of a Fourier series plot in MATLAB. The suggestion is to use matrix operations instead of for loops, which can significantly improve performance. The conversation also touches on the use of symbolic math and the importance of understanding how to use MATLAB effectively for more complex tasks.
  • #1
etf
179
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
  • #2
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
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
It should be:
a=input('Unesi lower limit');
b=input('Unesi upper limit: ');
 
  • #5
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?
 
  • #6
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.
 
  • #7
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
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
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!
 

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

1. How do I plot a Fourier series in MATLAB?

To plot a Fourier series in MATLAB, you can use the "plot" function and provide the frequency and amplitude values as inputs. You can also use the "stem" function for a discrete plot.

2. Can I change the number of terms in the Fourier series plot?

Yes, you can change the number of terms by adjusting the range of the frequency values in the input. The more terms you include, the more accurate the plot will be.

3. How can I add a legend to the Fourier series plot?

To add a legend in MATLAB, you can use the "legend" function and provide labels for each plot. You can also use the "DisplayName" property while plotting to automatically add a legend.

4. What is the difference between a discrete and continuous Fourier series plot?

A discrete Fourier series plot shows the values of the series at specific frequencies, while a continuous Fourier series plot shows a smooth curve representing the series over a continuous range of frequencies.

5. How do I interpret the Fourier series plot?

The Fourier series plot shows the amplitude and frequency components of a function. The amplitude represents the strength of each frequency component, while the frequency represents the number of cycles in the function over a specific interval.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
367
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
960
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
990
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top