Need help plotting Fourier series in matlab

In summary, the person is learning about Fourier series and is trying to plot the answer to a equation. However, they are unsure of how to do it and need help.
  • #1
BigDave11
9
0
Hi all my first post as I need to seek help!

I have just learned some simple Fourier series stuff and would like to be able to plot my answers in matlab.

Assuming this is correct I was wondering if someone would be able to walk me through plotting this equation in Matlab?

[tex] y=\sum_{n=1}^\infty (3sin(2∏nx))/(n∏)[/tex]
 
Physics news on Phys.org
  • #2
Here is some quick and dirty code to plot this over the domain [itex]-2\pi \le x \le 2\pi[/itex] for n up to 10,000

Code:
%Use linspace() for the domain, it generates 100 evenly spaced points over
%a specified interval
x = linspace(-2*pi,2*pi);

%Define your function anonymously with two variables, X and N
y = @(X,N) (3./N*pi)*sin(2*pi*N*X);

%Loop through the n values and evaluate the function over the domain x for
%each value of n. Store those values in the rows of Y such that the first
%row of Y is n=1, the second is n=2, etc...
for n = 1:10000
Y(n,:) = y(x,n);
end

%Plot all rows of Y
figure;
plot(x,Y)

%Plot the summation over n
figure(2);
plot(x,sum(Y))

See the plots in the attached files.
 

Attachments

  • 1.jpg
    1.jpg
    21.9 KB · Views: 674
  • 2.jpg
    2.jpg
    37.8 KB · Views: 633
  • #3
Maybe it is the linespace that is the problem is there a way to evenly space more than 100 points in the domain?

edit: no come to think of it I don't think this will help do you know why this doesn't resemble a Fourier series?
 
Last edited:
  • #4
linspace(-2*pi, 2*pi, N) gives N evenly spaced points.
 
  • #5
Wow just realized the equation must be wrong as it tends to 0 as n tends to infinity which should not happen.
 
  • #6
So where did you derive the equation from? It sounds like you need to start over.
 
  • #7
yeh it was a simple example used to teach me about Fourier series. I thought would be fun to see how the series works out but I fear I may have done it wrong

f(x)=3 ; 0<x<1
=-3 ; -1<x<0

Sorry about the formatting still new to me
 

What is a Fourier series?

A Fourier series is a mathematical representation of a periodic function as an infinite sum of sine and cosine functions. It is named after French mathematician and physicist Joseph Fourier.

Why do we use Fourier series?

Fourier series are used to analyze and approximate periodic functions in various fields, including mathematics, physics, engineering, and signal processing. It allows us to break down complex functions into simpler components, making it easier to analyze and manipulate them.

How do I plot a Fourier series in MATLAB?

To plot a Fourier series in MATLAB, you can use the plot function and specify the x and y values as vectors. You will need to calculate the Fourier coefficients and use them to generate the y values. There are also built-in functions in MATLAB, such as fourier and ifourier, that can help you plot Fourier series.

What are Fourier coefficients?

Fourier coefficients are the coefficients of the sine and cosine terms in a Fourier series. They represent the amplitudes and phases of the individual components in the series, and can be calculated using various methods such as integration, complex analysis, and numerical methods.

What are some common mistakes when plotting Fourier series in MATLAB?

Some common mistakes when plotting Fourier series in MATLAB include not specifying the correct range for the x values, forgetting to include the constant term in the series, and using incorrect formulas for calculating the Fourier coefficients. It is also important to ensure that the function being plotted is periodic and has a finite number of discontinuities for the Fourier series to be accurate.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • Calculus and Beyond Homework Help
Replies
3
Views
286
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
939
Back
Top