RosettaCodeData/Task/Runge-Kutta-method/Excel/runge-kutta-method.excel
2023-07-01 13:44:08 -04:00

37 lines
1.1 KiB
Text

//Worksheet formula to manage looping
=LET(
T₊, SEQUENCE(11, 1, 0, 1),
T, DROP(T₊, -1),
τ, SEQUENCE(1 / δt, 1, 0, δt),
calculated, SCAN(1, T, LAMBDA(y₀, t, REDUCE(y₀, t + τ, RungaKutta4λ(Dλ)))),
calcs, VSTACK(1, calculated),
exact, f(T₊),
HSTACK(T₊, calcs, exact, (exact - calcs) / exact)
)
//Lambda function passed to RungaKutta4λ to evaluate derivatives
Dλ(y,t)
= LAMBDA(y,t, t * SQRT(y))
//Curried Lambda function with derivative function D and y, t as parameters
RungaKutta4λ(Dλ)
= LAMBDA(D,
LAMBDA(yᵣ, tᵣ,
LET(
δy₁, δt * D(yᵣ, tᵣ),
δy₂, δt * D(yᵣ + δy₁ / 2, tᵣ + δt / 2),
δy₃, δt * D(yᵣ + δy₂ / 2, tᵣ + δt / 2),
δy₄, δt * D(yᵣ + δy₃, tᵣ + δt),
yᵣ₊₁, yᵣ + (δy₁ + 2 * δy₂ + 2 * δy₃ + δy₄) / 6,
yᵣ₊₁
)
)
)
//Lambda function returning the exact solution
f(t)
= LAMBDA(t, (1/16) * (t^2 + 4)^2 )