Files
TMA4135/exercise7/problem4.typ
2025-10-10 22:42:11 +02:00

71 lines
1.7 KiB
Typst

#import "@preview/physica:0.9.6": *
== a)
given the explicit midpoint method
$
y_(n+1) = y_n + h f(t_n + 0.5h, y_n + 0.5h f(t_n, y_n)),
$
notice $k_1 = f(t_n, y_n)$ appears at depth 1 meaning the funcion is only nested
twice, such that our number of stages $s = 2$. furthermore, we have $c_2 = 0.5$
from the time step and $a_21 = 0.5$ from the function step. we can also observe
that $b_1 = 1$ and $b_2 = 0$. this can be visualized in a mnemonic butcher
tableau.
#let butcher(s, nums) = {
let nums = nums.map(x => $#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
)
}
#align(center)[#butcher(2, (0.5, 0.5, 1, 0))]
#pagebreak()
== b)
I don't wanna write python right now, so I'm gonna solve this in typst. yes, I'm
coding this in the typesetting language I use to write this text.
#let code = ```typc
let h = 0.01;
let f(t, y) = calc.exp(-y * y);
let y_1(t_0, y_0) = y_0
+ h * f(t_0 + 0.5 * h,
y_0 + 0.5 * h * f(t_0, y_0));
let t = 0;
let y = 1;
while t < 1 {
y = y_1(t, y);
t += h;
};
[the value for $y$ at $t = 1$ is
#calc.round(y, digits: 4)]
```
#eval(code.text.split("\n").join())
#code