# fourier series of this function

1 view (last 30 days)
Commented: HADIMARGO on 28 Jun 2019
i want the fourier seris of function:
my code:
clear all
clc
syms x
f=(x+2).*(x>-2)-(x+2).*(x>-1)+(1).*(x<=1)-(1).*(x<=-1)+(2-x).*(x>1)-(2-x).*(x>=2);
N=10;
a_0=(1/pi)*int(f*cos(0*x),-pi,pi)
for n=1:N
a_n(n)=(1/pi)*int(f*cos(n*x),-pi,pi);
b_n(n)=(1/pi)*int(f*sin(n*x),-pi,pi);
end
a_n
b_n
f_new=a_0/2;
for n=1:N
f_new=f_new+a_n(n)*cos(n*x)+b_n(n)*sin(n*x);
end
subs(f_new,x,5)
subs(f,x,5)
the error:
Error using symengine
Invalid integrand.
Error in sym/int (line 162)
Error in Untitled3 (line 7)
a_0=(1/pi)*int(f*cos(0*x),-pi,pi)
my function:

infinity on 27 Jun 2019
Hello,
It is seen that the function "f" is not easy for Matlab to find it intergrand by using the fucntion "int". Maybe, you should try to split the piecewise function "f" into some small intervals, for example, [0, -2], [-2, -1], [-1, 1], .... Then, you can get the integral over these intervals.
i tryid but cannot find the correct code.