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

*. 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*

**‘fourierplot’***. And finally I have plotted the Fourier Series.*

**‘solution’****Sample Demo:**

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

**Output:**

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.

Ph.D. researcher at Friedrich-Schiller University Jena, Germany. I’m a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT, and Machine Learning related apps and software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.

gj sir

It’s showing round off error. How to remove that?

I have taken 0.00000000001

Hi there,

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

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..

Which version of Scilab are you using?

6.0.0

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:

Ok

Thank you so much

meaning of funcprot(0)

what if the function has two or more values in the interval? For example a rectifier?

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

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

I AM STILL NOT GETTING THE GRAPH

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+i

h;g(x);if modulo(i,2)==0

sum=sum+2

else

sum=sum+4

g(x);(g(a)+g(b)+sum);end

end

sm=(h/3)

endfunction

function [a0,A,B]=Fr(l,n,f)

//l=period ; n=no. of Fourier coefficients ; f=function

a0=1/l

simpson(-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);sin(iendfunction

function c=f2(x)

c=f(x)

%pix/l);endfunction

A(i)=1/l

simpson(-l,0,900,f1)+1/lsimpson(0,l,900,f1);B(i)=1/l

simpson(-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=0

l: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