https://www3.nd.edu/~nancy/Math30650/Matlab/Demos/fourier_series/fourier_series.html
>> syms x k L n
>> evalin(symengine,'assume(k,Type::Integer)');
>> a = @(f,x,k,L) int(f*cos(k*pi*x/L)/L,x,-L,L);
>> b = @(f,x,k,L) int(f*sin(k*pi*x/L)/L,x,-L,L);
>> fs = @(f,x,n,L) a(f,x,0,L)/2 + ...
symsum(a(f,x,k,L)*cos(k*pi*x/L) + b(f,x,k,L)*sin(k*pi*x/L),k,1,n);
>> f = abs(x)
f =
abs(x)
>> pretty(fs(f,x,10,1))
1 cos(3 pi x) 4 cos(5 pi x) 4 cos(7 pi x) 4 cos(9 pi x) 4 4 cos(pi x)
- - ------------- - ------------- - ------------- - ------------- - -----------
2 2 2 2 2 2
9 pi 25 pi 49 pi 81 pi pi
>> ezplot(fs(f,x,2,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=2')
Warning: MATLAB has disabled some advanced graphics rendering features by switching to software OpenGL.
For more information, click here.
>> ezplot(fs(f,x,5,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=5')
>> ezplot(fs(f,x,10,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=10')
Fourier Series
The Fourier series of a


where the Fourier coefficients



and

The nth partial sum of the Fourier series is

You can use the following commands to calculate the nth partial sum of the Fourier series of the expression f on the interval [-L,L]
syms x k L nThe next command tells MATLAB that k is an integer. That will allow simple and simplify to evaluate


evalin(symengine,'assume(k,Type::Integer)');
The kth Fourier cosine coefficient 
a = @(f,x,k,L) int(f*cos(k*pi*x/L)/L,x,-L,L);The kth Fourier sine coefficient

b = @(f,x,k,L) int(f*sin(k*pi*x/L)/L,x,-L,L);The nth partial sum is given by
fs = @(f,x,n,L) a(f,x,0,L)/2 + ...
symsum(a(f,x,k,L)*cos(k*pi*x/L) + b(f,x,k,L)*sin(k*pi*x/L),k,1,n);
For example, I can calculate the Fourier series of f(x) = |x| on the interval [-1,1].f = abs(x)
f = abs(x)The 10th partial sum is
pretty(fs(f,x,10,1))
4 cos(3 pi x) 4 cos(5 pi x) 4 cos(7 pi x) 4 cos(9 pi x) 4 cos(pi x) 1/2 - ------------- - ------------- - ------------- - ------------- - ----------- 2 2 2 2 2 9 pi 25 pi 49 pi 81 pi piWe can also have MATLAB calculuate the general Fourier coefficients. To do this and get MATLAB to simplify the results, we can use simple. The following command gives the kth Fourier cosine coefficient, suppressing the results of all of the steps of simple except for the simplest.
[A,how]=simple(a(f,x,k,1))
A = -(4*sin((pi*k)/2)^2)/(pi^2*k^2) how = simplifyIf I don't want to see how simple found the answer, I can suppress the output, then just display the simplified answer. The following command does that for the kth Fourier sine coefficient.
[B,how]=simple(b(f,x,k,1)); B
B = 0Here are the plots of the partial sums for n=2,5,10. The plot also shows the function f.
ezplot(fs(f,x,2,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=2')

ezplot(fs(f,x,5,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=5')

ezplot(fs(f,x,10,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=10')

f = x
f = xThe Fourier cosine coefficients are
[A,how]=simple(a(f,x,k,1)); A
A = 0The Fourier sine coefficients are
[B,how]=simple(b(f,x,k,1)); B
B = -(2*(-1)^k)/(pi*k)The 10th partial sum is
pretty(fs(f,x,10,1))
2 sin(pi x) sin(2 pi x) 2 sin(3 pi x) sin(4 pi x) 2 sin(5 pi x) sin(6 pi x) ----------- - ----------- + ------------- - ----------- + ------------- - ----------- + pi pi 3 pi 2 pi 5 pi 3 pi 2 sin(7 pi x) sin(8 pi x) 2 sin(9 pi x) sin(10 pi x) ------------- - ----------- + ------------- - ------------ 7 pi 4 pi 9 pi 5 piHere are plots of the partial sums for n=2,5,10,20,50.
ezplot(fs(f,x,2,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=2')

ezplot(fs(f,x,5,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=5')

ezplot(fs(f,x,10,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=10')

ezplot(fs(f,x,20,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=20')

ezplot(fs(f,x,50,1),-1,1) hold on ezplot(f,-1,1) hold off title('Partial sum with n=50')

g = inline(vectorize(fs(f,x,50,1))); X = -1:.001:1; plot(X,g(X),'r') hold on ezplot(f,-1,1) hold off title('Partial sum with n=50')

g = inline(vectorize(fs(f,x,100,1))); X = -1:.0001:1; plot(X,g(X),'r') hold on ezplot(f,-1,1) hold off title('Partial sum with n=100')

ezplot(fs(f,x,20,1),-2,2) hold on ezplot(f,-2,2) hold off



f = heaviside(x+3)*heaviside(-1-x)*(x+2) + heaviside(x+1)*heaviside(1-x)*x ...
+ heaviside(x-1)*heaviside(3-x)*(x-2);
extends f(x) = x to be periodic on [-3,3], with period 2. To check that we've extended it correctly, we plot it.ezplot(f,-3,3)
title('Periodic extension of x')

ezplot(fs(x,x,20,1),-3,3) hold on ezplot(f,-3,3) hold off title('Periodic extension of x and partial sum with n=20')

X = -3:0.001:3; g = inline(vectorize(fs(f,x,20,1))); ezplot(f,-3,3) hold on plot(X,g(X),'r') hold off title('Periodic extension of x and partial sum with n=20')

No hay comentarios:
Publicar un comentario