Calculating Fourier Series and plotting it -SCILAB

In the last post I showed you guys how to calculate Fourier Coefficients for a given function defined in the range, [-l,l].

We can also use the Fourier Coefficients to calculate the Fourier Series and then Plot the FS Approximation and compare it to the original function.

I have used the same code as before and just added a few more lines of code.

If you haven’t read the last post which is being continued here then I recommend you read that first here.

Code:

funcprot(0);
function [a0,A,B]=fourierplot(l,n,f)
    clf();
    a0=1/l*intg(-l,l,f,.000000001);
    for i=1:n
        function b=f1(x,f)
            b=f(x)*cos(i*%pi*x/l);
        endfunction
        function c=f2(x,f)
            c=f(x)*sin(i*%pi*x/l);
        endfunction
        A(i)=1/l*intg(-l,l,f1,.000000001);
        B(i)=1/l*intg(-l,l,f2,.000000001);
    end
    function series=solution(x)
        series=a0/2;
        for i=1:n
            series=series+A(i)*cos(i*%pi*x/l)+B(i)*sin(i*%pi*x/l);
        end
    endfunction
    x=-5*l:0.1:5*l;
    plot(x,solution(x));
endfunction

As you can notice, the code is almost the same as before, except that I have changed the name of the function from ‘fourier’ to ‘fourierplot’. And in the previous post I had only calculated the Fourier Coefficients, a0, A, B, but this time I am using those to calculate the Fourier Series, through the function ‘solution’. And finally I have plotted the Fourier Series.

Sample Demo:

deff('a=f(x)','a=x');
[A0,A,B]=fourierplot(2,100,f)

Output:

fourierplot output approximating x by series

In the above sample code I have defined a function f(x)=x, and then I have called the function fourierplot with arguments l=2, n=100 & the function f. l is the half of the period so the function is periodic with a period of 2l=4 from [-2,2], which is evident from the graph. If the value of n is not large enough then you might not get a very good approximation.

I have created a module in scilab which contains both the above macro, and once installed can be used as an in-built function. You can download it from here: https://atoms.scilab.org/toolboxes/fstools

Tutorial:

Leave your questions/suggestion/corrections in the comments section down below and I’ll get back to you soon.

 

[wpedon id="7041" align="center"]

14 thoughts on “Calculating Fourier Series and plotting it -SCILAB

  1. It’s showing round off error. How to remove that?
    I have taken 0.00000000001

    1. Hi there,
      Are you getting error for larger tolerances as well? Maybe try using a larger tolerance value.

      1. Yes I’m getting error for larger tolerance as well.
        It’s showing- round of error and try for large tolerance value.. something like this..

          1. Okay, found the problem. Turns out, in the newer(6.0) version we need to pass another argument that is the relative error.
            I wrote the code with some older version, therefore it worked for me.
            You can try the following code:

            funcprot(0);
            function [a0,A,B]=fourierplot(l,n,f)
                clf();
                a0=1/l*intg(-l,l,f,.000000001,0.00001);
                for i=1:n
                    function b=f1(x,f)
                        b=f(x)*cos(i*%pi*x/l);
                    endfunction
                    function c=f2(x,f)
                        c=f(x)*sin(i*%pi*x/l);
                    endfunction
                    A(i)=1/l*intg(-l,l,f1,.000000001,0.00001);
                    B(i)=1/l*intg(-l,l,f2,.000000001,0.00001);
                end
                function series=solution(x)
                    series=a0/2;
                    for i=1:n
                        series=series+A(i)*cos(i*%pi*x/l)+B(i)*sin(i*%pi*x/l);
                    end
                endfunction
                x=-5*l:0.1:5*l;
                plot(x,solution(x));
            endfunction
            
            
  2. what if the function has two or more values in the interval? For example a rectifier?

  3. i am still not getting the graph and solution. the code just gets repeated in the console

    1. Maybe the code isn’t compaitble with newer versions of Scilab, as it was written quite some time ago.

  4. function sm=simpson(a,b,m,g)
    //a=initial limit(real no.) ; b=final limit(real no.) ; n=no. of sub-intervals
    h=(b-a)/m;
    sum=0;
    for i=1:m-1
    x=a+ih;
    if modulo(i,2)==0
    sum=sum+2
    g(x);
    else
    sum=sum+4g(x);
    end
    end
    sm=(h/3)
    (g(a)+g(b)+sum);
    endfunction

    function [a0,A,B]=Fr(l,n,f)
    //l=period ; n=no. of Fourier coefficients ; f=function
    a0=1/lsimpson(-l,0,900,f)+1/lsimpson(0,l,900,f);
    for i=1:n
    function b=f1(x)
    b=f(x)cos(i%pix/l);
    endfunction
    function c=f2(x)
    c=f(x)
    sin(i%pix/l);
    endfunction
    A(i)=1/lsimpson(-l,0,900,f1)+1/lsimpson(0,l,900,f1);
    B(i)=1/lsimpson(-l,0,900,f2)+1/lsimpson(0,l,900,f2);
    end
    function series=solution(x)
    series=a0/2;
    for i=1:n
    series=series+A(i)cos(i%pix/l)+B(i)sin(i%pix/l);
    end
    endfunction
    x=0l:0.1:10l;
    plot(x,solution(x));
    endfunction

    // Define your function here
    function a = my_function(x)
    if x<0 then
    a=-1;
    else
    a=1;
    end
    endfunction
    c=input(“Enter the no. of coeff :”)
    // Compute Fourier coefficients of my_function with period 2*pi
    [a0, A, B] = Fr(2, c, my_function);
    disp(a0,A,B)

    can you check the code i made with simpson method

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.