Skip to content

Commit

Permalink
python for chapter 6
Browse files Browse the repository at this point in the history
  • Loading branch information
tobydriscoll committed Dec 10, 2024
1 parent 03a69ce commit b5171f7
Show file tree
Hide file tree
Showing 6 changed files with 693 additions and 102 deletions.
48 changes: 19 additions & 29 deletions chapter6/rk.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,27 @@ The second stage employs an Euler-style strategy over the whole time step, but u
Our implementation of IE2 is shown in {numref}`Function {number} <function-ie2>`.

(function-ie2)=
``````{prf:algorithm} ie2
`````{tab-set}
````{tab-item} Julia
:sync: julia
:::{embed} #function-ie2-julia
:::
````
````{prf:function} ie2
**Improved Euler method for an IVP**
```{code-block} julia
:lineno-start: 1
"""
ie2(ivp,n)
Apply the Improved Euler method to solve the given IVP using `n`
time steps. Returns a vector of times and a vector of solution
values.
"""
function ie2(ivp,n)
# Time discretization.
a,b = ivp.tspan
h = (b-a)/n
t = [ a + i*h for i in 0:n ]
# Initialize output.
u = fill(float(ivp.u0),n+1)
# Time stepping.
for i in 1:n
uhalf = u[i] + h/2*ivp.f(u[i],ivp.p,t[i]);
u[i+1] = u[i] + h*ivp.f(uhalf,ivp.p,t[i]+h/2);
end
return t,u
end
```
````{tab-item} MATLAB
:sync: matlab
:::{embed} #function-ie2-matlab
:::
````
````{tab-item} Python
:sync: python
:::{embed} #function-ie2-python
:::
````
`````
``````

## More Runge–Kutta methods

Expand Down
9 changes: 9 additions & 0 deletions chapter6/zerostability.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ For one-step methods such as Runge–Kutta, {numref}`Theorem %s <theorem-euler-o

(demo-zs-LIAF)=
::::{prf:example}
It is straightforward to check that the two-step method LIAF, defined by

```{math}
:label: LIAF
\mathbf{u}_{i+1} = -4u_i + 5u_{i-1} + h(4f_i + 2f_{i-1}),
```

is third-order accurate. Let's apply it to the ridiculously simple IVP $u'=u$, $u(0)=1$, whose solution is $e^t$.

`````{tab-set}
````{tab-item} Julia
:sync: julia
Expand Down
13 changes: 3 additions & 10 deletions julia/chapter6.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ The coupling makes the pendulums swap energy back and forth.
``````

(demo-rk-converge-julia)=
``````{dropdown} Convergence of RK4
``````{dropdown} Convergence of Runge–Kutta methods
We solve the IVP $u'=\sin[(u+t)^2]$ over $0\le t \le 4$, with $u(0)=-1$.
```{code-cell}
Expand Down Expand Up @@ -583,14 +583,7 @@ So AB4, which is supposed to be _more_ accurate than AM2, actually needs somethi

(demo-zs-LIAF-julia)=
``````{dropdown} Instability
It is straightforward to check that the two-step method LIAF, defined by
```{math}
:label: LIAF
\mathbf{u}_{i+1} = -4u_i + 5u_{i-1} + h(4f_i + 2f_{i-1}),
```
is third-order accurate. Let's apply it to the ridiculously simple IVP $u'=u$, $u(0)=1$, whose solution is $e^t$. We'll measure the error at the time $t=1$.
We'll measure the error at the time $t=1$.
```{code-cell}
du_dt(u, t) = u
Expand All @@ -604,7 +597,7 @@ for n in n
t = [a + i * h for i in 0:n]
u = [1; û(h); zeros(n - 1)]
f_val = [du_dt(u[1], t[1]); zeros(n)]
f_valor i in 2:n
for i in 2:n
f_val[i] = du_dt(u[i], t[i])
u[i+1] = -4 * u[i] + 5 * u[i-1] + h * (4 * f_val[i] + 2 * f_val[i-1])
end
Expand Down
2 changes: 2 additions & 0 deletions myst.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ project:
heading_1: true
heading_2: true
heading_3: false
output_matplotlib_strings: remove
toc:
# Auto-generated by `myst init --write-toc`
- file: home.md
Expand Down Expand Up @@ -215,6 +216,7 @@ project:
- file: python/chapter3.md
- file: python/chapter4.md
- file: python/chapter5.md
- file: python/chapter6.md
- file: genindex.md


Expand Down
Loading

0 comments on commit b5171f7

Please sign in to comment.