ex9: start, finish, and everything in between

This commit is contained in:
2025-10-22 16:25:33 +02:00
parent 3d270fc245
commit 61fdb082fb
7 changed files with 694 additions and 0 deletions

60
exercise9/lib.typ Normal file
View File

@@ -0,0 +1,60 @@
#let title(cont, size: 18pt) = align(center)[
#text(size: size * 2, weight: "bold")[#underline[#cont]]
]
// writes a butcher tableau using the minimum required amount of numbers.
#let butcher(s, nums) = {
let nums = nums.map(x => if type(x) == float or type(x) == int {
$#x$
} else { x })
table(
stroke: (x, y) => if x == 0 and y == s {
(right: 0.7pt + black, top: 0.7pt + black)
} else if x == 0 {
(right: 0.7pt + black)
} else if y == s {
(top: 0.7pt + black)
},
align: (x, y) => (
if x > 0 { center } else { left }
),
columns: s + 1,
$0$, ..range(s).map(x => none), // first row
..range(2, s + 1)
.map(i => {
let p(i) = calc.floor(i * i / 2 + i / 2 - 1)
(
nums.slice(p(i - 1), p(i - 1) + i),
range(i, s + 1).map(x => none),
).flatten()
})
.flatten(),
none, ..nums.rev().slice(0, s).rev() // last row
)
}
// automatically adds aligned "if" and "otherwise" strings to a case block.
//
// example:
// ```
// $ccases(x, x > 0, -x, x <= 0)$
// // is the same as
// $cases(x & quad "if" space x > 0, -x & quad "if" space x <= 0$
//
// $ccases(x, x > 0, -x)$
// // is the same as
// $cases(x & quad "if" space x > 0, -x & quad "otherwise"$
// ```
#let ccases(..args) = {
let parsed = args.pos().map(x => if type(x) == content { x } else { $#x$ })
let result_array = parsed
.windows(2)
.enumerate()
.filter(x => calc.rem-euclid(x.first(), 2) == 0)
.map(array.last)
.map(x => x.intersperse($& quad "if" space$).join())
if calc.rem-euclid(parsed.len(), 2) != 0 {
result_array.push($#parsed.last() & quad "otherwise"$)
}
math.cases(..result_array)
};

BIN
exercise9/main.pdf Normal file

Binary file not shown.

43
exercise9/main.typ Normal file
View File

@@ -0,0 +1,43 @@
#import "lib.typ"
#let FONT_SIZE = 18pt;
#set page(paper: "a4", margin: (x: 2.6cm, y: 2.8cm), numbering: "1 : 1")
#set par(justify: true, leading: 0.52em)
#set text(font: "FreeSerif", size: FONT_SIZE, lang: "us")
#show math.equation: set text(
font: "Euler Math",
size: (FONT_SIZE * 1.0),
lang: "en",
)
#set heading(numbering: none)
#show heading.where(level: 1): it => {
rect(inset: FONT_SIZE / 2)[#it]
}
#show ref: it => if it.element.func() != heading { it } else {
link(it.target, it.element.body)
}
#lib.title(size: FONT_SIZE)[exercise 9]
these are my solutions to the ninth exercise set of TMA4135.
this document was created using
#link("https://typst.app/")[#text(blue.darken(5%))[typst]].
\
#outline(title: none)
#pagebreak()
#include "problem1.typ"
#include "problem2.typ"
#include "problem3.typ"
#include "problem4.typ"

114
exercise9/problem1.typ Normal file
View File

