Section 12.1: The complex Fourier series
It is no more difficult to compute complex Fourier series than the real Fourier series discussed earlier. You should recall that the imaginary unit sqrt(-1) is denoted by i (or j, but I will use i) in MATLAB. As an example, I will compute the complex Fourier series of on the interval
clear
f=inline('x.^2','x');
syms x pi n
1/2*int(f(x)*exp(-i*pi*n*x),x,-1,1)
ans =
- (exp(pi*n*sqrt(-1))*(pi^2*n^2 + 2*pi*n*sqrt(-1) - 2)*sqrt(-1))/(2*pi^3*n^3) - ((1/exp(pi*n*sqrt(-1)))*(- pi^2*n^2 + 2*pi*n*sqrt(-1) + 2)*sqrt(-1))/(2*pi^3*n^3)
We now have a simplification problem similar to what we first encountered in Chapter 5, and for which I wrote the function mysubs. In this case, since n is an integer, exp(-iπn) equals (-1)n, as does exp(iπn). For this reason, I wrote another version of mysubs, namely mysubs1, to handle this case:
type mysubs1
function expr=mysubs1(expr,varargin)
%expr=mysubs1(expr,m,n,...)
%
% This function substitutes (-1)^m for exp(-i*pi*m) and
% for exp(i*pi*m), and similarly for exp(-i*pi*n) and
% exp(i*pi*n), and any other symbols given as inputs.
syms pi i
k=length(varargin);
for jj=1:k
expr=subs(expr,exp(-i*(pi*varargin{jj})),(-1)^varargin{jj});
expr=subs(expr,exp(i*pi*varargin{jj}),(-1)^varargin{jj});
end
expr=simplify(expr);
Now I apply mysubs1:
a=mysubs1(ans,n);
pretty(a)
n
2 (-1)
-------
2 2
pi n
This formula obviously does not hold for , and so I need to compute the coefficient a0 separately:
a0=1/2*int(f(x),x,-1,1)
a0 =
1/3
Now I can define the partial Fourier series. Recall from Section 9.1 of the text that, since f is real-valued, the following partial Fourier series is also real-valued (I choose to use 21 terms in the series):
S=a0+symsum(a*exp(i*n*pi*x),n,-10,-1)+symsum(a*exp(i*n*pi*x),n,1,10);
t=linspace(-1,1,101)';
plot(t,subs(S,x,t))
Warning: Imaginary parts of complex X and/or Y arguments ignored
The approximation is not bad, as the following graph of the error shows:
plot(t,f(t)-subs(S,x,t))
Warning: Imaginary parts of complex X and/or Y arguments ignored
You should notice the above warning messages complaining that the quantity I am trying to graph is complex. This is not important, because I know from the results in the text that the partial Fourier series must evaluate to a real number (since the underlying function f is real-valued). Therefore, any imaginary part must be due to roundoff error, and it is right to ignore it. Here is an example:
subs(S,x,1)
ans =
0.9614 + 0.0000i
imag(ans)
ans =
3.0815e-033
(The imag command extracts the imaginary part of a complex number.)
Share with your friends: |