-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathguide11.m
More file actions
483 lines (430 loc) · 18.5 KB
/
guide11.m
File metadata and controls
483 lines (430 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
%% 11. Periodic Chebfuns
% Grady B. Wright and Lloyd N. Trefethen, December 2014, latest revision July 2019
%% 11.1 Introduction
% One of the major new features introduced in
% Chebfun version 5 was the ability to
% use trigonometric functions instead of polynomials for
% representing smooth periodic functions
% [Wright et al. 2015]. These trig-based chebfuns, or
% "trigfuns", can be created with the use of
% the `'trig'` (or `'periodic'`) flag in the Chebfun constructor. For
% example, the function $f(t) = \tanh(3\sin t)-\sin(t+1/2)$ on
% $[-\pi,\pi]$ can be constructed and plotted as follows:
f = chebfun(@(t) tanh(3*sin(t))-sin(t+1/2),[-pi pi],'trig')
plot(f), grid on
%%
% The text `'trig'` in the display
% indicates that $f$ is represented by trigonometric functions,
% as discussed in the next section.
% For now we note that the length of 131 means that $f$ is resolved to
% machine precision using a trigonometric interpolant through 131 equally
% spaced samples over $[-\pi,\pi)$, or equivalently, 65
% trigonometric (= Fourier) modes.
%%
% In this chapter we review some of the functionality for
% trigfuns as well as some theory of trigonometric interpolation.
% For brevity, we refer to trigonometric-based chebfuns as
% _trigfuns_ and polynomial-based chebfuns as _chebfuns_.
%%
% Throughout our discussion, the trigfuns we construct live on
% the interval $[-\pi,\pi]$, which we specify explicitly in
% each call to the constructor. Mathematically, it might have made
% sense for this to be the default domain for trigfuns, but in
% Chebfun the factory default is always $[-1,1]$, whether the representation
% is trigonometric or not. It is possible to change the default
% domain to $[-\pi,\pi]$, and indeed to change the default
% function representation to trigonometric; see Section 8.2.
% To avoid confusion, however, we have not changed any defaults
% in this chapter.
%%
% For examples of Chebfun solution of periodic ODEs, see Chapter 7 and also
% Chapter 15 of [Trefethen, Birkisson & Driscoll 2018].
%%
% (_Editors' note._ Periodic chebfuns were created by Grady Wright
% of Boise State University during a visit to Oxford in 2014.
% Another MATLAB project based on trigonometric
% interpolants, called _Fourfun_, was developed independently
% by Kristyn McLeod under the supervision of Rodrigo Platte [McLeod 2014].)
%% 11.2 Trigonometric series and interpolants
% The classical trigonometric series of a periodic function
% $u$ defined on $[-\pi,\pi]$ is given as
% $$ \mathcal{F}[u] = \sum_{k=-\infty}^{\infty} a_k e^{ikt} $$
% with coefficients
% $$ a_k = \frac{1}{2\pi} \int_{-\pi}^{\pi} u(t)e^{-ikt} dt. $$
% Alternatively, we can express the series in terms of sines and cosines,
% $$ \mathcal{F}[u] = \sum_{k=0}^{\infty} a_k \cos(k t) +
% \sum_{k=1}^{\infty} b_k \sin(k t) $$
% with
% $$ a_k = \frac{1}{\pi} \int_{-\pi}^{\pi} f(t)\cos(kt) dt, \quad
% b_k = \frac{1}{\pi} \int_{-\pi}^{\pi} f(t)\sin(kt) dt, $$
% for $k>0$ and
% $$ a_0 = \frac{1}{2\pi} \int_{-\pi}^{\pi} f(t) dt. $$
% In what follows we will use the complex exponential form of the series.
%%
% Note that the first of these series
% is often referred to as the _Fourier series_ of
% $u$, but we will use the term _trigonometric series_ as advocated by
% Zygmund [Zygmund, 1959]. Similar expressions for the series and
% coefficients hold for intervals other than $[-\pi,\pi]$ by shifting and
% scaling appropriately.
%%
% Approximation theory results for trigonometric series are classic
% and can be found in, for example, [Zygmund, 1959]. Here we review some
% of these properties.
%%
% The convergence of the trigonometric series depends on the
% smoothness of $u$ on $[-\pi,\pi]$ and its periodic extension. Let $q_N$
% be the truncated series
% $$ q_N(t) = \sum_{k=-(N-1)/2}^{(N-1)/2} a_k e^{ikt} $$
% when $N$ is odd and
% $$ q_N(t) = \sum_{k=-N/2}^{N/2} a_k e^{ikt} $$
% when $N$ is even. If $u$ is periodic, continuous,
% and of bounded variation on
% $[-\pi,\pi]$, then as $N\rightarrow \infty$,
% $$ \| u - q_N \| \rightarrow 0, $$
% where $\|\cdot\|$ is the maximum norm. From our first series formula it is clear
% that the error is bounded by
% $$ \| u - q_N \| \leq \sum_{|k| > \lfloor N/2 \rfloor} |a_k|. $$
% The decay rate of the coefficients $a_k$ depends on the smoothness
% of $u$ and can be estimated by integration by parts. A classical
% result says that if $u$ is $(\ell-1)$-times continuously differentiable
% on $[-\pi,\pi]$, with each of these derivatives being periodic, and
% the derivative of order $\ell$ is of bounded variation on $[-\pi,\pi]$, then
% $$ |a_k| = O(|k|^{-\ell-1}),\; k=\pm 1, \pm 2,\dots. $$
% Adding up the tail, this gives us
% $$ \| u - q_N \| \leq O(N^{-\ell}) $$
% as $N\to\infty$, assuming $\ell\ge 1$. If $u$ and its periodic
% extension are $C^{\infty}$, then $q_N$ converges to $u$ faster than any
% inverse power of $N$. If $u$ is analytic on
% $[-\pi,\pi]$ then the convergence rate is exponential, i.e.,
% $\| u - q_N \| = O(b^N)$ for some $b < 1$.
%
% In general the coefficients $a_k$ are not known exactly.
% Let us imagine that we use the trapezoidal
% quadrature formula to approximate them [Trefethen & Weideman 2014].
% Defining the *trigonometric points* by
% $$ t_j = -\pi + 2\pi j/N, \qquad j=0,\ldots,N-1 $$
% (Chebfun: `trigpts(N,[-pi,pi])`) gives the approximation
% $$ a_k \approx c_k := \frac{1}{N} \sum_{j=0}^{N-1}
% u(t_j) e^{-i k t_j}. $$
% The coefficients $c_k$ are referred to as the _discrete Fourier coefficients,_ and when
% they are replaced by $a_k$ in the truncated trigonometric series $q_N$, the resulting
% series is called the _discrete Fourier series_ of $u$. The form of this series
% depends on the parity of $N$. For odd $N$ we have
% $$ p_N(t) = \sum_{k=-(N-1)/2}^{(N-1)/2} c_k e^{ikt}. $$
% When $N$ is even we have
% $$ p_N(t) = \sum_{k=-N/2+1}^{N/2-1} c_k e^{ikt}\,+
% c_{N/2} \cos(N/2 t), $$
% which is often rewritten as
% $$ p_N(t) = {\sum_{k=-N/2}^{N/2}} {}' c_k e^{ikt}, $$
% where $c_{-N/2} = c_{N/2}$ and the prime means the first and
% last terms in the sum are halved. The reason the $\sin(N/2 t)$ term is
% missing is that this function vanishes when evaluated at $t_j$, so that
% there is no contribution of this mode to the coefficient $c_{N/2}$.
%%
% The *degree* of a trigonometric sum is the degree of its
% highest order term. Thus our sums above for odd $N$ are of degree $(N-1)/2$,
% and those for even $N$ are of degree $N/2$.
%%
% The discrete Fourier series $p_N$ has the property that it
% interpolates $u$ at the gridpoints $t_j$.
% The coefficients $c_k$ can be computed in $O(N\log N)$ operations
% with the fast Fourier transform [Van Loan 1992], and the computation is
% numerically stable [Henrici 1986].
% The approximation properties of the interpolants are very similar to
% those of the truncated trigonometric series approximation of a function
% $u$; see e.g. [Canuto et al. 2006/7], [Hesthaven et al. 2007],
% [Trefethen 2000].
% Analysis of this relationship relies on the *Poisson summation formula*
% (or aliasing formula), which relates the interpolation coefficients
% $c_k$ to the trigonometric series coefficients $a_k$:
% $$ c_k = a_k + \sum_{m=1}^{\infty}
% \left(a_{k+m N} + a_{k-m N}\right). $$
% This formula implies that if $\ell\ge 1$, the decay rate of $c_k$ is
% essentially the same as that
% of $a_k$, with $c_k\to a_k$ as $N\to\infty$.
%%
% To illustrate some of these ideas numerically, consider $u(t) = |\sin t|^3$,
% a function with $\ell = 3$.
uu = @(t) abs(sin(t))^3;
u = chebfun(uu,[-pi,pi],'trig');
%%
% We can construct the truncated trigonometric series approximation with
% $N=11$ using the "trunc" option:
q11 = chebfun(uu,[-pi,pi],'trunc',11,'trig');
%%
% For comparison, here is the 11-point trigonometric interpolant:
p11 = chebfun(uu,[-pi,pi],11,'trig');
%%
% The error curves for the two approximations are similar:
plot([q11-u p11-u]), grid on
legend('projection error','interpolation error','location','southeast')
%%
% The difference between truncation of a trigonometric series
% and trigonometric interpolation, while interesting mathematically,
% is rarely very important in practical computation.
%% 11.3 Basic operations
% Most computations with trigfuns follow the same pattern as with
% chebfuns. Typically the result will be a trigfun too.
% Here for example we construct an initial trigfun $g$ and
% then transform it to a new trigfun $f$.
g = chebfun(@(t) sin(t),[-pi pi],'trig');
f = tanh(cos(1+2*g)^2) +g/3 - 0.5
%%
% The operations $+$, $*$, and so on are all carried out by appropriate
% manipulation of trigonometric representations.
% Here we compute and plot the maximum, minimum, and roots of $f$:
[maxval, maxpos] = max(f);
[minval, minpos] = min(f);
r = roots(f);
plot(f), grid on, hold on
plot(r,f(r),'.r','markersize',14)
plot(maxpos,maxval,'ok')
plot(minpos,minval,'ok')
%%
% The derivative of a trigfun can be computed with `diff`, and the
% definite integral with `sum`:
sum(f)
%%
% Other operations should generally proceed as expected. Sometimes,
% if an operation breaks the smoothness needed for
% a trig representation, the result of a
% trigfun computation will be a chebfun.
% For example, the absolute value of the function
% above is a chebfun, not a trigfun:
g = abs(f);
plot(g), grid on
hold off
%%
% If you combine a trigfun and a chebfun, the result will be a chebfun.
%% 11.4 Complex-valued functions and contour integrals
% Like other chebfuns, trigfuns can take complex values, and this feature
% is especially useful for the computation of contour integrals over
% smooth contours in the complex plane, such as circles,
% as was highlighted in Section 5.3.
% We recommend trigfuns for the computation of most contour integrals.
%%
% A pretty example of a periodic complex-valued chebfun
% can be generated with the commands
cheb.gallerytrig('starburst')
%% 11.5 Circular convolution
% The circular or periodic convolution of two functions
% $f$ and $g$ with period $T$ is defined by
% $$ (f*g)(t) := \int_{t_0}^{t_0 + T} g(s)f(t-s)ds $$
% where $t_0$ is arbitrary. Circular convolutions can be
% computed for trigfuns with the `circconv` function.
% For example, here is a trigonometric interpolant through
% $201$ samples of a smooth function plus noise (available
% via `cheb.gallerytrig('noisyfun')`):
rng('default'), rng(0);
n = 201;
tt = trigpts(n,[-pi pi]);
ff = exp(sin(tt)) + 0.05*randn(n,1);
f = chebfun(ff,[-pi pi],'trig')
plot(f), grid on
%%
% The high frequencies
% can be smoothed by convolving $f$ with a mollifier such as
% a Gaussian.
gaussian = @(t,sigma) 1/(sigma*sqrt(2*pi))*exp(-0.5*(t/sigma)^2);
g = @(sigma) chebfun(@(t) gaussian(t,sigma),[-pi pi],'trig');
%%
% This function is numerically periodic for $\sigma\le 0.35$.
% Here we take $\sigma = 0.1$ and
% superimpose the smoothed curve on top of the noisy one:
h = circconv(f,g(0.1));
hold on, plot(h), hold off
%% 11.6 Trigfuns vs. chebfuns
% Trigonometric interpolants have a resolution power of 2 points per
% wavelength, whereas Chebyshev interpolants require approximately $\pi$
% points per wavelength (averaged over the grid).
% This means that smooth periodic functions can
% usually be represented as trigfuns using fewer samples
% than standard chebfuns.
%%
% To illustrate this, consider the chebfun and trigfun representations of
% $f(t) = \cos(11\sin(3(t-1/\pi)))$ over $[-\pi,\pi]$:
ff = @(t) cos(11*sin(3*(t-1/pi)));
f_cheb = chebfun(ff,[-pi pi]);
f_trig = chebfun(ff,[-pi pi], 'trig');
%%
% The ratio of lengths of the two representations should be approximately
% $\pi/2$, and this is indeed what we find:
ratio = length(f_cheb)/length(f_trig)
%%
% Another advantage of trigonometric representations appears
% in the computation of derivatives.
% For example, consider the function $\cos(10\sin t)$,
f = chebfun(@(t) cos(10*sin(t)),[-pi pi],'trig');
%%
% All odd derivatives of $f$ vanish at $\pm \pi$. Here is what
% the trigfun finds for $f'''(\pi)$:
df3 = diff(f,3);
df3(pi)
%%
% This is essentially full precision since the scale of
% $f'''$ is about $1000$.
% By contrast, we lose six digits of accuracy if we
% use a non-trigonometric chebfun:
f_cheb = chebfun(@(t) cos(10*sin(t)),[-pi pi]);
df3_cheb = diff(f_cheb,3);
df3_cheb(pi)
%%
% Trying to construct a trigfun from a function
% that is not smoothly periodic will
% typically result in a warning, as illustrated by
% this result for $t^2$:
f = chebfun(@(t) t^2,[-pi pi],'trig')
%%
% In such a case one should usually switch to
% a non-trigonometric representation.
%% 11.7 Trigonometric coefficients
% Trigfuns provide an easy tool for computing Fourier coefficients via
% the command `trigcoeffs`.
% (See the end of this section for an important fine
% point concerning $[-\pi,\pi]$ vs. $[0,2\pi]$.)
% Here as an example is $u(t) = 1 - 4\cos t + 6\sin(2t)$:
u = chebfun(@(t) 1-4*cos(t)+6*sin(2*t),[-pi pi],'trig');
trigcoeffs(u)
%%
% Note that in this default mode, `trigcoeffs` returns the coefficients
% in complex exponential form,
% from lowest degree to highest. The equivalent coefficients in
% terms of cosines and sines can be obtained by
% specifying two output arguments:
[a,b] = trigcoeffs(u);
%%
% The first output variable contains the coefficients of
% $1, \cos t, \cos 2t, \dots$:
a
%%
% The second contains the coefficients of $\sin t, \sin 2t, \dots$:
b
%%
% Coefficients of trigfuns (their absolute values)
% can be plotted with `plotcoeffs`.
% For the entire function $\exp(\sin t)$ (i.e., analytic throughout
% the complex plane), the coefficients decrease faster than geometrically:
f = chebfun('exp(sin(t))',[-pi pi],'trig');
plotcoeffs(f)
%%
% For $f(t) = 1/(2-\cos t)$, which is analytic
% on $[-\pi,\pi]$ but not entire, the decrease is perfectly geometric:
f = chebfun('1/(2-cos(t))',[-pi pi],'trig');
plotcoeffs(f)
%%
% A function with a finite number of derivatives gives
% algebraic decay:
f = chebfun('abs(sin(x))^5',[-pi pi],'trig');
plotcoeffs(f)
%%
% The `loglog` option enables one more easily
% to quantify the decay rate (showing the
% coefficients of index $k>0$). This function has $\ell =5$ for
% the estimates above, which imply that the decay rate of coefficients
% is $a_k = O(|k|^{-6})$.
plotcoeffs(f,'loglog')
hold on, loglog(3*[3 300],[3 300].^-6,'--r'), hold off
text(110,4e-9,'k^{-6}','color','r')
%%
% At the beginning of this section, we alluded to
% an important fine point concerning
% $[-\pi,\pi]$ vs. $[0,2\pi]$ --- or more generally, the
% transplantation from one domain $[a,b]$ to another.
% We now explain this.
%%
% Consider, say, the function $f(x) = \cos x = (e^{ix}+e^{-ix})/2$.
% Obviously its Fourier coefficients in the exponential basis
% are $1/2, 0, 1/2$:
f = chebfun(@cos,[-pi,pi],'trig'); trigcoeffs(f)
%%
% Naturally we expect the same result on $[0,2\pi]$:
f = chebfun(@cos,[0,2*pi],'trig'); trigcoeffs(f)
%%
% In fact, we expect these coefficients on any interval of
% length $2\pi$:
f = chebfun(@cos,[7,7+2*pi],'trig'); trigcoeffs(f)
%%
% This all seems so natural that one can easily overlook
% that something of substance is going on. To work on an
% interval like $[7,7+2\pi]$, Chebfun first transplants
% the problem to $[-\pi,\pi]$. The transplanted
% function is not $\cos x$ at all --- it is $\cos(x+8)$.
% Now $\cos(x+8)$ has a perfectly good Fourier expansion, but
% the last thing a user would expect `trigcoeffs` to return would
% be those coefficients. We can say it precisely like this:
% the coefficients returned by `trigcoeffs` for expansion on
% any interval $[a,b]$ correspond to the basis
% $\{\exp(i\alpha k x)\}$ with $\alpha = 2\pi/(b-a)$, _not_
% to the basis $\{\exp(i\alpha k (x- (b+a)/2))\}$.
%%
% Note that this is a point where the analogy between
% trigfuns and chebfuns breaks down.
% If you call `chebcoeffs(f)` for a chebfun on $[a,b]$, the resulting
% coefficients correspond to expansion in Chebyshev polynomials
% of the transplant of $f$.
% If you call `trigcoeffs(f)` for a trigfun on $[a,b]$, the resulting
% coefficients correspond to expansion in complex exponentials that
% are _not_ transplanted to $[-\pi,\pi]$. If you truly want
% the latter coefficients, at the time of this writing you
% can get them with `f.coeffs`, though we are not sure if this may
% change in the future.
%% 11.8 Truncated trigonometric series approximations
% The `trigcoeffs` function can also be used to compute a prescribed
% number of trigonometric coefficients of a function that
% may not be smooth enough for resolution to
% machine precision; this is done by accurate numerical evaluation of
% the integral defining $a_k$. For example, here is a non-smooth function,
% a square wave, represented as a chebfun.
% Superimposed on the square wave is its approximation by
% a trigonometric sum of degree $15$:
sq_wave = @(t) sign(sin((t)));
u = chebfun(sq_wave,[-pi pi],'splitting','on');
plot(u), grid on, ylim([-1.5 1.5])
degree = 15;
a = trigcoeffs(u,2*degree+1);
u_trunc = chebfun(a,[-pi pi],'trig','coeffs');
hold on, plot(u_trunc), hold off
%%
% This represents the best degree 15
% trigonometric approximation to the square
% wave over $[-\pi,\pi]$ in the $L^2$ sense. The oscillations
% show the famous Gibbs phenomenon.
%% 11.9 References
%
% [Austin, Kravanja & Trefethen 2014] A. P. Austin, P. Kravanja and
% L. N. Trefethen, "Numerical algorithms based on analytic function
% values at roots of unity", _SIAM J. Numer. Anal._ 52 (2014),
% 1795--1821.
%
% [Canuto et al. 2006/7] C. Canuto, M. Y. Hussaini, A. Quarteroni and T. A.
% Zang, _Spectral Methods_, 2 vols., Springer, 2006 and 2007.
%
% [Henrici 1986] P. Henrici, _Applied and Computational Complex Analysis_,
% vol. 3, Wiley, 1986.
%
% [Hesthaven et al. 2007] J. S. Hesthaven, S. Gottlieb, and D. Gottlieb,
% _Spectral Methods for Time-Dependent Problems_, Cambridge U. Press, 2007.
%
% [McLeod 2014] K. McLeod, "Fourfun: A new system for
% automatic computations using Fourier expansions", submitted, 2014.
% See also http://uk.mathworks.com/matlabcentral/fileexchange/46999-fourfun.
%
% [Trefethen 2000] L. N. Trefethen, _Spectral Methods in MATLAB_, SIAM, 2000.
%
% [Trefethen, Birkisson & Driscoll 2018] L. N. Trefethen, A. Birkisson,
% and T. A. Driscoll, _Exploring ODEs_, SIAM, 2018; freely available at
% http://people.maths.ox.ac.uk/trefethen/ExplODE/.
%
% [Trefethen & Weideman 2014] L. N. Trefethen and J. A. C. Weideman,
% "The exponentially convergent trapezoidal rule",
% _SIAM Review_ 56 (2014), 385-458.
%
% [Van Loan 1992] C. Van Loan, _Computational Frameworks for the Fast
% Fourier Transform_, SIAM, 1992.
%
% [Wright et al. 2015] G. B. Wright, M. Javed, H. Montanelli, and L. N.
% Trefethen, Extension of Chebfun to periodic functions,
% _SIAM J. Sci. Comp._ 37 (2015), C554--C573.
%
% [Zygmund 1959] A. Zygmund, _Trigonometric Series_, Cambridge U. Press, 1959.