@@ -0,0 +1,114 @@
#import "@preview/physica:0.9.6": *
= problem 1
recall the definition of a periodic function $f$ for a $p > 0$
$
f(x + p) = f(x) quad forall quad x in RR.
$
the smallest such $p$ is called the fundamental period of $f$.
== a)
"every periodic function has a fundamental period" is a false statement.
examine $f(x) = 1,$ which is periodic since given a $p = 1$, then $f(x + 1)
= f(x) = 1$ for all $x in RR$. it is trivially periodic. however, there is no
smallest $p$ for which this holds:
given a $delta > 0$ there is always a $hat(delta) > 0$ for which $hat(delta)
< delta$ holds. this is a property of the real number line, thus there is no
fundamental period for this periodic function.
== b)
+ "$"Per"_p := {f: RR -> RR | f #[is $p$-periodic]}$ is a vector space" is
a true statement since all periodic functions that are added together or
scaled by some scalar are still periodic. this comes from the linear property
of periodic functions.\
we can prove this by taking two periodic functions $f$ and $g$ and seeing if
their linear combination is an element of the space
$
h(t) & := a dot f(t) + b dot g(t) \
& = a dot f(t + p_f) + b dot g(t + p_g) \
& = a dot f(t + p_f dot p_g) + b dot g(t + p_g dot p_f) \
& = h(t + p_f dot p_g) = h(t + p_h)
$
thus the linear combination $h(t)$ must be periodic itself and therefore an
element of $"Per"_p$. this proves scalar multiplication and vector addition
axioms, rest is trivial.
+ "let $phi.alt : RR -> RR => phi.alt compose f$ is $p$-periodic" is true, since
it 'captures' the input of the function $phi.alt$ such that it becomes
periodic itself, always reiterating over the same values. \
$
(phi.alt compose f)(t) & = phi.alt(f(t)) \
& = phi.alt(f(t + p)) \
& = (phi compose f)(t + p)
$
+ "let $n in NN, a in RR => f(x + a), f(n x), f(x slash n)$ are $p$-periodic" is
false, since scaling the input parameter changes the period. \
$
(1) &quad f(x + a) = f((x + p) + a) = f((x + a) + p)) #underline[ok] \
(2) &quad f(n x) = f(n (x + p)) = f(n x + n p) = f(n x + p) #underline[ok] \
(3) &quad f(x slash n) = f((x + p) slash n) = f(x slash n + p slash n) #underline[not ok]
$
(2) works because $n in NN$, such that the period repeats $n$ times. (3)
doesn't work because dividing by a natural number causes the period to
contract and thus isn't $p$-periodic anymore.
+ "the absolute difference between two periods $p$ and $p'$ is also a period of
$f$" is true, since it just means that the periods occur periodically. \
$
f(x) & = f(x + p') = f(x - p') \
& = f((x + p) - p') = f(x + (p - p'))
$
given that $p > p'$.
+ "let $a, b in RR => integral_a^(a + p) f(x) dd(x) = integral_b^(b + p) f(x) dd(x)$"
is true, since it says that integrating over the period is the same regardless
of where you start integrating from. \
we can choose a $c in RR$ such that
$
integral_a^(a + p) f(x) dd(x)
& = integral_a^c f(x) dd(x) + integral_c^(a + p) f(x) dd(x) \
& = integral_c^(a + p) f(x) dd(x) + integral_a^c f(x) dd(x)
= integral_b^(b + p) f(x) dd(x)
$
because $c$ is a midpoint chosen to displace the integral sum such that the
startpoints of the integration are the same in terms of the period of the
function $f$.
+ "if $f$ is differentiable, $f'$ is also $p$-periodic" is true \
$
f(x) = f(x + p) ==> f'(x) = f'(x + p)
$
== c)
- $f(x) = cos(2x + 3)$ has a fundamental period $p = pi$ since the $+ 3$ doesn't
affect the period and then the usual period of $2 pi$ is halved by the
coefficient in-front of $x$.
- $f(x) = pi sin(3/2 pi x)$ has a fundamental period $p = 4 slash 3$ since the
$pi$ in-front of the $sin$-expression only affects the amplitude and $(2 pi)
/ ((3 slash 2) pi) = 4 slash 3$.
- $f(x) = cos(pi/(m+1) x) + sin(pi/(n-1) x)$ for $m in ZZ \\ {-1}, n in ZZ \\ {1}$
can be broken into two functions that can be analyzed separately first.
- $g(x) := cos(pi/(m + 1) x)$ has a fundamental period $p_g = 2(m + 1)$.
- $h(x) := sin(pi/(n + 1) x)$ has a fundamental period $f_h = 2(n + 1)$.
- we can combine these two to obtain $f(x) = g(x) + h(x)$.
to find the fundamental period, we can draw inspiration from number theory to
see that the combined period must be
$
gcd(p_g, f_H) & = gcd(2(m + 1), 2(n + 1)) \
& = gcd(m + 1, n + 1) \
& <= (m + 1)(n + 1) \
& = m n + m + n + 1
$
but we cannot shorten this further using $p$-period algebra, so this must be
the fundamental period for this wave.

