Files
TMA4135/exercise9/problem4.typ

325 lines
8.7 KiB
Typst

#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.