Fourier Series and SCILAB

Today I wrote a code that calculates the Fourier Coefficients.

In case you don’t know what a Fourier Series is, then, basically it is a way of approximating or representing a periodic function by a series of simple harmonic(sine and cosine) functions.

You can check out the wikipedia for  more information on it:

For a waveform
f(x) with period 2L=2π/k, we have that k=2π/2L=π/L and nkx =nπx/L , Then we have
fourier series coefficients
So, here’s how my code works:
I created a function


l :half of the period, (periodicity of the function f which is to be approximated by Fourier Series)

n: no. of Fourier Coefficients you want to calculate

 f: function which is to be approximated by Fourier Series

A0: The first fourier coefficient.

A: An array/matrix whose nth element is the nth coefficient An.

B: An array/matrix whose nth element is the nth coefficient Bn.

The code is:

//Fourier Series Coefficients
//The following function returns the fourier coefficients,'a0', 'An' & 'Bn'
//User needs to provide the following arguments:
//l=periodicity of the function f which is to be approximated by Fourier Series
//n=no. of Fourier Coefficients you want to calculate
//f=function which is to be approximated by Fourier Series
//*Some necessary guidelines for defining f:
//*The program integrates the function f from -l to l so make sure you define the function f correctly in the interval -l to l.
//for more information on Fourier Series visit:
//Written by: Manas Sharma([email protected])
//For more useful toolboxes and tutorials on Scilab visit:
function [a0, A, B]=fourier(l, n, f)
    for i=1:n
        function b=f1(x, f)
        function c=f2(x, f)


fourier output


That’s it. I hope it was not too difficult to understand. If you have any questions I will be glad to answer them.


PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
[wpedon id="7041" align="center"]

10 thoughts on “Fourier Series and SCILAB

  1. Hi! thank you for the tutorial. I was just wondering if you need to change the function where would you have to make the change. For example if you have a square plot.

  2. Hi,Thank you for the tutorial, I was wondering how to take fourier series of some other function for that matter of fact if i wanted to plot FS of triangular wave where and how would I define f(x) in this case.

  3. in exam one question came find Fourier series coefficient of f(x)=3x^2 in(-pi,pi)for n=5 .Plase reply how to solve this kind of problem

  4. Hi,
    Manas i need your help regarding a scilab prog.To solve the S -wave schrodinger equation for the ground state and first excited state of hydrogen atom :(m is the reduced mass of electron.Obtain the energy eigen value and plot the corresponding value wave function. e=3.795 (eVA)1/2, h=1973(eVA) and m=0.511*106 eV/c2

  5. Hello Sir;
    I want to just ask you that the error is displaying while executing this prog in my PC at line 20 i.e.
    x=-5*l:0.1:5*l . Its showing that there is undefined variable ‘l’.
    Can you just help me out.

  6. Madam i have a trignometric fourier series with all coefficients how can i plot it . do u have code forthat ?

  7. Is this code for piecewise function too? i couldn’t use, so can you make the video for piecewise function too?

Leave a Reply

Your email address will not be published. Required fields are marked *