62
exercise9/problem2.typ Normal file
View File

@@ -0,0 +1,62 @@
#import "@preview/physica:0.9.6": *
= problem 2
== a)
$h(x) := f(x) g(x)$ is odd for odd $f$ and even $g$, since
$
h(x) = -f(-x) g(-x) = - (f(-x) g(-x)) = -h(-x)
$
== b)
if $f$ and $g$ are both odd or even, then $h(x) := f(x) g(x)$ is even
1. for the first case: $f$ and $g$ are both even
$
h(x) = f(x) g(x) = f(-x) g(-x) = h(-x)
$
2. for the second case: $f$ and $g$ are both odd
$
h(x) & = f(x) g(x) \
& = (-f(-x)) (-g(-x)) = f(-x) g(-x) = h(-x)
$
== c)
if $f$ is odd and $g$ is even then both $f compose g$ and $g compose f$ are
even, since
1. $f(g(x)) = f(g(-x))$, since g is even
2. $g(f(x)) = g(-f(-x)) = g(f(-x))$
== d)
if $f$ is odd and $L > 0$,
$
integral_(-L)^L f(x) dd(x) & = integral_(-L)^0 f(x) dd(x)
+ integral_0^L f(x) dd(x) \
& = integral_0^L f(-x) (-1) dd(x)
+ integral_0^L f(x) dd(x) \
& = -integral_0^L f(-x) dd(x)
+ integral_0^L f(x) dd(x) \
& = -integral_0^L f(x) dd(x)
+ integral_0^L f(x) dd(x) \
& = 0
$
== e)
if $f$ is even and $L > 0$,
$
integral_(-L)^L f(x) dd(x) & = integral_(-L)^0 f(x) dd(x)
+ integral_0^L f(x) dd(x) \
& = integral_0^L f(-x) dd(x)
+ integral_0^L f(x) dd(x) \
& = integral_0^L f(x) dd(x)
+ integral_0^L f(x) dd(x) \
& = 2 integral_0^L f(x) dd(x)
$

91
exercise9/problem3.typ Normal file
View File

@@ -0,0 +1,91 @@
#import "@preview/physica:0.9.6": *
= problem 3
$
a_0(f') = 0, quad a_n (f') = n b_n (f), quad b_n(f') = -n a_n(f)
$
if $f(-pi) != f(pi)$ then the formulas don't work anymore, since there will be
a discreet jump every time the period is restarted. to fix the formulas we need
to account for this. let $[f] = f(pi) - f(-pi)$
$
cases(
a_0 (f') & = 1/(2 pi) integral_(-pi)^pi f'(x) dd(x)
= 1/(2 pi) [f(x)]_(-pi)^pi = [f]/(2 pi), ,
a_n (f') & = 1/pi integral_(-pi)^pi f'(x) cos(n x) dd(x) \
& = 1/pi ([f(x) cos(n x)]_(-pi)^pi
+ n integral_(-pi)^pi f(x) sin(n x) dd(x)) \
& = 1/pi ((-1)^n [f] + n pi b_n (f)) \
& = n b_n (f) + 1/pi (-1)^n [f], ,
b_n (f') & = - n a_n (f)
)
$
recall the fourier series for a $p$-periodic function $f$
$
f(x) approx f_N (x) = a_0 + sum_(n=1)^N
(a_n cos((2 pi)/p x) + b_n sin((2 pi)/p x))
$
where
$
cases(
a_0 = 1/p integral_p f(x) dd(x),
a_n = 2/p integral_p f(x) cos((2 pi)/p n x) dd(x),
b_n = 2/p integral_p f(x) sin((2 pi)/p n x) dd(x)
)
$
let $f(x) = sin^2(x) + 3 x^2 - 4 x + 5$ be periodically continued based on
$[-pi, pi]$.
notice that this series has a jump $[f] > 0$ so we need to use the modified
formulas we found above.
also remark that $sin^2(x) = (1 - cos(2x))/2$, which is already its fourier
series.
since fourier series are linear, we can compute the fourier series of each
term and we can use the provided properties to compute the coefficients using
the derivative of $f$.
$
f'(x) = dots.c + 6 x - 4
$
we then need to compute the fourier series for $x^2$, $x$ and $1$ using
$
"fourier"(1) & = cases(
a_0 (1) = 1/(2 pi) integral_(-pi)^pi 1 dd(x) = 1,
a_n (1) = 1/pi integral_(-pi)^pi cos(x) dd(x) = 0,
b_n (1) = 0
) \
"fourier"(x) & = cases(
a_0 (x) = 1/(2 pi) integral_(-pi)^pi x dd(x) = 0,
a_n (x) = 1/n (b_n (1)) = 0,
b_n (x) = 1/n (a_n (1) - 1/pi (-1)^n [x])
= 2/n (-1)^(n + 1)
) \
"fourier"(x^2) & = cases(
a_0 (x^2) = 1/(2 pi) integral_(-pi)^pi x^2 dd(x)
= 1/(6 pi) [x^3]_(-pi)^pi = 1/3 pi^2,
a_n (x^2) = -1/n b_n (2 x)
= -2/n b_n (x) = 4/n^2 (-1)^n,
b_n (x^2) = 1/n (a_n (2 x) - 1/pi (-1)^n [x^2]) = 0
)
$
combining these we get
$
"fourier"(f(x)) & = "fourier"(sin^2(x)) \
& + "fourier"(3 x^2) - "fourier"(4 x) + "fourier"(5) \
& = 1/2 (1 - cos(2 x)) + 3 "fourier"(x^2) \
& - 4 "fourier"(x) + 5 "fourier"(1) \
& = 1/2 (1 - cos(2 x)) x + cases(
a_0 (f(x)) & = 3 a_0 (x^2) - 4 a_0 (x) + 5 a_0 (1) \
& = pi^2 + 5,
a_n (f(x)) & = 12/n^2 (-1)^n,
b_n (f(x)) & = -8/n (-1)^(n+1) = 8/n (-1)^n
) \
& = pi^2 + 11/2 - 1/2 cos(2 x) \
& + sum_(n=1)^oo (12/n cos(n x)
+ 8 sin(n x))(-1)^n / n
$

324
exercise9/problem4.typ Normal file
View File

@@ -0,0 +1,324 @@
#import "@preview/physica:0.9.6": *
#import "lib.typ": ccases
#import "@preview/lilaq:0.5.0" as lq
= problem 4
== a)
to find the fourier series of
$
f(x) = ccases(
0, -pi < x < 0 "or" pi/2 < x <= pi,
x, 0 <= x <= pi/2,
)
$
we can use the formulae for the coefficients
$
a_0 & = 1/pi integral_(-pi)^pi f(x) dd(x) \
& = 1/pi integral_0^(pi slash 2) x dd(x) \
& = 1/(2 pi) [x^2]_0^(pi slash 2) \
& = pi/8
$
$
a_n & = 1/pi integral_(-pi)^pi f(x) cos(n x) dd(x) \
& = 1/pi integral_0^(pi slash 2) x cos(n x) dd(x) \
& = 1/pi [x/n sin(n x) + 1/n^2 cos(n x)]_0^(pi slash 2) \
& = 1/(2 n) sin(pi/2 n) + 1/(pi n^2) (cos (pi/2 n) - 1)
$
then for the different cases of $n mod 4$
$
a_n = ccases(
0, n equiv 0,
1/(2 n) - 1/(pi n^2), n equiv 1,
-2/(pi n^2), n equiv 2,
-1/(2 n) - 1/(pi n^2), n equiv 3,
)
$
then for the $sin$ terms
$
b_n & = 1/pi integral_(-pi)^pi f(x) sin(n x) dd(x) \
& = 1/pi integral_0^(pi slash 2) x sin(n x) dd(x) \
& = 1/pi [1/n^2 sin(n x) - x/n cos(n x)]_0^(pi slash 2) \
& = 1/(pi n^2) sin(pi/2 n) - 1/(2 n) cos(pi/2 n)
$
then similar $n mod 4$ computations
$
b_n = ccases(
-1/(2 n), n equiv 0,
1/(pi n^2), n equiv 1,
1/(2 n), n equiv 2,
-1/(pi n^2), n equiv 3
)
$
then putting it all together
$
f(x) = a_0/2 + sum_(n=1)^oo a_n cos(n x) + sum_(n=1)^oo b_n sin (n x)
$
which is our final fourier series for $f(x)$.
#{
let a_0 = calc.pi / 8
let a(n) = {
if calc.rem-euclid(n, 4) == 0 {
0
} else if calc.rem-euclid(n, 4) == 1 {
1 / (2 * n) - 1 / (calc.pi * n * n)
} else if calc.rem-euclid(n, 4) == 2 {
-2 / (calc.pi * n * n)
} else if calc.rem-euclid(n, 4) == 3 {
-1 / (2 * n) - 1 / (calc.pi * n * n)
}
}
let b(n) = {
if calc.rem-euclid(n, 4) == 0 {
-1 / (2 * n)
} else if calc.rem-euclid(n, 4) == 1 {
1 / (calc.pi * n * n)
} else if calc.rem-euclid(n, 4) == 2 {
1 / (2 * n)
} else if calc.rem-euclid(n, 4) == 3 {
-1 / (calc.pi * n * n)
}
}
let fourier(x, n: 50) = (
a_0 / 2
+ range(1, n).map(n => a(n) * calc.cos(n * x)).sum()
+ range(1, n).map(n => b(n) * calc.sin(n * x)).sum()
)
let f(x) = {
let t = calc.rem-euclid(x, 2 * calc.pi)
if 0 <= t and t <= calc.pi / 2 { t } else { 0 }
}
let xs = lq.linspace(-3 * calc.pi, 3 * calc.pi, num: 100)
let configs = (
(n: 5),
(n: 20),
(n: 100),
)
for config in configs {
lq.diagram(
title: [the fourier series of $f(x)$ with $N = #config.n$],
xlabel: $x$,
ylabel: $y$,
width: 100%,
height: 32%,
xlim: (-3 * calc.pi, 3 * calc.pi),
xaxis: (
locate-ticks: lq.locate-ticks-linear.with(unit: calc.pi),
format-ticks: lq.format-ticks-linear.with(suffix: $pi$),
),
lq.plot(xs, x => fourier(x, n: config.n), mark: none),
lq.plot(xs, f, mark: none),
)
}
}
== b)
let
$
f(x) = ccases(
0, -pi < x < 0,
x, 0 < x < pi/2,
pi - x, pi/2 < x <= pi
)
$
then we can proceed as usual to calculate the coefficients of the fourier
series, utilising symmetries of $x$ and $pi - x$ on the given intervals.
$
a_0 & = 1/pi integral_(-pi)^pi f(x) dd(x) \
& = 1/pi (integral_0^(pi slash 2) x dd(x)
+ integral_(pi slash 2)^pi (pi - x) dd(x)) \
& = 2/pi integral_0^(pi slash 2) x dd(x) \
& = 1/pi [x^2]_0^(pi slash 2) \
& = pi / 4
$
$
a_n & = 1/pi integral_(-pi)^pi f(x) cos(n x) dd(x) \
& = 1/pi (integral_0^(pi slash 2) x cos(n x) dd(x)
+ integral_(pi slash 2)^pi (pi - x) cos(n x) dd(x)) \
& = 1/pi ([x/n sin(n x) + 1/n^2 cos(n x)]_0^(pi slash 2) \
& + [(pi-x)/n sin(n x) - 1/n^2 cos(n x)]_(pi slash 2)^pi) \
& = 1/pi ([cancel(pi/(2 n) sin(pi/2 n)) + 1/n^2 cos(pi/2 n) - 1/n^2] \
& + [1/n^2 (-1)^(n+1) - cancel(pi/(2 n) sin(pi/2 n)) + 1/n^2 cos(pi/2 n)]) \
& = 1/(pi n^2) (2 cos(pi/2 n) - 1 + (-1)^(n + 1))
$
notice that for odd $n$, $a_n = 0$, since the cosine is zero and the alternating
sign is fixed to 1, canceling with the constant term. thus we can shorten it
further with a definition for $n = 2 k$
$
a_n & = 1/(4 pi k^2) (2 cos(pi k) - 1 + (-1)^(2k + 1)) \
& = 1/(2 pi k^2) ((-1)^k - 1) \
& = 2/(pi n^2) ((-1)^(n slash 2) - 1)
$
similarly $b_n$, we can recognize the symmetry earlier in our calculations
$
b_n & = 1/pi integral_(-pi)^pi f(x) sin(n x) dd(x) \
& = 1/pi (integral_0^(pi slash 2) x sin(n x) dd(x)
+ integral_(pi slash 2)^pi (pi - x) sin(n x) dd(x)) \
& = 1/pi (1 - (-1)^n) integral_0^(pi slash 2) x sin(n x) dd(x)
$
notice that for even $n$, $b_n = 0$, let $n = 2k + 1$
$
b_n & = 1/pi (1 - (-1)^(2k + 1))
integral_0^(pi slash 2) x sin ((2k + 1)x) dd(x) \
& = 1/pi (1 + (-1)^(2k)) [1/n^2 sin(n x)
- x/n cos(n x)]_0^(pi slash 2) \
& = 2/(pi n^2) sin(pi/2 n) \
& = 2/(pi n^2) (-1)^((n-1)/2)
$
#{
let a_0 = calc.pi / 4
let a(n) = {
if calc.rem-euclid(n, 2) == 0 {
2 / (calc.pi * n * n) * (calc.pow(-1, n / 2) - 1)
} else { 0 }
}
let b(n) = {
if calc.rem-euclid(n, 2) == 1 {
2 / (calc.pi * n * n) * calc.pow(-1, (n - 1) / 2)
} else { 0 }
}
let fourier(x, n: 50) = (
a_0 / 2
+ range(1, n).map(n => a(n) * calc.cos(n * x)).sum()
+ range(1, n).map(n => b(n) * calc.sin(n * x)).sum()
)
let f(x) = {
let t = calc.rem-euclid(x, 2 * calc.pi)
if 0 < t and t < calc.pi / 2 {
t
} else if calc.pi / 2 < t and t <= calc.pi {
calc.pi - t
} else {
0
}
}
let xs = lq.linspace(-3 * calc.pi, 3 * calc.pi, num: 100)
let configs = (
(n: 5),
(n: 20),
(n: 100),
)
for config in configs {
lq.diagram(
title: [the fourier series of $f(x)$ with $N = #config.n$],
xlabel: $x$,
ylabel: $y$,
width: 100%,
height: 32%,
xlim: (-3 * calc.pi, 3 * calc.pi),
xaxis: (
locate-ticks: lq.locate-ticks-linear.with(unit: calc.pi),
format-ticks: lq.format-ticks-linear.with(suffix: $pi$),
),
lq.plot(xs, x => fourier(x, n: config.n), mark: none),
lq.plot(xs, f, mark: none),
)
}
}
== c)
let
$
f(x) = ccases(
-pi - x, -pi < x < -pi/2,
x, -pi/2 < x < pi/2,
pi - x, pi/2 < x <= pi,
)
$
then
$
a_0 & = 1/pi integral_(-pi)^pi f(x) dd(x) \
& = 1/pi (integral_(-pi)^(-pi slash 2) (-pi - x) dd(x)
+ cancel(integral_(-pi slash 2)^(pi slash 2) x dd(x))
+ integral_(pi slash 2)^pi (pi - x) dd(x)) \
& = 2/pi integral_(pi slash 2)^pi (pi - x) dd(x) \
& = 2/pi ([pi^2 - pi^2/2] - 1/2 [pi^2 - pi^2/4]) \
& = 2 pi - pi - pi + pi/2 \
& = pi/2
$
$
a_n & = 1/pi integral_(-pi)^pi f(x) cos(n x) dd(x) \
& = 2/pi integral_(pi slash 2)^pi (pi - x) cos(n x) dd(x) \
& = 2/pi [1/n^2 cos(n x) + (pi - x)/n sin(n x)]_(pi slash 2)^pi \
& = 2/pi (1/n^2 (-1)^n - 1/n^2 cos(pi/2 n) - pi/(2 n) sin(pi/2 n)) \
$
$
b_n & = 1/pi integral_(-pi)^pi f(x) sin(n x) dd(x) \
& = 2/pi (integral_(pi slash 2)^pi (pi - x) sin(n x) dd(x)
+ integral_0^(pi slash 2) x sin(n x) dd(x)) \
& = 2/pi ([(x - pi)/n cos(n x) - 1/n^2 sin(n x)]_(pi slash 2)^pi \
& + [1/n^2 sin(n x) - x/n cos(n x)]_0^(pi slash 2)) \
& = 2/pi (- pi/(2 n) cos(pi/2 n) - cancel(1/n^2 sin(pi/2 n)) \
& + cancel(1/n^2 sin(pi/2 n)) - pi/(2 n) cos(pi/2 n)) \
& = 2/n cos(-pi/2 n)
$
we could figure out what these expressions equal in each case ($mod 4$), but for
the sake of brevity, i'll leave it at that.
#{
let a_0 = calc.pi / 2
let a(n) = (
2
/ calc.pi
* (
1 / (n * n) * calc.pow(-1, n)
- 1 / (n * n) * calc.cos(calc.pi / 2 * n)
- calc.pi / (2 * n) * calc.sin(calc.pi / 2 * n)
)
)
let b(n) = (
2 / n * calc.cos(-calc.pi / 2 * n)
)
let fourier(x, n: 50) = (
a_0 / 2
+ range(1, n).map(n => a(n) * calc.cos(n * x)).sum()
+ range(1, n).map(n => b(n) * calc.sin(n * x)).sum()
)
let f(x) = {
let t = calc.rem-euclid(x, 2 * calc.pi)
if -calc.pi < t and t < -calc.pi / 2 {
-calc.pi - t
} else if -calc.pi / 2 < t and t < calc.pi / 2 {
t
} else {
calc.pi - t
}
}
let xs = lq.linspace(-3 * calc.pi, 3 * calc.pi, num: 100)
let configs = (
(n: 5),
(n: 20),
(n: 100),
)
for config in configs {
lq.diagram(
title: [the fourier series of $f(x)$ with $N = #config.n$],
xlabel: $x$,
ylabel: $y$,
width: 100%,
height: 32%,
xlim: (-3 * calc.pi, 3 * calc.pi),
xaxis: (
locate-ticks: lq.locate-ticks-linear.with(unit: calc.pi),
format-ticks: lq.format-ticks-linear.with(suffix: $pi$),
),
lq.plot(xs, x => fourier(x, n: config.n), mark: none),
lq.plot(xs, f, mark: none),
)
}
}
as we can see, i've made a mistake in my calculations.